<?php

namespace Tests\Feature;

use App\Livewire\Sync\SyncIndex;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class SyncTest extends TestCase
{
    use RefreshDatabase;

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

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

        $response = $this->get('/sync');
        $response->assertStatus(200);
    }

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

        Livewire::test(SyncIndex::class)
            ->call('syncAll')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('sync_logs', []);
    }

    public function test_artisan_sync_command_runs_successfully(): void
    {
        $this->artisan('fingerprint:sync')
            ->assertExitCode(0);
    }
}
