<?php

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

return new class extends Migration
{
    public function up(): void
    {
        $exists = DB::table('admin_menu_terminals')->where('type', 'user')->exists();

        if (! $exists) {
            DB::table('admin_menu_terminals')->insert([
                'type' => 'user',
                'name' => '用户中心',
                'description' => '前台用户管理终端',
                'sort' => 2,
                'status' => 1,
                'create_time' => now(),
                'update_time' => now(),
            ]);
        }

        $parentExists = DB::table('admin_menus')
            ->where('terminal_type', 'user')
            ->where('parent_id', 0)
            ->where('name', '用户中心')
            ->exists();

        if (! $parentExists) {
            $parentId = DB::table('admin_menus')->insertGetId([
                'parent_id' => 0,
                'name' => '用户中心',
                'icon' => 'layui-icon-user',
                'path' => '',
                'component' => '',
                'permission_code' => '',
                'type' => 1,
                'visible' => 1,
                'sort' => 1,
                'status' => 1,
                'terminal_type' => 'user',
                'app_id' => null,
                'create_time' => now(),
                'update_time' => now(),
            ]);

            $dashboardExists = DB::table('admin_menus')
                ->where('terminal_type', 'user')
                ->where('path', '/user/dashboard')
                ->exists();

            if (! $dashboardExists) {
                DB::table('admin_menus')->insert([
                    'parent_id' => $parentId,
                    'name' => '我的首页',
                    'icon' => 'layui-icon-home',
                    'path' => '/user/dashboard',
                    'component' => '',
                    'permission_code' => '',
                    'type' => 2,
                    'visible' => 1,
                    'sort' => 1,
                    'status' => 1,
                    'terminal_type' => 'user',
                    'app_id' => null,
                    'create_time' => now(),
                    'update_time' => now(),
                ]);
            }

            $accountExists = DB::table('admin_menus')
                ->where('terminal_type', 'user')
                ->where('path', '/user/account')
                ->exists();

            if (! $accountExists) {
                DB::table('admin_menus')->insert([
                    'parent_id' => $parentId,
                    'name' => '账号管理',
                    'icon' => 'layui-icon-username',
                    'path' => '/user/account',
                    'component' => '',
                    'permission_code' => '',
                    'type' => 2,
                    'visible' => 1,
                    'sort' => 2,
                    'status' => 1,
                    'terminal_type' => 'user',
                    'app_id' => null,
                    'create_time' => now(),
                    'update_time' => now(),
                ]);
            }
        }
    }

    public function down(): void
    {
        DB::table('admin_menus')->where('terminal_type', 'user')->delete();
        DB::table('admin_menu_terminals')->where('type', 'user')->delete();
    }
};
