<?php

namespace Tests\Unit\Services\Scores;

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

class ExamScoreServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_returns_latest_exam_score(): void
    {
        $this->seedStudent(1, 'SCH1', 'Leo', 'Park');
        $this->seedStudentClass(1, 3, '2025-2026');

        DB::table('midterm_exam')->insert([
            [
                'student_id' => 1,
                'school_id' => 'SCH1',
                'class_section_id' => 3,
                'updated_by' => 99,
                'score' => 70,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'student_id' => 1,
                'school_id' => 'SCH1',
                'class_section_id' => 3,
                'updated_by' => 99,
                'score' => 80,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);

        $service = new ExamScoreService(new ScoreTermService());
        $result = $service->list('midterm_exam', 3, 'Fall', '2025-2026');

        $this->assertCount(1, $result['students']);
        $this->assertSame(80.0, $result['students'][0]['score']);
    }

    public function test_update_creates_exam_score_and_missing_override(): void
    {
        $this->seedStudent(1, 'SCH1', 'Leo', 'Park');
        $this->seedStudentClass(1, 3, '2025-2026');

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

        $count = $service->update(
            'midterm_exam',
            3,
            'Fall',
            '2025-2026',
            [1 => ['score' => 95]],
            [1 => true],
            99
        );

        $this->assertSame(1, $count);
        $this->assertDatabaseHas('midterm_exam', [
            'student_id' => 1,
            'class_section_id' => 3,
            'score' => 95.0,
        ]);
        $this->assertDatabaseHas('missing_score_overrides', [
            'student_id' => 1,
            'class_section_id' => 3,
            'item_type' => 'midterm_exam',
            'item_index' => null,
            'is_allowed' => 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' => 11,
            'gender' => 'M',
            '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,
        ]);
    }
}
