<?php

namespace Tests\Feature\Api\V1\CompetitionScores;

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

class CompetitionScoresControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_returns_competitions_and_counts(): void
    {
        $this->seedCompetitionData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/competition-scores');

        $response->assertOk();
        $response->assertJson([
            'ok' => true,
            'activeClassId' => 101,
        ]);
        $this->assertNotEmpty($response->json('competitions'));
        $this->assertSame(1, $response->json('studentTotal'));
    }

    public function test_edit_returns_students_and_scores(): void
    {
        $this->seedCompetitionData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/competition-scores/1');

        $response->assertOk();
        $this->assertNotEmpty($response->json('students'));
        $this->assertSame(5, $response->json('scoreMap.1'));
        $this->assertSame(20, $response->json('questionCount'));
    }

    public function test_save_updates_scores(): void
    {
        $this->seedCompetitionData();

        $user = $this->createUser();
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/competition-scores/1', [
            'class_section_id' => 101,
            'scores' => [
                1 => 10,
            ],
        ]);

        $response->assertOk();
        $response->assertJson([
            'ok' => true,
        ]);

        $this->assertDatabaseHas('competition_scores', [
            'competition_id' => 1,
            'student_id' => 1,
            'class_section_id' => 101,
            'score' => 10,
        ]);
    }

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

        DB::table('classes')->insert([
            'id' => 1,
            'class_name' => 'Class 1',
            'schedule' => 'Sun',
            'capacity' => 20,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

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

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

        DB::table('students')->insert([
            'id' => 1,
            'school_id' => 'S-1',
            'firstname' => 'Student',
            'lastname' => 'One',
            'age' => 8,
            'gender' => 'Male',
            'photo_consent' => 1,
            'parent_id' => 1,
            'year_of_registration' => '2025',
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_active' => 1,
        ]);

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

        DB::table('competitions')->insert([
            'id' => 1,
            'title' => 'Quiz',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'class_section_id' => 101,
            'is_locked' => 0,
        ]);

        DB::table('competition_class_winners')->insert([
            'competition_id' => 1,
            'class_section_id' => 101,
            'question_count' => 20,
        ]);

        DB::table('competition_scores')->insert([
            'competition_id' => 1,
            'student_id' => 1,
            'class_section_id' => 101,
            'score' => 5,
        ]);
    }

    private function createUser(): User
    {
        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',
        ]);

        return User::query()->findOrFail(1);
    }
}
