<?php

namespace Tests\Unit\Services\ClassPrep;

use App\Services\ClassPrep\StickerCountService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class StickerCountServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_all_returns_totals_excluding_youth(): void
    {
        $this->seedStickerData();

        $service = new StickerCountService();
        $payload = $service->listAll('2025-2026', 'Fall');

        $this->assertSame(2, $payload['totals']['students']);
        $this->assertSame(6, $payload['totals']['primary']);
        $this->assertSame(1, $payload['totals']['secondary']);
    }

    public function test_list_for_class_limits_results(): void
    {
        $this->seedStickerData();

        $service = new StickerCountService();
        $payload = $service->listForClass('2025-2026', 'Fall', 101);

        $this->assertSame(1, $payload['totals']['students']);
        $this->assertSame(3, $payload['totals']['primary']);
        $this->assertSame(0, $payload['totals']['secondary']);
    }

    private function seedStickerData(): void
    {
        DB::table('configuration')->insert([
            ['id' => 1, 'config_key' => 'school_year', 'config_value' => '2025-2026'],
            ['id' => 2, 'config_key' => 'semester', 'config_value' => 'Fall'],
        ]);

        DB::table('classes')->insert([
            [
                'id' => 1,
                'class_name' => 'Class 1',
                'schedule' => 'Sun',
                'capacity' => 20,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 2,
                'class_name' => 'Class 2',
                'schedule' => 'Sun',
                'capacity' => 20,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 3,
                'class_name' => 'Class 3',
                'schedule' => 'Sun',
                'capacity' => 20,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);

        DB::table('classSection')->insert([
            [
                'id' => 1,
                'class_id' => 1,
                'class_section_id' => 101,
                'class_section_name' => '1-A',
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 2,
                'class_id' => 2,
                'class_section_id' => 102,
                'class_section_name' => '5-B',
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'id' => 3,
                'class_id' => 3,
                'class_section_id' => 103,
                'class_section_name' => 'Youth-1',
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);

        DB::table('students')->insert([
            [
                '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,
                'is_new' => 0,
            ],
            [
                'school_id' => 'S-2',
                'firstname' => 'Student',
                'lastname' => 'Two',
                'age' => 10,
                'gender' => 'Female',
                'photo_consent' => 1,
                'parent_id' => 1,
                'year_of_registration' => '2025',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'is_active' => 1,
                'is_new' => 0,
            ],
            [
                'school_id' => 'S-3',
                'firstname' => 'Student',
                'lastname' => 'Three',
                'age' => 12,
                'gender' => 'Female',
                'photo_consent' => 1,
                'parent_id' => 1,
                'year_of_registration' => '2025',
                'school_year' => '2025-2026',
                'semester' => 'Fall',
                'is_active' => 1,
                'is_new' => 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' => 102,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
            [
                'student_id' => 3,
                'class_section_id' => 103,
                'semester' => 'Fall',
                'school_year' => '2025-2026',
            ],
        ]);
    }
}
