<?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 SlipPrinterControllerTest extends TestCase
{
    use RefreshDatabase;

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

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/reports/slips/preview', [
            'student_name' => 'Ali Student',
            'school_year' => '2025-2026',
        ]);

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertNotEmpty($response->json('text'));
    }

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

        Sanctum::actingAs($user);
        $response = $this->postJson('/api/v1/reports/slips/print', [
            'student_name' => 'Nora Student',
            'school_year' => '2025-2026',
            'date' => '09/01/2025',
            'time_in' => '8:10 AM',
            'grade' => '3',
        ]);

        $response->assertOk();
        $this->assertSame('application/pdf', $response->headers->get('content-type'));
        $this->assertDatabaseHas('late_slip_logs', [
            'student_name' => 'Nora Student',
            'school_year' => '2025-2026',
        ]);
    }

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

        DB::table('late_slip_logs')->insert([
            'school_year' => '2025-2026',
            'semester' => 'fall',
            'student_name' => 'Log Student',
            'slip_date' => '2025-09-01',
            'time_in' => '08:00:00',
            'grade' => '2',
            'reason' => 'Late',
            'admin_name' => 'Admin',
            'printed_by' => $user->id,
            'printed_at' => now(),
        ]);

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

        $response->assertOk();
        $response->assertJson(['ok' => true]);
        $this->assertNotEmpty($response->json('logs'));
    }

    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' => 'slip.printer@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);
    }
}
