<?php

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

return new class extends Migration {
    private const MENU_URI = 'egame';

    private const MENU_PERMISSION_NAME = 'egame.catalog.menu';

    private const BUTTONS = [
        'admin/egame/options' => '筛选选项',
        'admin/egame/items' => '配置列表',
        'admin/egame/update' => '保存配置',
        'admin/egame/setStatus' => '切换状态',
        'admin/egame/delete' => '删除配置',
    ];

    private const BUTTON_PERMISSION_NAMES = [
        'admin/egame/options' => 'egame.catalog.options',
        'admin/egame/items' => 'egame.catalog.items',
        'admin/egame/update' => 'egame.catalog.update',
        'admin/egame/setStatus' => 'egame.catalog.status',
        'admin/egame/delete' => 'egame.catalog.delete',
    ];

    public function up(): void
    {
        if (!Schema::hasTable('menus')) {
            return;
        }

        $now = date('Y-m-d H:i:s');
        $menu = DB::table('menus')
            ->where('uri', self::MENU_URI)
            ->where('type', 1)
            ->first(['id', 'status']);

        if (!$menu) {
            $menuId = DB::table('menus')->insertGetId([
                'parent_id' => 0,
                'title' => '第三方游戏',
                'icon' => null,
                'uri' => self::MENU_URI,
                'permission_name' => self::MENU_PERMISSION_NAME,
                'sort' => 0,
                'status' => 1,
                'type' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        } else {
            $menuId = (int)$menu->id;
            if ((int)$menu->status !== 1) {
                DB::table('menus')->where('id', $menuId)->update([
                    'status' => 1,
                    'updated_at' => $now,
                ]);
            }
        }

        $sort = 0;
        foreach (self::BUTTONS as $uri => $title) {
            $button = DB::table('menus')
                ->where('uri', $uri)
                ->first(['id', 'parent_id', 'title', 'status', 'type']);
            if ($button) {
                $updates = [];
                if ((int)$button->parent_id !== $menuId) {
                    $updates['parent_id'] = $menuId;
                }
                if ((int)$button->type !== 2) {
                    $updates['type'] = 2;
                }
                if (trim((string)$button->title) === '') {
                    $updates['title'] = $title;
                }
                if ((int)$button->status !== 1) {
                    $updates['status'] = 1;
                }
                if ($updates !== []) {
                    $updates['updated_at'] = $now;
                    DB::table('menus')->where('id', $button->id)->update($updates);
                }
            } else {
                DB::table('menus')->insert([
                    'parent_id' => $menuId,
                    'title' => $title,
                    'icon' => null,
                    'uri' => $uri,
                    'permission_name' => self::BUTTON_PERMISSION_NAMES[$uri],
                    'sort' => $sort,
                    'status' => 1,
                    'type' => 2,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
            }
            $sort++;
        }
    }

    public function down(): void
    {
        // This is an irreversible menu and permission data migration. Existing
        // rows may have been reused, reparented, and re-enabled, so their prior
        // parent and status state cannot be reconstructed safely on rollback.
    }
};
