<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // MySQL 不支持直接修改列类型，需要先删除再添加
        Schema::table('paper_questions', function (Blueprint $table) {
            // 删除旧的 paper_id 字段
            $table->dropColumn('paper_id');
        });

        Schema::table('paper_questions', function (Blueprint $table) {
            // 添加新的字符串类型的 paper_id 字段
            $table->string('paper_id', 64)->nullable()->after('id')->comment('试卷标识符');
            $table->index('paper_id', 'idx_paper_questions_paper_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('paper_questions', function (Blueprint $table) {
            // 删除字符串类型的 paper_id
            $table->dropIndex('idx_paper_questions_paper_id');
            $table->dropColumn('paper_id');
        });

        Schema::table('paper_questions', function (Blueprint $table) {
            // 恢复 bigint 类型的 paper_id
            $table->bigInteger('paper_id')->unsigned()->nullable()->after('id');
            $table->index('paper_id', 'idx_paper_questions_paper_id');
        });
    }
};
