<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class PermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Define all permissions for your application
        $permissions = [
            // Dashboard permissions
            'access dashboard',
            
            // User management permissions
            'view users',
            'create users',
            'edit users',
            'delete users',
            'reset user password',
            'assign user roles',
            
            // Role management permissions
            'view roles',
            'create roles',
            'edit roles',
            'delete roles',
            'assign permissions',
            
            // Folder management permissions
            'view folders',
            'create folders',
            'edit folders',
            'delete folders',
            'share folders',
            'manage folder permissions',
            'access shared folders',
            'view folder contents',
            
            // File management permissions
            'upload files',
            'download files',
            'view file metadata',
            'search files',
            'delete files',
            
            // Profile management permissions
            'edit profile',
            'delete profile',
            
            // Administrative permissions
            'access admin panel',
            'manage system settings',
            'view system logs',
            'backup system',
            'restore system',
            
            // Workflow permissions (for future use)
            'view workflows',
            'create workflows',
            'edit workflows',
            'delete workflows',
            'execute workflows',
            
            // Report permissions (for future use)
            'view reports',
            'create reports',
            'export reports',
        ];

        // Create permissions
        foreach ($permissions as $permission) {
            Permission::firstOrCreate(['name' => $permission]);
        }

        // Create default roles
        $adminRole = Role::firstOrCreate(['name' => 'Super Admin']);
        $managerRole = Role::firstOrCreate(['name' => 'Admin']);
        $userRole = Role::firstOrCreate(['name' => 'User']);
        
        // Assign all permissions to Super Admin
        $adminRole->syncPermissions(Permission::all());
        
        // Assign manager permissions
        $managerPermissionNames = [
            'access dashboard',
            'view users',
            'create users',
            'edit users',
            'view roles',
            'view folders',
            'create folders',
            'edit folders',
            'delete folders',
            'share folders',
            'manage folder permissions',
            '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',
        ];
        $managerPermissions = Permission::whereIn('name', $managerPermissionNames)->get();
        $managerRole->syncPermissions($managerPermissions);
        
        // Assign basic user permissions
        $basicUserPermissionNames = [
            'access dashboard',
            'view folders',
            'access shared folders',
            'view folder contents',
            'upload files',
            'download files',
            'view file metadata',
            'search files',
            'edit profile',
            'view workflows',
            'view reports',
        ];
        $basicUserPermissions = Permission::whereIn('name', $basicUserPermissionNames)->get();
        $userRole->syncPermissions($basicUserPermissions);

        $this->command->info('Permissions and roles seeded successfully!');
    }
}