<?php

namespace Tests\Feature;

use App\Models\AttendanceLog;
use App\Models\AttendanceRecord;
use App\Models\FingerprintMachine;
use App\Models\Shift;
use App\Models\SyncLog;
use App\Models\User;
use App\Services\AttendanceProcessorService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class PrdAdjustmentsTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_dashboard_renders_latest_sync_logs_widget(): void
    {
        $admin = User::where('email', 'admin@smkn1gunungagung.sch.id')->first();
        $admin->update(['force_password_change' => false]);
        $this->actingAs($admin);

        $machine = FingerprintMachine::first();
        SyncLog::create([
            'machine_id' => $machine->id,
            'status' => 'Success',
            'rows_fetched' => 12,
            'message' => 'Test sync message PRD',
        ]);

        Livewire::test(\App\Livewire\Dashboard::class)
            ->assertViewHas('latestSyncLogs')
            ->assertSee('Aktivitas Sinkronisasi Terkini')
            ->assertSee('Test sync message PRD');
    }

    public function test_attendance_processor_respects_grace_period_minutes(): void
    {
        $shift = Shift::create([
            'name' => 'Shift Grace Test',
            'start_time' => '07:00:00',
            'end_time' => '15:30:00',
            'grace_period_minutes' => 15, // Toleransi sampai 07:15
        ]);

        $siswa = User::factory()->create([
            'role_id' => 5, // Siswa
            'shift_id' => $shift->id,
            'nip_nis' => '999888777',
        ]);

        $processor = app(AttendanceProcessorService::class);

        // Absen pada 07:12:00 (masih dalam grace period 15 menit) -> Harus HADIR
        $record = $processor->processUserLogForDate($siswa, '2026-07-29', '07:12:00');
        $this->assertEquals('Hadir', $record->status);

        // Absen pada 07:18:00 (lewat grace period 15 menit) -> Harus TERLAMBAT
        $siswa2 = User::factory()->create([
            'role_id' => 5,
            'shift_id' => $shift->id,
            'nip_nis' => '999888666',
        ]);
        $record2 = $processor->processUserLogForDate($siswa2, '2026-07-29', '07:18:00');
        $this->assertEquals('Terlambat', $record2->status);
    }

    public function test_personnel_and_report_support_class_name_filtering(): void
    {
        $admin = User::where('email', 'admin@smkn1gunungagung.sch.id')->first();
        $admin->update(['force_password_change' => false]);
        $this->actingAs($admin);

        $today = now()->toDateString();
        $currentMonth = now()->format('Y-m');

        $siswaClassX = User::factory()->create([
            'role_id' => 5,
            'name' => 'Siswa Kelas X RPL 1',
            'class_name' => 'X RPL 1',
        ]);

        AttendanceRecord::create([
            'user_id' => $siswaClassX->id,
            'date' => $today,
            'check_in' => '06:55:00',
            'status' => 'Hadir',
        ]);

        Livewire::test(\App\Livewire\Reports\ReportIndex::class)
            ->set('month', $currentMonth)
            ->set('selectedClassName', 'X RPL 1')
            ->assertSee('Siswa Kelas X RPL 1');
    }
}
