<?php

declare(strict_types=1);

namespace ImovelZapAi\Authentication\Application\Services;

use ImovelZapAi\Authentication\Domain\Enums\SystemRole;
use ImovelZapAi\Authentication\Infrastructure\Persistence\Eloquent\RoleModel;
use Spatie\Permission\PermissionRegistrar;

final class RoleProvisioner
{
    public function __construct(
        private readonly PermissionRegistrar $permissionRegistrar,
    ) {}

    public function provisionForTenant(string $tenantId): void
    {
        $this->permissionRegistrar->setPermissionsTeamId($tenantId);

        foreach (SystemRole::cases() as $role) {
            RoleModel::findOrCreate($role->value, 'web');
        }

        $this->permissionRegistrar->forgetCachedPermissions();
    }
}
