<?php

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

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

    private const BUTTONS = [
        'admin/user/registerTrend' => '注册走势',
        'admin/user/registerDomain' => '域名统计',
    ];

    public function up()
    {
        if (Schema::hasTable('users') && !Schema::hasColumn('users', 'register_domain')) {
            Schema::table('users', function (Blueprint $table) {
                $table->string('register_domain', 255)
                    ->default('')
                    ->comment('注册来源域名（Referer 主机）')
                    ->after('register_ip');
            });
        }

        if (!Schema::hasTable('menus')) {
            return;
        }

        $now = date('Y-m-d H:i:s');
        $parent = DB::table('menus')
            ->where('uri', 'user')
            ->where('type', 1)
            ->where('parent_id', 0)
            ->first(['id']);

        // 兼容「会员管理」父菜单
        if (!$parent) {
            $parent = DB::table('menus')
                ->where('title', '会员管理')
                ->where('type', 1)
                ->first(['id']);
        }

        $parentId = $parent ? (int)$parent->id : 0;

        $menu = DB::table('menus')
            ->where('uri', self::MENU_URI)
            ->where('type', 1)
            ->first(['id', 'status']);

        if (!$menu) {
            $menuId = DB::table('menus')->insertGetId([
                'parent_id' => $parentId,
                'title' => '注册/域名统计',
                'icon' => null,
                'uri' => self::MENU_URI,
                'permission_name' => 'user.register_domain',
                'sort' => 4,
                'status' => 1,
                'type' => 1,
                'created_at' => $now,
                'updated_at' => $now,
            ]);
        } else {
            $menuId = (int)$menu->id;
            DB::table('menus')->where('id', $menuId)->update([
                'parent_id' => $parentId,
                'title' => '注册/域名统计',
                'status' => 1,
                'updated_at' => $now,
            ]);
        }

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

    public function down()
    {
        if (Schema::hasTable('users') && Schema::hasColumn('users', 'register_domain')) {
            Schema::table('users', function (Blueprint $table) {
                $table->dropColumn('register_domain');
            });
        }

        if (Schema::hasTable('menus')) {
            DB::table('menus')->whereIn('uri', array_merge([self::MENU_URI], array_keys(self::BUTTONS)))->delete();
        }
    }
};
