<?php

namespace Tests\Unit\Services\ClassPreparation;

use App\Services\ClassPreparation\ClassPreparationRosterService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ClassPreparationRosterServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_get_class_section_student_counts(): void
    {
        $this->seedRoster();

        $service = new ClassPreparationRosterService();
        $rows = $service->getClassSectionStudentCounts('2025-2026', 'Fall', true);

        $this->assertCount(1, $rows);
        $this->assertSame(101, (int) $rows[0]->class_section_id);
        $this->assertSame(2, (int) $rows[0]->student_count);
    }

    public function test_get_student_count_for_section(): void
    {
        $this->seedRoster();

        $service = new ClassPreparationRosterService();
        $count = $service->getStudentCountForSection('2025-2026', 'Fall', true, '101');

        $this->assertSame(2, $count);
    }

    private function seedRoster(): void
    {
        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,
            ],
            [
                'id' => 2,
                'school_id' => 'S-2',
                'firstname' => 'Student',
                'lastname' => 'Two',
                'age' => 9,
                'gender' => 'Female',
                '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',
            ],
            [
                'student_id' => 2,
                'class_section_id' => 101,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);
    }
}
