<?php

namespace Tests\Unit\Services\Roles;

use App\Services\Roles\RolePermissionService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class RolePermissionServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_save_role_permissions_persists_rows(): void
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'admin',
            'slug' => 'admin',
            'description' => 'Admin',
            'dashboard_route' => 'administrator/administratordashboard',
            'priority' => 1,
            'is_active' => 1,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        $permissionId = DB::table('permissions')->insertGetId([
            'name' => 'manage_roles',
            'description' => 'Manage roles',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $service = new RolePermissionService();
        $service->saveRolePermissions($roleId, [
            $permissionId => ['create' => true, 'read' => true],
        ]);

        $this->assertDatabaseHas('role_permissions', [
            'role_id' => $roleId,
            'permission_id' => $permissionId,
            'can_create' => 1,
            'can_read' => 1,
        ]);
    }
}
