<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class RemoveDuplicatePermissions extends Command
{
    protected $signature = 'admin:remove-duplicates';
    protected $description = 'Remove all lowercase duplicate permissions';

    public function handle()
    {
        $this->info('Removing duplicate lowercase permissions...');
        
        // Define permissions to delete (lowercase/incorrect versions)
        $permissionsToDelete = [
            'create users',
            'edit users', 
            'delete users',
            'reset user password',
            'assign user roles',
            'create roles',
            'edit roles',
            'delete roles', 
            'assign permissions',
            'view folders',
            'create folders',
            'edit folders',
            'delete folders',
            'share folders'
        ];
        
        foreach ($permissionsToDelete as $permName) {
            $permission = Permission::where('name', $permName)->first();
            if ($permission) {
                // Remove from all roles first
                $permission->roles()->detach();
                // Delete the permission
                $permission->delete();
                $this->line("❌ Deleted: {$permName}");
            }
        }
        
        // Update role permissions with correct names
        $this->info('Updating role permissions with correct names...');
        
        $superAdmin = Role::where('name', 'Super Admin')->first();
        if ($superAdmin) {
            $superAdmin->syncPermissions(Permission::all());
            $this->line("✅ Super Admin: All permissions assigned");
        }
        
        $manager = Role::where('name', 'Manager')->first();
        if ($manager) {
            $managerPermissions = [
                'Access Dashboard', 'View Users', 'Create User', 'Edit User',
                'View Roles', 'View Folder', 'Create Folder', 'Edit Folder',
                'Share Folder', 'Access Shared Folders', 'View Folder Contents',
                'Upload Files', 'Download Files', 'View File Metadata', 'Search Files',
                'Edit Profile', 'View Workflows', 'Create Workflows', 'Edit Workflows',
                'View Reports', 'Create Reports'
            ];
            $permissions = Permission::whereIn('name', $managerPermissions)->get();
            $manager->syncPermissions($permissions);
            $this->line("✅ Manager: {$permissions->count()} permissions assigned");
        }
        
        $user = Role::where('name', 'User')->first();
        if ($user) {
            $userPermissions = [
                'Access Dashboard', 'Access Shared Folders', 'View Folder Contents',
                'Upload Files', 'Download Files', 'View File Metadata', 'Search Files',
                'Edit Profile', 'View Workflows', 'View Reports'
            ];
            $permissions = Permission::whereIn('name', $userPermissions)->get();
            $user->syncPermissions($permissions);
            $this->line("✅ User: {$permissions->count()} permissions assigned");
        }
        
        $finalCount = Permission::count();
        $this->info("✅ Cleanup complete! Final permission count: {$finalCount}");
        
        return Command::SUCCESS;
    }
}