<?php
// database/seeders/RolePermissionSeeder.php
namespace Database\Seeders;

use App\Models\Role;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Database\Seeder;

class RolePermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Define permissions by category
        $permissions = [
            // File permissions
            ['name' => 'View Files', 'slug' => 'view_files', 'category' => 'files'],
            ['name' => 'Create Files', 'slug' => 'create_files', 'category' => 'files'],
            ['name' => 'Edit Files', 'slug' => 'edit_files', 'category' => 'files'],
            ['name' => 'Delete Files', 'slug' => 'delete_files', 'category' => 'files'],
            ['name' => 'Download Files', 'slug' => 'download_files', 'category' => 'files'],
            
            // Category permissions
            ['name' => 'View Categories', 'slug' => 'view_categories', 'category' => 'categories'],
            ['name' => 'Create Categories', 'slug' => 'create_categories', 'category' => 'categories'],
            ['name' => 'Edit Categories', 'slug' => 'edit_categories', 'category' => 'categories'],
            ['name' => 'Delete Categories', 'slug' => 'delete_categories', 'category' => 'categories'],
            
            // Stage permissions
            ['name' => 'View Stages', 'slug' => 'view_stages', 'category' => 'stages'],
            ['name' => 'Create Stages', 'slug' => 'create_stages', 'category' => 'stages'],
            ['name' => 'Edit Stages', 'slug' => 'edit_stages', 'category' => 'stages'],
            ['name' => 'Delete Stages', 'slug' => 'delete_stages', 'category' => 'stages'],
            ['name' => 'Update File Stages', 'slug' => 'update_file_stages', 'category' => 'stages'],
            
            // Attachment permissions
            ['name' => 'View Attachments', 'slug' => 'view_attachments', 'category' => 'attachments'],
            ['name' => 'Upload Attachments', 'slug' => 'upload_attachments', 'category' => 'attachments'],
            ['name' => 'Delete Attachments', 'slug' => 'delete_attachments', 'category' => 'attachments'],
            
            // Notice permissions
            ['name' => 'View Notices', 'slug' => 'view_notices', 'category' => 'notices'],
            ['name' => 'Create Notices', 'slug' => 'create_notices', 'category' => 'notices'],
            ['name' => 'Edit Notices', 'slug' => 'edit_notices', 'category' => 'notices'],
            ['name' => 'Delete Notices', 'slug' => 'delete_notices', 'category' => 'notices'],
            
            // User permissions
            ['name' => 'View Users', 'slug' => 'view_users', 'category' => 'users'],
            ['name' => 'Create Users', 'slug' => 'create_users', 'category' => 'users'],
            ['name' => 'Edit Users', 'slug' => 'edit_users', 'category' => 'users'],
            ['name' => 'Delete Users', 'slug' => 'delete_users', 'category' => 'users'],
            ['name' => 'Assign Roles', 'slug' => 'assign_roles', 'category' => 'users'],
            
            // Role permissions
            ['name' => 'View Roles', 'slug' => 'view_roles', 'category' => 'roles'],
            ['name' => 'Create Roles', 'slug' => 'create_roles', 'category' => 'roles'],
            ['name' => 'Edit Roles', 'slug' => 'edit_roles', 'category' => 'roles'],
            ['name' => 'Delete Roles', 'slug' => 'delete_roles', 'category' => 'roles'],
            ['name' => 'Assign Permissions', 'slug' => 'assign_permissions', 'category' => 'roles'],
            
            // Dashboard
            ['name' => 'View Dashboard', 'slug' => 'view_dashboard', 'category' => 'dashboard'],
        ];

        // Create permissions
        foreach ($permissions as $perm) {
            Permission::updateOrCreate(
                ['slug' => $perm['slug']],
                [
                    'name' => $perm['name'],
                    'category' => $perm['category'],
                    'active' => true,
                    'created_by' => 1,
                    'updated_by' => 1,
                ]
            );
        }

        // Define roles with their permissions
        $roles = [
            'super-admin' => [
                'name' => 'Super Administrator',
                'description' => 'Full system access',
                'is_default' => false,
                'permissions' => Permission::all()->pluck('slug')->toArray(),
            ],
            'admin' => [
                'name' => 'Administrator',
                'description' => 'Administrative access',
                'is_default' => false,
                'permissions' => [
                    'view_dashboard',
                    'view_files', 'create_files', 'edit_files', 'delete_files', 'download_files',
                    'view_categories', 'create_categories', 'edit_categories', 'delete_categories',
                    'view_stages', 'create_stages', 'edit_stages', 'delete_stages', 'update_file_stages',
                    'view_attachments', 'upload_attachments', 'delete_attachments',
                    'view_notices', 'create_notices', 'edit_notices', 'delete_notices',
                    'view_users', 'create_users', 'edit_users', 'assign_roles',
                ],
            ],
            'editor' => [
                'name' => 'Editor',
                'description' => 'Can create and edit content',
                'is_default' => false,
                'permissions' => [
                    'view_dashboard',
                    'view_files', 'create_files', 'edit_files', 'download_files',
                    'view_categories',
                    'view_stages', 'update_file_stages',
                    'view_attachments', 'upload_attachments',
                    'view_notices', 'create_notices', 'edit_notices',
                ],
            ],
            'viewer' => [
                'name' => 'Viewer',
                'description' => 'Read-only access',
                'is_default' => true,
                'permissions' => [
                    'view_dashboard',
                    'view_files', 'download_files',
                    'view_categories',
                    'view_stages',
                    'view_attachments',
                    'view_notices',
                ],
            ],
        ];

        // Create roles and assign permissions
        foreach ($roles as $slug => $roleData) {
            $role = Role::updateOrCreate(
                ['slug' => $slug],
                [
                    'name' => $roleData['name'],
                    'description' => $roleData['description'],
                    'is_default' => $roleData['is_default'],
                    'active' => true,
                    'created_by' => 1,
                    'updated_by' => 1,
                ]
            );
            
            // Assign permissions
            $permissions = Permission::whereIn('slug', $roleData['permissions'])->get();
            $role->syncPermissions($permissions);
        }

        // Assign super-admin role to the first user (user ID 1)
        $user = User::find(1);
        if ($user) {
            $superAdminRole = Role::where('slug', 'super-admin')->first();
            $user->assignRole($superAdminRole);
        }
    }
}