<?php

namespace Database\Seeders;

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

class RolePermissionSeeder extends Seeder
{
    public function run(): void
    {
        app()[PermissionRegistrar::class]->forgetCachedPermissions();

        $permissions = [
            'dashboard' => ['view', 'attendance', 'revenue', 'expense', 'orders_channel', 'orders_payment', 'orders_marketing', 'orders_status', 'cash'],
            'analysis' => ['view', 'attendance', 'cash', 'product_stock', 'revenue', 'revenue_trend', 'expense', 'busy_hours', 'profit_orders', 'profit_hpp', 'profit_gross', 'profit_margin', 'top_customers', 'top_products', 'top_suppliers', 'marketing_sales', 'raw_materials'],
            'employees' => ['view', 'create', 'update', 'delete', 'toggle_status', 'reset_password'],
            'attendances' => ['view', 'create', 'delete', 'manage'],
            'leave_requests' => ['view', 'create', 'update', 'delete', 'verify'],
            'categories' => ['view', 'create', 'update', 'delete'],
            'customers' => ['view', 'create', 'update', 'delete'],
            'products' => ['view', 'create', 'update', 'delete', 'toggle_status', 'toggle_featured', 'transfer_stock', 'view_stock_mutations'],
            'stocks' => ['view'],
            'orders' => ['view', 'create', 'update', 'delete', 'send', 'complete', 'cancel'],
            'cuttings' => ['view', 'create', 'update', 'delete', 'complete'],
            'cash' => ['view', 'deposit', 'withdraw', 'update', 'delete'],
            'expenses' => ['view', 'create', 'update', 'delete'],
            'activity_logs' => ['view'],
            'employee_advances' => ['view', 'create', 'update', 'delete', 'pay', 'view_payments', 'verify'],
            'payroll' => ['view', 'pay', 'cancel', 'adjust'],
            'restocks' => ['view', 'create', 'update', 'delete'],
            'raw_materials' => ['view', 'create', 'update', 'delete', 'toggle_status'],
            'suppliers' => ['view', 'create', 'update', 'delete'],
            'purchases' => ['view', 'create', 'update', 'delete'],
            'stok_opnames' => ['view', 'create', 'update', 'delete', 'submit', 'verify'],
            'roles' => ['view', 'create', 'update', 'delete'],
            'settings' => ['view_system', 'update_system', 'view_homepage', 'update_homepage', 'view_social_media', 'update_social_media', 'view_hr', 'update_hr'],
        ];

        foreach ($permissions as $module => $actions) {
            foreach ($actions as $action) {
                Permission::firstOrCreate(['name' => "{$module}.{$action}"]);
            }
        }

        $allPermissions = Permission::all()->pluck('name')->toArray();

        $excludedFromDeveloperOwner = [
            'attendances.create',
            'attendances.delete',
            'stok_opnames.submit',
            'roles.view',
            'roles.create',
            'roles.update',
            'roles.delete',
        ];

        $developerOwnerPerms = array_values(array_filter(
            $allPermissions,
            fn ($p) => ! in_array($p, $excludedFromDeveloperOwner, true)
        ));

        $rolePermissions = [
            'developer' => $developerOwnerPerms,

            'owner' => $developerOwnerPerms,

            'admin-toko' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',
                    'dashboard.attendance',
                    'dashboard.revenue',
                    'dashboard.expense',
                    'dashboard.orders_channel',
                    'dashboard.orders_payment',
                    'dashboard.orders_marketing',
                    'dashboard.orders_status',

                    'analysis.view',
                    'analysis.attendance',
                    'analysis.cash',
                    'analysis.product_stock',
                    'analysis.revenue',
                    'analysis.revenue_trend',
                    'analysis.expense',
                    'analysis.busy_hours',
                    'analysis.profit_orders',
                    'analysis.profit_hpp',
                    'analysis.profit_gross',
                    'analysis.profit_margin',
                    'analysis.top_customers',
                    'analysis.top_products',
                    'analysis.marketing_sales',

                    'stok_opnames.view',

                    'attendances.view',
                    'attendances.create',
                    'attendances.delete',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'restocks.view',
                    'restocks.create',
                    'restocks.update',
                    'restocks.delete',

                    'categories.view',
                    'categories.create',
                    'categories.update',
                    'categories.delete',

                    'customers.view',
                    'customers.create',
                    'customers.update',
                    'customers.delete',

                    'products.view',
                    'products.create',
                    'products.update',
                    'products.delete',
                    'products.toggle_status',
                    'products.toggle_featured',
                    'products.transfer_stock',
                    'products.view_stock_mutations',

                    'orders.view',
                    'orders.create',
                    'orders.update',
                    'orders.delete',
                    'orders.send',
                    'orders.complete',
                    'orders.cancel',

                    'cash.view',
                    'cash.deposit',
                    'cash.withdraw',
                    'cash.update',
                    'cash.delete',

                    'expenses.view',
                    'expenses.create',
                    'expenses.update',
                    'expenses.delete',

                    'activity_logs.view',

                    'employee_advances.view',
                    'employee_advances.create',
                    'employee_advances.update',
                    'employee_advances.delete',
                    'employee_advances.pay',
                    'employee_advances.view_payments',
                    'employee_advances.verify',

                    'payroll.view',
                    'payroll.adjust',
                    'payroll.pay',
                    'payroll.cancel',

                    'settings.view_system',
                    'settings.update_system',
                    'settings.view_social_media',
                    'settings.update_social_media',
                    'settings.view_hr',
                    'settings.update_hr',
                    'settings.view_homepage',
                    'settings.update_homepage',

                    'employees.view',
                    'employees.create',
                    'employees.update',
                    'employees.toggle_status',
                    'employees.delete',
                ], true);
            })),

            'cashier' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',
                    'dashboard.revenue',
                    'dashboard.orders_channel',
                    'dashboard.orders_payment',
                    'dashboard.orders_status',

                    'analysis.view',
                    'analysis.attendance',
                    'analysis.revenue',
                    'analysis.profit_orders',
                    'analysis.top_customers',
                    'analysis.top_products',

                    'attendances.view',
                    'attendances.create',
                    'attendances.delete',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'customers.view',
                    'customers.create',
                    'customers.update',
                    'customers.delete',

                    'products.view',

                    'orders.view',
                    'orders.create',
                    'orders.update',
                    'orders.delete',
                    'orders.send',
                    'orders.complete',
                    'orders.cancel',

                    'employee_advances.view',
                    'employee_advances.create',
                    'employee_advances.update',
                    'employee_advances.delete',
                    'employee_advances.view_payments',

                    'payroll.view',
                ], true);
            })),

            'admin-bahan-baku' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',

                    'employees.view',
                    'employees.create',
                    'employees.update',
                    'employees.toggle_status',
                    'employees.delete',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'suppliers.view',
                    'suppliers.create',
                    'suppliers.update',
                    'suppliers.delete',

                    'raw_materials.view',
                    'raw_materials.create',
                    'raw_materials.update',
                    'raw_materials.toggle_status',

                    'cuttings.view',
                    'cuttings.create',
                    'cuttings.update',
                    'cuttings.delete',
                    'cuttings.complete',

                    'purchases.view',
                    'purchases.create',
                    'purchases.update',
                    'purchases.delete',

                    'activity_logs.view',

                    'payroll.view',
                ], true);
            })),

            'marketing-offline' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',
                    'dashboard.revenue',
                    'dashboard.orders_channel',
                    'dashboard.orders_payment',
                    'dashboard.orders_marketing',
                    'dashboard.orders_status',

                    'analysis.view',
                    'analysis.attendance',
                    'analysis.revenue',
                    'analysis.profit_orders',
                    'analysis.top_products',
                    'analysis.top_customers',

                    'attendances.view',
                    'attendances.create',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'products.view',

                    'orders.view',

                    'employee_advances.view',
                    'employee_advances.create',
                    'employee_advances.update',
                    'employee_advances.delete',
                    'employee_advances.view_payments',

                    'payroll.view',
                ], true);
            })),

            'marketing-online' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',
                    'dashboard.revenue',
                    'dashboard.orders_channel',
                    'dashboard.orders_payment',
                    'dashboard.orders_marketing',
                    'dashboard.orders_status',

                    'analysis.view',
                    'analysis.attendance',
                    'analysis.revenue',
                    'analysis.profit_orders',
                    'analysis.top_products',
                    'analysis.top_customers',

                    'attendances.view',
                    'attendances.create',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'products.view',

                    'orders.view',

                    'employee_advances.view',
                    'employee_advances.create',
                    'employee_advances.update',
                    'employee_advances.delete',
                    'employee_advances.view_payments',

                    'payroll.view',
                ], true);
            })),

            'direktur' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',
                    'dashboard.attendance',
                    'dashboard.revenue',
                    'dashboard.expense',
                    'dashboard.orders_channel',
                    'dashboard.orders_payment',
                    'dashboard.orders_marketing',
                    'dashboard.orders_status',

                    'analysis.view',
                    'analysis.attendance',
                    'analysis.cash',
                    'analysis.product_stock',
                    'analysis.revenue',
                    'analysis.revenue_trend',
                    'analysis.expense',
                    'analysis.busy_hours',
                    'analysis.profit_orders',
                    'analysis.profit_hpp',
                    'analysis.profit_gross',
                    'analysis.profit_margin',
                    'analysis.top_customers',
                    'analysis.top_products',
                    'analysis.marketing_sales',

                    'employees.view',

                    'attendances.view',
                    'attendances.create',
                    'attendances.delete',
                    'attendances.manage',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'categories.view',

                    'customers.view',

                    'products.view',

                    'orders.view',

                    'cash.view',

                    'expenses.view',

                    'activity_logs.view',

                    'employee_advances.view',
                    'employee_advances.create',
                    'employee_advances.update',
                    'employee_advances.delete',
                    'employee_advances.pay',
                    'employee_advances.view_payments',
                    'employee_advances.verify',

                    'payroll.view',
                ], true);
            })),

            'non-operator' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',

                    'analysis.view',
                    'analysis.attendance',

                    'attendances.view',
                    'attendances.create',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'employee_advances.view',
                    'employee_advances.create',
                    'employee_advances.update',
                    'employee_advances.delete',

                    'payroll.view',
                ], true);
            })),

            'stok-opname' => array_values(array_filter($allPermissions, function ($p) {
                return in_array($p, [
                    'dashboard.view',

                    'attendances.view',
                    'attendances.create',

                    'leave_requests.view',
                    'leave_requests.create',
                    'leave_requests.update',
                    'leave_requests.delete',

                    'products.view',

                    'stok_opnames.view',
                    'stok_opnames.create',
                    'stok_opnames.update',
                    'stok_opnames.delete',
                    'stok_opnames.submit',

                    'employee_advances.view',
                    'employee_advances.create',
                    'employee_advances.update',
                    'employee_advances.delete',

                    'payroll.view',
                ], true);
            })),
        ];

        foreach ($rolePermissions as $roleName => $perms) {
            $role = Role::firstOrCreate(['name' => $roleName]);
            $role->syncPermissions($perms);
        }
    }
}
