<?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
{
    public function up(): void
    {
        if (!Schema::hasTable('app_versionmgr_system_version')) {
            Schema::create('app_versionmgr_system_version', function (Blueprint $table) {
                $table->bigIncrements('id')->comment('主键ID');
                $table->string('version', 20)->comment('版本号');
                $table->string('build', 20)->comment('构建号');
                $table->text('changelog')->nullable()->comment('更新日志');
                $table->string('upgrade_type', 20)->comment('升级类型：full/incremental');
                $table->unsignedTinyInteger('status')->default(1)->comment('状态：0-待升级 1-升级中 2-已完成 3-失败');
                $table->unsignedBigInteger('operator_id')->nullable()->comment('操作人ID');
                $table->string('backup_path', 500)->nullable()->comment('备份文件路径');
                $table->unsignedTinyInteger('upgrade_result')->default(1)->comment('升级结果：0-未执行 1-成功 2-失败');
                $table->text('error_message')->nullable()->comment('错误信息');
                $table->json('detail')->nullable()->comment('升级详情（JSON）');
                $table->timestamp('create_time')->nullable()->comment('创建时间');
                $table->timestamp('update_time')->nullable()->comment('更新时间');

                $table->unique(['version', 'build']);
                $table->index('status');
            });
            DB::statement("ALTER TABLE `app_versionmgr_system_version` COMMENT '版本管理-系统版本记录表'");
        }

        if (!Schema::hasTable('app_versionmgr_upgrade_package')) {
            Schema::create('app_versionmgr_upgrade_package', function (Blueprint $table) {
                $table->bigIncrements('id')->comment('主键ID');
                $table->string('from_version', 20)->comment('源版本号');
                $table->string('to_version', 20)->comment('目标版本号');
                $table->string('build', 20)->comment('构建号');
                $table->text('changelog')->nullable()->comment('更新日志');
                $table->string('package_path', 500)->comment('升级包存储路径');
                $table->unsignedBigInteger('package_size')->nullable()->comment('升级包大小（字节）');
                $table->string('checksum', 100)->comment('升级包校验值（SHA256）');
                $table->string('source', 20)->comment('来源：local/remote');
                $table->unsignedTinyInteger('status')->default(1)->comment('状态：0-待处理 1-已校验 2-已部署');
                $table->unsignedBigInteger('operator_id')->nullable()->comment('操作人ID');
                $table->timestamp('create_time')->nullable()->comment('创建时间');
                $table->timestamp('update_time')->nullable()->comment('更新时间');

                $table->index(['from_version', 'to_version']);
                $table->index('status');
            });
            DB::statement("ALTER TABLE `app_versionmgr_upgrade_package` COMMENT '版本管理-升级包表'");
        }

        if (!Schema::hasTable('app_versionmgr_skip_record')) {
            Schema::create('app_versionmgr_skip_record', function (Blueprint $table) {
                $table->bigIncrements('id')->comment('主键ID');
                $table->string('version', 20)->comment('跳过的版本号');
                $table->string('build', 20)->comment('跳过的构建号');
                $table->unsignedBigInteger('operator_id')->nullable()->comment('操作人ID');
                $table->timestamp('create_time')->nullable()->comment('创建时间');

                $table->unique(['version', 'build']);
            });
            DB::statement("ALTER TABLE `app_versionmgr_skip_record` COMMENT '版本管理-跳过升级记录表'");
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('app_versionmgr_skip_record');
        Schema::dropIfExists('app_versionmgr_upgrade_package');
        Schema::dropIfExists('app_versionmgr_system_version');
    }
};
