<?php

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        $now = Carbon::now();

        if (! DB::table('admin_action_catalog')->where('code', 'manual_burst')->exists()) {
            DB::table('admin_action_catalog')->insert([
                'code' => 'manual_burst',
                'name' => '手动爆池',
                'sort_order' => 85,
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        }

        $jackpotMenuId = (int) DB::table('admin_menus')->where('code', 'config.jackpot')->value('id');
        $manualBurstActionId = (int) DB::table('admin_action_catalog')->where('code', 'manual_burst')->value('id');

        if ($jackpotMenuId <= 0 || $manualBurstActionId <= 0) {
            return;
        }

        DB::table('admin_menu_actions')->updateOrInsert(
            ['permission_code' => 'jackpot.pool.manual_burst'],
            [
                'menu_id' => $jackpotMenuId,
                'action_id' => $manualBurstActionId,
                'name' => 'Jackpot 手动爆池',
                'status' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ],
        );

        if (Schema::hasTable('admin_permissions')) {
            DB::table('admin_permissions')->updateOrInsert(
                ['slug' => 'prd.jackpot.manual_burst'],
                [
                    'name' => 'Jackpot 手动爆池·仅超管',
                    'created_at' => $now,
                    'updated_at' => $now,
                ],
            );
        }

        $menuActionId = (int) DB::table('admin_menu_actions')
            ->where('permission_code', 'jackpot.pool.manual_burst')
            ->value('id');

        $superRoleId = (int) DB::table('admin_roles')->where('slug', 'super_admin')->value('id');
        if ($superRoleId > 0 && $menuActionId > 0) {
            if (Schema::hasTable('admin_role_legacy_permissions')) {
                DB::table('admin_role_legacy_permissions')->updateOrInsert(
                    [
                        'role_id' => $superRoleId,
                        'permission_slug' => 'prd.jackpot.manual_burst',
                    ],
                    [
                        'created_at' => $now,
                        'updated_at' => $now,
                    ],
                );
            }

            DB::table('admin_role_menu_actions')->updateOrInsert(
                [
                    'role_id' => $superRoleId,
                    'menu_action_id' => $menuActionId,
                ],
                [],
            );
        }

        $resourceId = DB::table('admin_api_resources')
            ->where('code', 'admin.jackpot.pools.manual-burst')
            ->value('id');

        if ($resourceId !== null && $menuActionId > 0) {
            DB::table('admin_api_resource_bindings')
                ->where('api_resource_id', (int) $resourceId)
                ->delete();

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

            if ($superRoleId > 0 && Schema::hasTable('admin_role_api_resources')) {
                DB::table('admin_role_api_resources')->updateOrInsert([
                    'role_id' => $superRoleId,
                    'api_resource_id' => (int) $resourceId,
                ], []);
            }
        }
    }

    public function down(): void
    {
        // 避免误删线上已调整的授权绑定。
    }
};
