<?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
    {
        Schema::table('paper_questions', function (Blueprint $table) {
            // 添加评分相关字段
            $table->text('student_answer')->nullable()->after('question_bank_id')->comment('学生答案');
            $table->boolean('is_correct')->nullable()->after('student_answer')->comment('是否正确');
            $table->decimal('score_obtained', 5, 2)->nullable()->after('is_correct')->comment('实际得分');
            $table->boolean('has_been_graded')->default(false)->after('score_obtained')->comment('是否已评分');
            $table->timestamp('graded_at')->nullable()->after('has_been_graded')->comment('评分时间');

            // 创建索引
            $table->index('is_correct', 'idx_paper_questions_is_correct');
            $table->index('has_been_graded', 'idx_paper_questions_has_been_graded');
            $table->index('graded_at', 'idx_paper_questions_graded_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('paper_questions', function (Blueprint $table) {
            // 删除索引
            $table->dropIndex('idx_paper_questions_is_correct');
            $table->dropIndex('idx_paper_questions_has_been_graded');
            $table->dropIndex('idx_paper_questions_graded_at');

            // 删除字段
            $table->dropColumn('student_answer');
            $table->dropColumn('is_correct');
            $table->dropColumn('score_obtained');
            $table->dropColumn('has_been_graded');
            $table->dropColumn('graded_at');
        });
    }
};
