<?php

namespace Database\Seeders;

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

class RolePermissionSeeder extends Seeder
{
    public function run(): void
    {
        // Reset cached roles and permissions
        app(PermissionRegistrar::class)->forgetCachedPermissions();

        // 1. Create Roles
        $roles = [
            User::ROLE_DEVELOPER,
            User::ROLE_OWNER,
            User::ROLE_SPV,
            User::ROLE_DESIGNER,
            User::ROLE_KASIR,
            User::ROLE_OPERATOR,
        ];

        foreach ($roles as $roleName) {
            Role::firstOrCreate(['name' => $roleName, 'guard_name' => 'web']);
        }

        // 2. Create Permissions
        $permissions = [
            // Core Permissions
            'manage users',
            'create orders',
            'view orders',
            'process payments',
            'update production',
            'view reports',
            'view click reports',
            'print spk',
            'management access',

            // AI Permissions
            'view ai',

            // Customer Permissions
            'view customers',
            'create customers',
            'edit customers',
            'delete customers',

            // Product Permissions
            'view products',
            'create products',
            'edit products',
            'delete products',

            // Unit Permissions
            'view units',
            'manage units',

            // Supplier Permissions
            'view suppliers',
            'manage suppliers',

            // Finance Permissions
            'view finance',
            'manage deposits',

            // Stock/Material Permissions
            'view stock',
            'manage stock',
            'view materials',
            'manage materials',

            // Machine Permissions
            'view machines',
            'manage machines',

            // Expense Permissions
            'view expenses',
            'manage expenses',

            // Employee Permissions
            'view employees',
            'create employees',
            'edit employees',
            'delete employees',

            // Salary Permissions
            'view salaries',
            'create salaries',
            'edit salaries',
            'delete salaries',

            // Backup Permissions
            'manage backups',

            // Payment Account Permissions
            'manage payment accounts',

            // Settings Permissions
            'manage settings',

            // Customer Management Permissions
            'manage customers',

            // Cancellation Permissions
            'request cancellations',
            'approve cancellations',
        ];

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

        // 3. Assign Permissions to Roles
        $rolePermissions = [
            User::ROLE_DEVELOPER => $permissions, // All permissions

            User::ROLE_OWNER => [
                'view reports', 'view click reports',
                'view orders',
                'view customers', 'create customers', 'edit customers', 'delete customers',
                'manage customers',
                'view products', 'view units', 'view suppliers',
                'view finance', 'manage deposits',
                'view stock', 'view materials',
                'view machines',
                'view expenses', 'manage expenses',
                'view employees', 'view salaries',
                'manage payment accounts',
                'manage settings',
                'view ai',
                'request cancellations', 'approve cancellations',
            ],

            User::ROLE_SPV => [
                'create orders', 'print spk',
                'view orders',
                'process payments',
                'update production',
                'view reports', 'view click reports',
                'view customers', 'create customers', 'edit customers',
                'view products',
                'view stock', 'view materials',
                'view machines',
                'view expenses',
                'view ai',
                'request cancellations', 'approve cancellations',
            ],

            User::ROLE_DESIGNER => [
                'create orders', 'view orders', 'print spk',
                'view customers', 'create customers',
                'view products',
                'view ai',
                'request cancellations',
            ],

            User::ROLE_KASIR => [
                'process payments',
                'view orders',
                'view customers', 'create customers', 'edit customers',
                'view products',
                'view expenses',
                'view ai',
                'request cancellations',
            ],

            User::ROLE_OPERATOR => [
                'update production',
                'view customers',
                'view products',
                'view stock', 'view materials',
                'view machines',
                'view ai',
                'request cancellations',
            ],
        ];

        foreach ($rolePermissions as $roleName => $perms) {
            $role = Role::findByName($roleName, 'web');
            $role->syncPermissions($perms);
        }

        // 4. Sync Roles to All Existing Users based on 'role' column
        $users = User::all();
        foreach ($users as $user) {
            if ($user->role) {
                $user->syncRoles($user->role);
            }
        }

        // Clear cache again after changes
        app(PermissionRegistrar::class)->forgetCachedPermissions();
    }
}