<?php

namespace Database\Seeders;

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

class PermissionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 重置快取
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();

        // 建立權限
        $permissions = [
            // 產品管理
            'products.view' => '檢視',
            'products.create' => '建立',
            'products.edit' => '編輯',
            'products.delete' => '刪除',

            // 採購單管理
            'purchase_orders.view' => '檢視',
            'purchase_orders.create' => '建立',
            'purchase_orders.edit' => '編輯',
            'purchase_orders.delete' => '刪除',
            'purchase_orders.approve' => '核准',
            'purchase_orders.cancel' => '作廢',

            // 庫存管理
            'inventory.view' => '檢視',
            'inventory.view_cost' => '檢視成本',
            'inventory.delete' => '刪除',

            // 庫存盤點 (Stock Counting)
            'inventory_count.view' => '檢視',
            'inventory_count.create' => '建立',
            'inventory_count.edit' => '編輯',
            'inventory_count.delete' => '刪除',

            // 庫存調整 (Stock Adjustment)
            'inventory_adjust.view' => '檢視',
            'inventory_adjust.create' => '建立',
            'inventory_adjust.edit' => '編輯',
            'inventory_adjust.delete' => '刪除',

            // 庫存調撥 (Stock Transfer)
            'inventory_transfer.view' => '檢視',
            'inventory_transfer.create' => '建立',
            'inventory_transfer.edit' => '編輯',
            'inventory_transfer.delete' => '刪除',

            // 庫存報表
            'inventory_report.view' => '檢視',
            'inventory_report.export' => '匯出',

            // 進貨單管理
            'goods_receipts.view' => '檢視',
            'goods_receipts.create' => '建立',
            'goods_receipts.edit' => '編輯',
            'goods_receipts.delete' => '刪除',

            // 出貨單管理 (Delivery Notes / Shipping Orders)
            'delivery_notes.view' => '檢視',
            'delivery_notes.create' => '建立',
            'delivery_notes.edit' => '編輯',
            'delivery_notes.delete' => '刪除',

            // 生產工單管理
            'production_orders.view' => '檢視',
            'production_orders.create' => '建立',
            'production_orders.edit' => '編輯',
            'production_orders.delete' => '刪除',
            'production_orders.approve' => '核准',
            'production_orders.cancel' => '作廢',

            // 配方管理
            'recipes.view' => '檢視',
            'recipes.create' => '建立',
            'recipes.edit' => '編輯',
            'recipes.delete' => '刪除',

            // 供應商管理
            'vendors.view' => '檢視',
            'vendors.create' => '建立',
            'vendors.edit' => '編輯',
            'vendors.delete' => '刪除',

            // 倉庫管理
            'warehouses.view' => '檢視',
            'warehouses.create' => '建立',
            'warehouses.edit' => '編輯',
            'warehouses.delete' => '刪除',

            // 使用者管理
            'users.view' => '檢視',
            'users.create' => '建立',
            'users.edit' => '編輯',
            'users.delete' => '刪除',
            'users.activate' => '狀態管理',

            // 角色權限管理
            'roles.view' => '檢視',
            'roles.create' => '建立',
            'roles.edit' => '編輯',
            'roles.delete' => '刪除',

            // 系統日誌
            'system.view_logs' => '檢視日誌',

            // 公共事業費管理
            'utility_fees.view' => '檢視',
            'utility_fees.create' => '建立',
            'utility_fees.edit' => '編輯',
            'utility_fees.delete' => '刪除',

            // 會計報表
            'accounting.view' => '檢視',
            'accounting.export' => '匯出',

            // 銷售匯入管理
            'sales_imports.view' => '檢視',
            'sales_imports.create' => '建立',
            'sales_imports.confirm' => '確認',
            'sales_imports.delete' => '刪除',
        ];

        foreach ($permissions as $name => $displayName) {
            Permission::updateOrCreate(
                ['name' => $name],
                ['display_name' => $displayName]
            );
        }

        // 建立角色
        $superAdmin = Role::firstOrCreate(['name' => 'super-admin'], ['display_name' => '系統管理員']);
        $admin = Role::firstOrCreate(['name' => 'admin'], ['display_name' => '一般管理員']);
        $warehouseManager = Role::firstOrCreate(['name' => 'warehouse-manager'], ['display_name' => '倉庫管理員']);
        $purchaser = Role::firstOrCreate(['name' => 'purchaser'], ['display_name' => '採購人員']);
        $viewer = Role::firstOrCreate(['name' => 'viewer'], ['display_name' => '檢視人員']);


        // 給角色分配權限
        // super-admin 擁有所有權限
        $superAdmin->givePermissionTo(Permission::all());

        // admin 擁有大部分權限（除了角色管理）
        $admin->givePermissionTo([
            'products.view', 'products.create', 'products.edit', 'products.delete',
            'purchase_orders.view', 'purchase_orders.create', 'purchase_orders.edit', 
            'purchase_orders.delete', 'purchase_orders.approve', 'purchase_orders.cancel',
            'inventory.view', 'inventory.view_cost', 'inventory.delete',
            'inventory_count.view', 'inventory_count.create', 'inventory_count.edit', 'inventory_count.delete',
            'inventory_adjust.view', 'inventory_adjust.create', 'inventory_adjust.edit', 'inventory_adjust.delete',
            'inventory_transfer.view', 'inventory_transfer.create', 'inventory_transfer.edit', 'inventory_transfer.delete',
            'inventory_report.view', 'inventory_report.export',
            'goods_receipts.view', 'goods_receipts.create', 'goods_receipts.edit', 'goods_receipts.delete',
            'delivery_notes.view', 'delivery_notes.create', 'delivery_notes.edit', 'delivery_notes.delete',
            'production_orders.view', 'production_orders.create', 'production_orders.edit', 'production_orders.delete',
            'recipes.view', 'recipes.create', 'recipes.edit', 'recipes.delete',
            'vendors.view', 'vendors.create', 'vendors.edit', 'vendors.delete',
            'warehouses.view', 'warehouses.create', 'warehouses.edit', 'warehouses.delete',
            'users.view', 'users.create', 'users.edit',
            'users.view', 'users.create', 'users.edit',
            'system.view_logs',
            'utility_fees.view', 'utility_fees.create', 'utility_fees.edit', 'utility_fees.delete',
            'accounting.view', 'accounting.export',
            'sales_imports.view', 'sales_imports.create', 'sales_imports.confirm', 'sales_imports.delete',
        ]);

        // warehouse-manager 管理庫存與倉庫
        $warehouseManager->givePermissionTo([
            'products.view',
            'inventory.view', 'inventory.delete',
            'inventory_count.view', 'inventory_count.create', 'inventory_count.edit', 'inventory_count.delete',
            'inventory_adjust.view', 'inventory_adjust.create', 'inventory_adjust.edit', 'inventory_adjust.delete',
            'inventory_transfer.view', 'inventory_transfer.create', 'inventory_transfer.edit', 'inventory_transfer.delete',
            'inventory_report.view', 'inventory_report.export',
            'goods_receipts.view', 'goods_receipts.create', 'goods_receipts.edit', 'goods_receipts.delete',
            'goods_receipts.view', 'goods_receipts.create', 'goods_receipts.edit', 'goods_receipts.delete',
            'production_orders.view', 'production_orders.create', 'production_orders.edit',
            'warehouses.view', 'warehouses.create', 'warehouses.edit',
        ]);

        // purchaser 管理採購與供應商
        $purchaser->givePermissionTo([
            'products.view',
            'purchase_orders.view', 'purchase_orders.create', 'purchase_orders.edit',
            'vendors.view', 'vendors.create', 'vendors.edit',
            'inventory.view',
            'goods_receipts.view', 'goods_receipts.create',
        ]);

        // viewer 僅能查看
        $viewer->givePermissionTo([
            'products.view',
            'purchase_orders.view',
            'inventory.view',
            'goods_receipts.view',
            'vendors.view',
            'warehouses.view',
            'utility_fees.view',
            'inventory_report.view',
            'accounting.view',
        ]);

        // 將現有使用者設為 super-admin（如果存在的話）
        $firstUser = User::first();
        if ($firstUser) {
            $firstUser->assignRole('super-admin');
            $this->command->info("已將使用者 {$firstUser->name} 設為 super-admin");
        }
    }
}
