<?php

namespace Database\Seeders;

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

class PermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // Create permissions
        $permissions = [
            // User permissions
            'view_users',
            'create_users',
            'edit_users',
            'delete_users',
            
            // Vendor permissions
            'view_vendors',
            'create_vendors',
            'edit_vendors',
            'delete_vendors',
            'approve_vendors',
            
            // Product permissions
            'view_products',
            'create_products',
            'edit_products',
            'delete_products',
            
            // Order permissions
            'view_orders',
            'create_orders',
            'edit_orders',
            'delete_orders',
            
            // Category permissions
            'view_categories',
            'create_categories',
            'edit_categories',
            'delete_categories',
            
            // Brand permissions
            'view_brands',
            'create_brands',
            'edit_brands',
            'delete_brands',
            
            // Review permissions
            'view_reviews',
            'create_reviews',
            'edit_reviews',
            'delete_reviews',
            
            // Coupon permissions
            'view_coupons',
            'create_coupons',
            'edit_coupons',
            'delete_coupons',
            
            // Role permissions
            'view_roles',
            'create_roles',
            'edit_roles',
            'delete_roles',
            
            // Currency permissions
            'view_currencies',
            'create_currencies',
            'edit_currencies',
            'delete_currencies',
            
            // Conversation permissions
            'view_conversations',
            'create_conversations',
            'edit_conversations',
            'delete_conversations',
            
            // Dashboard permissions
            'view_dashboard',
            'view_reports',
            
            // Settings permissions
            'view_settings',
            'edit_settings',
        ];

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

        // Get roles
        $adminRole = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']);
        $vendorRole = Role::firstOrCreate(['name' => 'vendor', 'guard_name' => 'web']);
        $customerRole = Role::firstOrCreate(['name' => 'customer', 'guard_name' => 'web']);

        // Assign all permissions to admin
        $adminRole->syncPermissions(Permission::all());

        // Assign vendor permissions
        $vendorRole->syncPermissions([
            'view_products',
            'create_products',
            'edit_products',
            'delete_products',
            'view_orders',
            'edit_orders',
            'view_dashboard',
        ]);

        // Assign customer permissions
        $customerRole->syncPermissions([
            'view_products',
            'create_orders',
            'view_orders',
            'create_reviews',
            'view_reviews',
        ]);

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