<?php

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Migrations\Migration;
use App\Support\AdminAuthorizationRegistry;

return new class extends Migration
{
    public function up(): void
    {
        $now = Carbon::now();
        $playersMenuId = (int) DB::table('admin_menus')->where('code', 'service.players')->value('id');
        $updateActionId = (int) DB::table('admin_action_catalog')->where('code', 'update')->value('id');

        if ($playersMenuId > 0 && $updateActionId > 0) {
            DB::table('admin_menu_actions')->updateOrInsert(
                ['permission_code' => 'service.players.freeze'],
                [
                    'menu_id' => $playersMenuId,
                    'action_id' => $updateActionId,
                    'name' => '冻结解冻玩家',
                    'status' => 1,
                    'created_at' => $now,
                    'updated_at' => $now,
                ],
            );
        }

        $menuActionIds = DB::table('admin_menu_actions')->pluck('id', 'permission_code');

        $playerResourceBindings = [
            'admin.players.index' => ['service.players.manage', 'service.players.view'],
            'admin.players.store' => ['service.players.manage'],
            'admin.players.show' => ['service.players.manage', 'service.players.view'],
            'admin.players.update' => ['service.players.manage'],
            'admin.players.destroy' => ['service.players.manage'],
            'admin.players.freeze' => ['service.players.freeze'],
            'admin.players.unfreeze' => ['service.players.freeze'],
            'admin.players.wallets' => ['service.players.manage', 'service.wallet.view'],
            'admin.players.ticket-items' => ['service.players.manage', 'service.tickets.view'],
        ];

        foreach (AdminAuthorizationRegistry::resources() as $resource) {
            if (($resource['module_code'] ?? null) !== 'player_service') {
                continue;
            }

            $resourceId = DB::table('admin_api_resources')
                ->where('code', $resource['code'])
                ->value('id');

            if ($resourceId === null) {
                continue;
            }

            DB::table('admin_api_resource_bindings')
                ->where('api_resource_id', (int) $resourceId)
                ->delete();

            $permissionCodes = $playerResourceBindings[$resource['code']] ?? $resource['permission_codes'];
            foreach ($permissionCodes as $permissionCode) {
                $menuActionId = $menuActionIds[$permissionCode] ?? null;
                if ($menuActionId === null) {
                    continue;
                }

                DB::table('admin_api_resource_bindings')->insert([
                    'api_resource_id' => (int) $resourceId,
                    'menu_action_id' => (int) $menuActionId,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
            }
        }
    }

    public function down(): void
    {
        // 不回滚授权绑定，避免误删线上已调整的资源权限关系。
    }
};
