<?php

namespace Tests\Unit\Services\Auth;

use App\Services\Auth\UserRoleService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class UserRoleServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_has_permission_for_crud_checks_roles(): void
    {
        $permissionId = DB::table('permissions')->insertGetId([
            'name' => 'manage_students',
            'description' => 'Manage students',
        ]);

        $roleId = DB::table('roles')->insertGetId([
            'name' => 'admin',
            'priority' => 1,
        ]);

        DB::table('user_roles')->insert([
            'user_id' => 5,
            'role_id' => $roleId,
        ]);

        DB::table('role_permissions')->insert([
            'role_id' => $roleId,
            'permission_id' => $permissionId,
            'can_read' => 1,
            'can_manage' => 0,
        ]);

        $service = new UserRoleService();
        $roleIds = $service->getRoleIds(5);

        $this->assertTrue($service->hasPermissionForCrud($roleIds, 'manage_students', 'read'));
        $this->assertFalse($service->hasPermissionForCrud($roleIds, 'manage_students', 'delete'));
    }
}
