<?php

namespace Tests\Unit\Services\Scores;

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

class ScoreDashboardServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_overview_returns_students_and_scores(): void
    {
        $this->seedClassSection(10, '1A');
        $this->seedStudent(1, 'SCH1', 'Noah', 'West', 5);
        $this->seedStudentClass(1, 10, '2025-2026');

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

        DB::table('semester_scores')->insert([
            'student_id' => 1,
            'school_id' => 'SCH1',
            'class_section_id' => 10,
            'homework_avg' => 85,
            'quiz_avg' => 90,
            'project_avg' => 92,
            'attendance_score' => 95,
            'ptap_score' => 90,
            'semester_score' => 91,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $service = new ScoreDashboardService(new ScoreTermService());
        $result = $service->overview(1, null, 'Fall', '2025-2026');

        $this->assertSame(10, $result['class_section_id']);
        $this->assertSame('1A', $result['class_section_name']);
        $this->assertSame(85.0, $result['students'][0]['scores']['homework']);
    }

    public function test_view_student_scores_groups_by_year_and_semester(): void
    {
        $this->seedClassSection(10, '1A');
        $this->seedStudent(2, 'SCH2', 'Mia', 'Clark', 7);
        $this->seedStudentClass(2, 10, '2025-2026');

        DB::table('semester_scores')->insert([
            'student_id' => 2,
            'school_id' => 'SCH2',
            'class_section_id' => 10,
            'homework_avg' => 80,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $service = new ScoreDashboardService(new ScoreTermService());
        $result = $service->viewStudentScores(7, '2025-2026');

        $this->assertArrayHasKey('2025-2026', $result['scores']);
        $this->assertArrayHasKey('Fall', $result['scores']['2025-2026']);
        $this->assertArrayHasKey(2, $result['scores']['2025-2026']['Fall']);
    }

    private function seedStudent(int $id, string $schoolId, string $first, string $last, int $parentId): void
    {
        DB::table('students')->insert([
            'id' => $id,
            'school_id' => $schoolId,
            'firstname' => $first,
            'lastname' => $last,
            'age' => 12,
            'gender' => 'F',
            'photo_consent' => 1,
            'parent_id' => $parentId,
            '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,
        ]);
    }

    private function seedClassSection(int $classSectionId, string $name): void
    {
        DB::table('classSection')->insert([
            'class_section_id' => $classSectionId,
            'class_section_name' => $name,
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);
    }
}
