<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (Schema::hasTable('paper_questions')) {
            Schema::table('paper_questions', function (Blueprint $table) {
                // 添加题目类型字段
                $table->string('question_type', 32)->nullable()->after('knowledge_point')->comment('题目类型：choice-选择题, fill-填空题, answer-解答题');

                // 创建索引
                $table->index('question_type', 'idx_paper_questions_type');
            });

            // 为现有题目设置默认类型（解答题）
            DB::statement("UPDATE paper_questions SET question_type = 'answer' WHERE question_type IS NULL");
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        if (Schema::hasTable('paper_questions')) {
            Schema::table('paper_questions', function (Blueprint $table) {
                $table->dropIndex('idx_paper_questions_type');
                $table->dropColumn('question_type');
            });
        }
    }
};
