<?php

namespace Tests\Feature;

use App\Livewire\Permissions\RolePermissionIndex;
use App\Models\Role;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class PermissionTest extends TestCase
{
    use RefreshDatabase;

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

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

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

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

        $guruRole = Role::where('name', 'guru')->first();

        Livewire::test(RolePermissionIndex::class)
            ->set('selectedRoleId', $guruRole->id)
            ->set('rolePermissions', [1, 2, 3])
            ->call('savePermissions')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('role_has_permissions', ['role_id' => $guruRole->id]);
    }
}
