<?php

namespace Tests\Unit\Services\Scores;

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

class ProjectScoreServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_returns_headers_and_scores(): void
    {
        $this->seedStudent(1, 'SCH1', 'Maya', 'Stone');
        $this->seedStudentClass(1, 10, '2025-2026');

        DB::table('project')->insert([
            'student_id' => 1,
            'school_id' => 'SCH1',
            'class_section_id' => 10,
            'updated_by' => 99,
            'project_index' => 1,
            'score' => 88,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

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

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

    public function test_update_creates_scores_and_missing_overrides(): void
    {
        $this->seedStudent(1, 'SCH1', 'Maya', 'Stone');
        $this->seedStudentClass(1, 10, '2025-2026');

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

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

        $this->assertSame(1, $count);
        $this->assertDatabaseHas('project', [
            'student_id' => 1,
            'class_section_id' => 10,
            'project_index' => 1,
            'score' => 92.0,
        ]);
        $this->assertDatabaseHas('missing_score_overrides', [
            'student_id' => 1,
            'class_section_id' => 10,
            'item_type' => 'project',
            'item_index' => 1,
            'is_allowed' => 1,
        ]);
    }

    public function test_add_column_inserts_next_index(): void
    {
        $this->seedStudent(1, 'SCH1', 'Maya', 'Stone');
        $this->seedStudentClass(1, 10, '2025-2026');

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

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

        $this->assertSame(1, $nextIndex);
        $this->assertDatabaseHas('project', [
            'student_id' => 1,
            'class_section_id' => 10,
            'project_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' => '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,
        ]);
    }
}
