<?php

namespace Tests\Feature;

use App\Models\FingerprintMachine;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Artisan;
use Livewire\Livewire;
use Tests\TestCase;

class SyncTimeTest extends TestCase
{
    use RefreshDatabase;

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

    public function test_sync_time_console_command_runs_successfully(): void
    {
        FingerprintMachine::create([
            'name' => 'Cloud Machine Time Sync Test',
            'ip_address' => '127.0.0.1',
            'connection_type' => 'adms',
            'protocol' => 'adms_push',
            'cloud_id' => 'FS-REVO-TIME-01',
            'status' => 'offline',
        ]);

        $exitCode = Artisan::call('fingerprint:sync-time');
        $this->assertEquals(0, $exitCode);
    }

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

        $machine = FingerprintMachine::create([
            'name' => 'Lobby Machine Time Sync UI Test',
            'ip_address' => '127.0.0.1',
            'connection_type' => 'adms',
            'protocol' => 'adms_push',
            'cloud_id' => 'FS-REVO-TIME-UI-01',
            'status' => 'offline',
        ]);

        Livewire::test(\App\Livewire\Machines\MachineIndex::class)
            ->call('syncTime', $machine->id)
            ->assertHasNoErrors();
    }
}
