<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 刪除 purchase_orders.publish 權限
        \Spatie\Permission\Models\Permission::where('name', 'purchase_orders.publish')->delete();
        
        // 重置權限快取
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // 恢復權限（如果需要回滾）
        \Spatie\Permission\Models\Permission::firstOrCreate(['name' => 'purchase_orders.publish']);
        
        // 重新分配給 admin (簡單恢復，可能無法完全還原所有角色配置)
        $admin = \Spatie\Permission\Models\Role::where('name', 'admin')->first();
        if ($admin) {
            $admin->givePermissionTo('purchase_orders.publish');
        }
        
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
    }
};
