<?php

namespace App\Services\Auth;

use Illuminate\Support\Facades\DB;

class PermissionCheckService
{
    public function hasPermission(int $userId, string $permissionName): bool
    {
        $permissionId = DB::table('permissions')
            ->where('name', $permissionName)
            ->value('id');

        if (!$permissionId) {
            return false;
        }

        return DB::table('user_roles')
            ->join('role_permissions', 'role_permissions.role_id', '=', 'user_roles.role_id')
            ->where('user_roles.user_id', $userId)
            ->whereNull('user_roles.deleted_at')
            ->where('role_permissions.permission_id', $permissionId)
            ->exists();
    }
}
