<?php

namespace Tests\Feature\Api\V1\Scores;

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

class ScoreControllerTest extends TestCase
{
    use RefreshDatabase;

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

        DB::table('teacher_class')->insert([
            'teacher_id' => 1,
            'class_section_id' => 1,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
        ]);

        $response = $this->json('GET', '/api/v1/scores/overview', [
            'class_section_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

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

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

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

        $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('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([
            'user_id' => 1,
            'role_id' => 1,
        ]);

        DB::table('classSection')->insert([
            '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([
            'student_id' => 100,
            'class_section_id' => 1,
            'school_year' => '2025-2026',
            'is_event_only' => 0,
            'semester' => 'Fall',
        ]);
    }
}
