<?php

namespace App\Services\Showroom;

use App\Models\Admin;

class ShowroomPermissionService
{
    public function abilitiesFor(Admin $admin): array
    {
        $modules = $admin->role?->modules ? (array) json_decode($admin->role->modules, true) : [];

        if ((int) $admin->role_id === 1) {
            return ['*'];
        }

        return array_values(array_filter($modules));
    }

    public function can(Admin $admin, string|array $ability): bool
    {
        if ((int) $admin->role_id === 1) {
            return true;
        }

        $abilities = $this->abilitiesFor($admin);
        $required = (array) $ability;

        return collect($required)->contains(fn ($item) => in_array($item, $abilities, true));
    }
}
