<?php

namespace Tests\Feature;

use App\Livewire\Backup\BackupRestoreIndex;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class BackupTest extends TestCase
{
    use RefreshDatabase;

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

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

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

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

        Livewire::test(BackupRestoreIndex::class)
            ->call('createBackup', 'db')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('backups', ['type' => 'db']);
    }
}
