<?php

namespace Tests\Feature\Api\V1\Grading;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class GradingControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_overview_returns_grading_data(): void
    {
        $this->seedBase();
        Sanctum::actingAs(User::query()->findOrFail(1));

        $response = $this->getJson('/api/v1/grading/overview?school_year=2025-2026&semester=Fall&class_id=1');

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertNotEmpty($response->json('grades'));
    }

    public function test_toggle_lock_locks_section(): void
    {
        $this->seedBase();
        Sanctum::actingAs(User::query()->findOrFail(1));

        $response = $this->postJson('/api/v1/grading/locks/toggle', [
            'class_section_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertDatabaseHas('grading_locks', [
            'class_section_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_locked' => 1,
        ]);
    }

    private function seedBase(): void
    {
        DB::table('configuration')->insert([
            ['id' => 1, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['id' => 2, 'config_key' => 'semester', 'config_value' => 'Fall'],
            ['id' => 3, 'config_key' => 'parent_scores_released_fall', 'config_value' => '0'],
            ['id' => 4, 'config_key' => 'parent_scores_released_spring', 'config_value' => '0'],
        ]);

        DB::table('roles')->insert([
            ['id' => 1, 'name' => 'teacher', 'slug' => 'teacher', 'is_active' => 1],
        ]);

        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Teacher',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'teacher@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('user_roles')->insert([
            'id' => 1,
            'user_id' => 1,
            'role_id' => 1,
        ]);

        DB::table('classSection')->insert([
            'id' => 1,
            'class_section_id' => 1,
            'class_section_name' => '1A',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('students')->insert([
            'id' => 100,
            'parent_id' => 10,
            'firstname' => 'Kid',
            'lastname' => 'User',
            'school_id' => 1,
            'age' => 10,
            'gender' => 'M',
            'photo_consent' => 1,
            'year_of_registration' => '2025',
            'is_active' => 1,
        ]);

        DB::table('student_class')->insert([
            'id' => 1,
            'student_id' => 100,
            'class_section_id' => 1,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
        ]);

        DB::table('semester_scores')->insert([
            'id' => 1,
            'student_id' => 100,
            'school_id' => 1,
            'class_section_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'semester_score' => 88,
        ]);
    }
}
