<?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('papers', function (Blueprint $table) {
            // 添加 completed_at 字段，记录试卷完成时间
            $table->timestamp('completed_at')->nullable()->after('updated_at')->comment('试卷完成时间');

            // 创建索引优化查询
            $table->index('completed_at', 'idx_papers_completed_at');
            $table->index(['status', 'completed_at'], 'idx_papers_status_completed_at');
        });
    }

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

            // 删除字段
            $table->dropColumn('completed_at');
        });
    }
};
