<?php

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

return new class extends Migration
{
    public function up()
    {
        // 系统配置表
        Schema::create('system_setting', function (Blueprint $table) {
            $table->comment('系统配置表');
            $table->id();
            $table->string('group')->comment('配置分组');
            $table->string('key')->unique()->comment('配置键');
            $table->string('name')->comment('配置名称');
            $table->string('type')->default('string')->comment('字段类型：string, text, number, boolean, select, radio, checkbox, file, json');
            $table->text('options')->nullable()->comment('选项配置(JSON格式)');
            $table->text('value')->nullable()->comment('配置值');
            $table->string('default_value')->nullable()->comment('默认值');
            $table->text('description')->nullable()->comment('配置说明');
            $table->string('validation')->nullable()->comment('验证规则');
            $table->integer('sort')->default(0)->comment('排序');
            $table->boolean('is_system')->default(false)->comment('是否系统配置');
            $table->boolean('status')->default(true)->comment('状态');
            $table->timestamps();
            $table->softDeletes();

            $table->index('group');
            $table->index('key');
        });

        // 系统日志表
        Schema::create('system_log', function (Blueprint $table) {
            $table->comment('系统日志表');
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable()->comment('用户ID');
            $table->string('username')->nullable()->comment('用户名');
            $table->string('module')->comment('模块名称');
            $table->string('action')->comment('操作类型');
            $table->string('method')->comment('请求方法');
            $table->string('url')->comment('请求URL');
            $table->string('ip')->nullable()->comment('IP地址');
            $table->text('user_agent')->nullable()->comment('用户代理');
            $table->json('params')->nullable()->comment('请求参数');
            $table->text('result')->nullable()->comment('操作结果');
            $table->integer('status_code')->default(200)->comment('状态码');
            $table->string('status')->default('success')->comment('状态');
            $table->text('error_message')->nullable()->comment('错误信息');
            $table->integer('execution_time')->nullable()->comment('执行时间(毫秒)');
            $table->timestamps();

            $table->index('user_id');
            $table->index('module');
            $table->index('action');
            $table->index('created_at');
        });

        // 系统字典表
        Schema::create('system_dictionary', function (Blueprint $table) {
            $table->comment('系统字典表');
            $table->id();
            $table->string('name')->comment('字典名称');
            $table->string('code')->unique()->comment('字典编码');
            $table->text('description')->nullable()->comment('字典描述');
            $table->string('value_type')->default('string')->comment('值类型：string, number, boolean, json');
            $table->boolean('status')->default(true)->comment('状态');
            $table->integer('sort')->default(0)->comment('排序');
            $table->timestamps();
            $table->softDeletes();
        });

        // 系统字典项表
        Schema::create('system_dictionary_item', function (Blueprint $table) {
            $table->comment('系统字典项表');
            $table->id();
            $table->unsignedBigInteger('dictionary_id')->comment('字典ID');
            $table->string('label')->comment('标签名称');
            $table->string('value')->comment('标签值');
            $table->string('color')->nullable()->comment('颜色标识');
            $table->text('description')->nullable()->comment('描述');
            $table->boolean('is_default')->default(false)->comment('是否默认');
            $table->boolean('status')->default(true)->comment('状态');
            $table->integer('sort')->default(0)->comment('排序');
            $table->timestamps();

            $table->foreign('dictionary_id')->references('id')->on('system_dictionary')->onDelete('cascade');
            $table->index('dictionary_id');
        });

        // 系统任务表
        Schema::create('system_task', function (Blueprint $table) {
            $table->comment('系统任务表');
            $table->id();
            $table->string('name')->comment('任务名称');
            $table->string('command')->comment('任务命令');
            $table->string('description')->nullable()->comment('任务描述');
            $table->string('type')->default('command')->comment('任务类型：command, job, closure');
            $table->string('expression')->comment('Cron表达式');
            $table->string('timezone')->default('Asia/Shanghai')->comment('时区');
            $table->boolean('is_active')->default(true)->comment('是否启用');
            $table->boolean('run_in_background')->default(true)->comment('是否后台运行');
            $table->boolean('without_overlapping')->default(false)->comment('是否防止重叠');
            $table->boolean('only_one')->default(false)->comment('是否只运行一个实例');
            $table->timestamp('last_run_at')->nullable()->comment('最后运行时间');
            $table->timestamp('next_run_at')->nullable()->comment('下次运行时间');
            $table->text('last_output')->nullable()->comment('最后输出');
            $table->integer('run_count')->default(0)->comment('运行次数');
            $table->integer('failed_count')->default(0)->comment('失败次数');
            $table->timestamps();
            $table->softDeletes();

            $table->index('is_active');
        });

        // 系统城市表
        Schema::create('system_city', function (Blueprint $table) {
            $table->comment('系统城市表');
            $table->id();
            $table->string('title')->comment('城市名称');
            $table->string('code')->unique()->comment('城市编码');
            $table->string('parent_code')->nullable()->comment('父级编码');
            $table->timestamps();

            $table->index('code');
            $table->index('parent_code');
        });

        // 系统通知表
        Schema::create('system_notification', function (Blueprint $table) {
            $table->comment('系统通知表');
            $table->id();
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->string('title')->comment('通知标题');
            $table->text('content')->comment('通知内容');
            $table->string('type')->default('info')->comment('通知类型：info, success, warning, error, task, system');
            $table->string('category')->nullable()->comment('通知分类：system, task, message, reminder, announcement');
            $table->json('data')->nullable()->comment('附加数据(JSON格式)');
            $table->string('action_type')->nullable()->comment('操作类型：link, modal, none');
            $table->text('action_data')->nullable()->comment('操作数据');
            $table->boolean('is_read')->default(false)->comment('是否已读');
            $table->timestamp('read_at')->nullable()->comment('阅读时间');
            $table->boolean('sent_via_websocket')->default(false)->comment('是否已通过WebSocket发送');
            $table->timestamp('sent_at')->nullable()->comment('发送时间');
            $table->integer('retry_count')->default(0)->comment('重试次数');
            $table->timestamps();
            $table->softDeletes();

            $table->index('user_id');
            $table->index('is_read');
            $table->index('type');
            $table->index('category');
            $table->index('created_at');
        });
    }

    public function down()
    {
        Schema::dropIfExists('system_notification');
        Schema::dropIfExists('system_dictionary_item');
        Schema::dropIfExists('system_dictionary');
        Schema::dropIfExists('system_setting');
        Schema::dropIfExists('system_log');
        Schema::dropIfExists('system_task');
        Schema::dropIfExists('system_city');
    }
};
