<?php

namespace Tests\Unit\Services\Scores;

use App\Services\Scores\QuizScoreService;
use App\Services\Scores\ScoreTermService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class QuizScoreServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_returns_headers_and_scores(): void
    {
        $this->seedStudent(1, 'SCH1', 'Nina', 'Kim');
        $this->seedStudentClass(1, 5, '2025-2026');

        DB::table('quiz')->insert([
            'student_id' => 1,
            'school_id' => 'SCH1',
            'class_section_id' => 5,
            'updated_by' => 99,
            'quiz_index' => 2,
            'score' => 77,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $service = new QuizScoreService(new ScoreTermService());
        $result = $service->list(5, 'Fall', '2025-2026');

        $this->assertSame([2], $result['headers']);
        $this->assertCount(1, $result['students']);
        $this->assertSame(77.0, $result['students'][0]['scores'][2]);
    }

    public function test_update_creates_scores_and_missing_overrides(): void
    {
        $this->seedStudent(1, 'SCH1', 'Nina', 'Kim');
        $this->seedStudentClass(1, 5, '2025-2026');

        $service = new QuizScoreService(new ScoreTermService());

        $count = $service->update(
            5,
            'Fall',
            '2025-2026',
            [1 => [1 => '85']],
            [1 => [1 => true]],
            99
        );

        $this->assertSame(1, $count);
        $this->assertDatabaseHas('quiz', [
            'student_id' => 1,
            'class_section_id' => 5,
            'quiz_index' => 1,
            'score' => 85.0,
        ]);
        $this->assertDatabaseHas('missing_score_overrides', [
            'student_id' => 1,
            'class_section_id' => 5,
            'item_type' => 'quiz',
            'item_index' => 1,
            'is_allowed' => 1,
        ]);
    }

    public function test_add_column_inserts_next_index(): void
    {
        $this->seedStudent(1, 'SCH1', 'Nina', 'Kim');
        $this->seedStudentClass(1, 5, '2025-2026');

        $service = new QuizScoreService(new ScoreTermService());

        $nextIndex = $service->addColumn(5, 'Fall', '2025-2026', 99);

        $this->assertSame(1, $nextIndex);
        $this->assertDatabaseHas('quiz', [
            'student_id' => 1,
            'class_section_id' => 5,
            'quiz_index' => 1,
        ]);
    }

    private function seedStudent(int $id, string $schoolId, string $first, string $last): void
    {
        DB::table('students')->insert([
            'id' => $id,
            'school_id' => $schoolId,
            'firstname' => $first,
            'lastname' => $last,
            'age' => 10,
            'gender' => 'F',
            'photo_consent' => 1,
            'parent_id' => 1,
            'year_of_registration' => '2025',
            'is_active' => 1,
        ]);
    }

    private function seedStudentClass(int $studentId, int $classSectionId, string $schoolYear): void
    {
        DB::table('student_class')->insert([
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'school_year' => $schoolYear,
            'semester' => 'Fall',
            'is_event_only' => 0,
        ]);
    }
}
