<?php

namespace Tests\Feature\Api\V1\Attendance;

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

class LateSlipLogsControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_index_requires_admin(): void
    {
        $user = $this->createUser('teacher');
        Sanctum::actingAs($user);

        $response = $this->getJson('/api/v1/attendance/late-slip-logs');

        $response->assertForbidden();
    }

    public function test_index_returns_logs(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        DB::table('late_slip_logs')->insert([
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'student_name' => 'Student One',
            'slip_date' => '2025-09-01',
            'time_in' => '08:10:00',
            'grade' => '3',
            'reason' => 'Traffic',
            'admin_name' => 'Admin User',
        ]);

        $response = $this->getJson('/api/v1/attendance/late-slip-logs?school_year=2025-2026');

        $response->assertOk();
        $response->assertJsonPath('data.logs.0.student_name', 'Student One');
        $response->assertJsonStructure(['data' => ['meta']]);
    }

    public function test_show_returns_log(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        DB::table('late_slip_logs')->insert([
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'student_name' => 'Student Two',
            'slip_date' => '2025-09-02',
            'time_in' => '08:15:00',
            'grade' => '2',
            'reason' => 'Bus delay',
            'admin_name' => 'Admin User',
        ]);

        $response = $this->getJson('/api/v1/attendance/late-slip-logs/1');

        $response->assertOk();
        $response->assertJsonPath('data.log.student_name', 'Student Two');
    }

    public function test_destroy_deletes_log(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        DB::table('late_slip_logs')->insert([
            'school_year' => '2025-2026',
            'semester' => 'Fall',
            'student_name' => 'Student Three',
            'slip_date' => '2025-09-03',
            'time_in' => '08:20:00',
            'grade' => '1',
            'reason' => 'Weather',
            'admin_name' => 'Admin User',
        ]);

        $response = $this->deleteJson('/api/v1/attendance/late-slip-logs/1');

        $response->assertOk();
        $this->assertDatabaseMissing('late_slip_logs', [
            'id' => 1,
        ]);
    }

    public function test_index_validation_rejects_bad_dates(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        $response = $this->getJson('/api/v1/attendance/late-slip-logs?date_from=not-a-date');

        $response->assertStatus(422);
        $response->assertJsonStructure(['message', 'errors']);
    }

    private function createUser(string $roleName): User
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => $roleName,
            'priority' => 1,
            'is_active' => 1,
        ]);

        $user = User::query()->create([
            'firstname' => 'Test',
            'lastname' => 'User',
            'email' => $roleName . '@example.com',
            'cellphone' => '5555555555',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'status' => 'Active',
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $user->id,
            'role_id' => $roleId,
        ]);

        return $user;
    }
}
