<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

/**
 * MediaPermissionSeeder
 *
 * Seeds all permissions for the Media & Pictorial Gallery Module.
 * Permissions are assigned to Super Manager, Manager, and Trainer roles.
 *
 * Permission Naming Convention: {action} {resource}
 * - view: Read-only access
 * - create: Ability to create new records
 * - edit: Ability to modify existing records
 * - delete: Ability to remove records
 * - upload: Ability to upload images/files
 * - manage: Full management access
 */
class MediaPermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Define all media module permissions
        $permissions = [
            // Gallery Type Permissions
            'view gallery types' => 'View gallery type categories and list',
            'create gallery types' => 'Create new gallery type categories',
            'edit gallery types' => 'Edit existing gallery type details',
            'delete gallery types' => 'Delete gallery type categories',

            // Gallery Permissions
            'view galleries' => 'View gallery collections and details',
            'create galleries' => 'Create new gallery collections',
            'edit galleries' => 'Edit existing gallery details and settings',
            'delete galleries' => 'Delete gallery collections and all associated images',

            // Gallery Image Permissions
            'view gallery images' => 'View images within galleries',
            'upload gallery images' => 'Upload images to galleries',
            'edit gallery images' => 'Edit image metadata (title, description)',
            'delete gallery images' => 'Delete individual images from galleries',
        ];

        // Create all permissions
        foreach ($permissions as $name => $description) {
            Permission::firstOrCreate(
                ['name' => $name],
                ['guard_name' => 'web']
            );
        }

        $this->command->info('Media module permissions created successfully.');

        // Get roles
        $superManager = Role::where('name', 'Super Manager')->first();
        $manager = Role::where('name', 'Manager')->first();
        $trainer = Role::where('name', 'Trainer')->first();

        if (!$superManager) {
            $this->command->warn('Super Manager role not found. Please run RolePermissionSeeder first.');
            return;
        }

        // Assign ALL permissions to Super Manager
        $superManager->givePermissionTo(array_keys($permissions));
        $this->command->info('All media permissions assigned to Super Manager.');

        // Assign permissions to Manager (full gallery management)
        if ($manager) {
            $managerPermissions = [
                'view gallery types',
                'create gallery types',
                'edit gallery types',
                'delete gallery types',
                'view galleries',
                'create galleries',
                'edit galleries',
                'delete galleries',
                'view gallery images',
                'upload gallery images',
                'edit gallery images',
                'delete gallery images',
            ];
            $manager->givePermissionTo($managerPermissions);
            $this->command->info('Gallery management permissions assigned to Manager.');
        }

        // Assign permissions to Trainer (can upload and manage gallery content)
        if ($trainer) {
            $trainerPermissions = [
                'view gallery types',
                'view galleries',
                'upload gallery images',
                'edit gallery images',
            ];
            $trainer->givePermissionTo($trainerPermissions);
            $this->command->info('Gallery content management permissions assigned to Trainer.');
        }

        $this->command->info('Media Permission Seeder completed successfully!');
    }
}
