<?php

namespace App\Console\Commands;

use App\Addons\Services\AddonLoader;
use App\Core\Services\PermissionService;
use App\Core\Services\PluginService;
use Illuminate\Console\Command;

class SyncRolePermissions extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'roles:sync-permissions';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sync permissions and assign default permissions to roles based on SaaS plugin status';

    /**
     * Execute the console command.
     */
    public function handle(): int
    {
        $this->info('Syncing permissions and roles...');

        $permissionService = new PermissionService(new PluginService(new AddonLoader));

        // Sync all permissions
        $this->info('Syncing permissions to database...');
        $permissionService->syncPermissions();
        $this->info('✓ Permissions synced');

        // Assign default permissions
        $this->info('Assigning default permissions based on SaaS plugin status...');
        $permissionService->assignDefaultPermissions();
        $this->info('✓ Default permissions assigned');

        $this->info('');
        $this->info('✅ Role permissions synchronized successfully!');

        return Command::SUCCESS;
    }
}
