<?php

namespace App\Commands;

use App\Models\Permission;
use App\Models\UserTypePermission;
use Database\Seeders\PermissionSeeder;
use Database\Seeders\UserTypePermissionSeeder;
use Illuminate\Console\Command;

class RefreshPermissions extends Command
{
    public function __construct(
        protected PermissionSeeder $permissionSeeder,
        protected UserTypePermissionSeeder $userTypePermissionSeeder,
    ) {
        parent::__construct();
    }

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'permissions:refresh';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Refresh all permissions and user type permissions';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->info('Starting permissions refresh...');

        try {
            // Run the Permission seeder

            $this->info('Running Permission seeder...');

            Permission::truncate();

            $this->permissionSeeder->run();
            $this->info('Permission seeder completed successfully.');

            // Run the UserTypePermission seeder

            $this->info('Running UserTypePermission seeder...');

            UserTypePermission::truncate();

            $this->userTypePermissionSeeder->run();

            $this->info('UserTypePermission seeder completed successfully.');
            $this->info('Permissions refresh completed successfully!');

            return Command::SUCCESS;
        } catch (\Exception $e) {
            $this->error('An error occurred while refreshing permissions:');
            $this->error($e);

            return Command::FAILURE;
        }
    }
}
