<?php

namespace Tests\Unit\Services\Reports\Stickers;

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

class StickerPrintServiceTest extends TestCase
{
    use RefreshDatabase;

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

        $result = $service->generate([
            'student_id' => 100,
            'school_year' => '2025-2026',
            'sticker_size' => '102x34',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertNotEmpty($result['pdf']);
    }

    public function test_generate_returns_error_when_no_students(): void
    {
        $service = new StickerPrintService(new StickerQueryService(new StickerContextService()));

        $result = $service->generate([
            'class_section_id' => 999,
            'school_year' => '2025-2026',
        ]);

        $this->assertFalse($result['ok']);
        $this->assertSame('No students found in this class.', $result['message']);
    }

    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(),
        ]);
    }
}
