<?php

namespace Tests\Unit\Services\Reports\Stickers;

use App\Services\Reports\Stickers\StickerContextService;
use App\Services\Reports\Stickers\StickerQueryService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class StickerQueryServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_list_students_by_class_returns_rows(): void
    {
        $this->seedData();
        $service = new StickerQueryService(new StickerContextService());

        $students = $service->listStudentsByClass(200, '2025-2026');

        $this->assertCount(1, $students);
        $this->assertSame(100, $students[0]['id']);
    }

    public function test_list_students_for_print_all_excludes_youth_and_kg(): void
    {
        $this->seedData();
        $this->seedYouthClass();

        $service = new StickerQueryService(new StickerContextService());
        $students = $service->listStudentsForPrintAll('2025-2026');

        $this->assertCount(1, $students);
        $this->assertSame('Grade 1', $students[0]['class_section_name']);
    }

    private function seedData(): void
    {
        DB::table('classes')->insert([
            ['id' => 1, 'class_name' => 'Grade 1', 'created_at' => now(), 'updated_at' => now()],
        ]);

        DB::table('classSection')->insert([
            'class_section_id' => 200,
            'class_section_name' => 'Grade 1',
            'class_id' => 1,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('students')->insert([
            'id' => 100,
            'school_id' => 1,
            'firstname' => 'Student',
            'lastname' => 'One',
            'parent_id' => 10,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('student_class')->insert([
            'student_id' => 100,
            'class_section_id' => 200,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }

    private function seedYouthClass(): void
    {
        DB::table('classes')->insert([
            ['id' => 2, 'class_name' => 'Youth', 'created_at' => now(), 'updated_at' => now()],
        ]);

        DB::table('classSection')->insert([
            'class_section_id' => 201,
            'class_section_name' => 'Youth',
            'class_id' => 2,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('students')->insert([
            'id' => 101,
            'school_id' => 1,
            'firstname' => 'Youth',
            'lastname' => 'Student',
            'parent_id' => 11,
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('student_class')->insert([
            'student_id' => 101,
            'class_section_id' => 201,
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'is_event_only' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
    }
}
