<?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 = 'onlineSummary';

    private const BUTTONS = [
        'admin/online/users' => '在线用户',
        'admin/online/newUserSummary' => '新用户汇总',
    ];

    public function up()
    {
        if (Schema::hasTable('user_login')) {
            Schema::table('user_login', function (Blueprint $table) {
                if (!Schema::hasColumn('user_login', 'login_domain')) {
                    $table->string('login_domain', 255)->default('')->comment('登录网站（Referer 主机）')->after('login_ip');
                }
                if (!Schema::hasColumn('user_login', 'platform')) {
                    $table->string('platform', 32)->default('')->comment('平台：PC/Android/iOS/WeChat')->after('os');
                }
            });
        }

        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']);
        if (!$menu) {
            $menuId = DB::table('menus')->insertGetId([
                'parent_id' => $parentId,
                'title' => '在线汇总',
                'icon' => null,
                'uri' => self::MENU_URI,
                'permission_name' => 'user.online_summary',
                'sort' => 5,
                '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('user_login')) {
            Schema::table('user_login', function (Blueprint $table) {
                foreach (['login_domain', 'platform'] as $col) {
                    if (Schema::hasColumn('user_login', $col)) {
                        $table->dropColumn($col);
                    }
                }
            });
        }
        if (Schema::hasTable('menus')) {
            DB::table('menus')->whereIn('uri', array_merge([self::MENU_URI], array_keys(self::BUTTONS)))->delete();
        }
    }
};
