<?php

namespace Tests\Feature\Api\V1\Reports;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class StickersControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_form_data_returns_classes_students_and_presets(): void
    {
        $this->seedConfig();
        $user = $this->seedUser();
        $this->seedClassAndStudent();

        Sanctum::actingAs($user);
        $response = $this->getJson('/api/v1/reports/stickers/form-data?school_year=2025-2026');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertNotEmpty($response->json('data.classes'));
        $this->assertNotEmpty($response->json('data.students'));
        $this->assertNotEmpty($response->json('data.presets'));
        $this->assertArrayHasKey('value', $response->json('data.presets.0'));
    }

    public function test_students_by_class_returns_students(): void
    {
        $this->seedConfig();
        $user = $this->seedUser();
        $this->seedClassAndStudent();

        Sanctum::actingAs($user);
        $response = $this->getJson('/api/v1/reports/stickers/students?class_section_id=200&school_year=2025-2026');

        $response->assertOk();
        $response->assertJsonPath('status', true);
        $this->assertNotEmpty($response->json('data.students'));
    }

    public function test_print_returns_pdf(): void
    {
        $this->seedConfig();
        $user = $this->seedUser();
        $this->seedClassAndStudent();

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/reports/stickers/print', [
            'student_id' => 100,
            'school_year' => '2025-2026',
            'sticker_size' => '102x34',
        ]);

        $response->assertOk();
        $this->assertSame('application/pdf', $response->headers->get('content-type'));
    }

    public function test_print_validation_rejects_missing_selection(): void
    {
        $this->seedConfig();
        $user = $this->seedUser();

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/reports/stickers/print', [
            'school_year' => '2025-2026',
        ]);

        $response->assertStatus(422);
        $response->assertJsonPath('message', 'Validation failed.');
    }

    public function test_requires_authentication(): void
    {
        $response = $this->getJson('/api/v1/reports/stickers/form-data');

        $response->assertStatus(401);
    }

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

    private function seedUser(): User
    {
        $userId = DB::table('users')->insertGetId([
            'firstname' => 'Admin',
            'lastname' => 'User',
            'cellphone' => '9999999999',
            'email' => 'stickers@example.com',
            'address_street' => '123 Street',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'password' => bcrypt('password'),
            'user_type' => 'primary',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
            'status' => 'Active',
        ]);

        return User::query()->findOrFail($userId);
    }

    private function seedClassAndStudent(): 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(),
        ]);
    }
}
