<?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) {
            // 添加 paper_id 字段（字符串类型，作为试卷的主标识符）
            $table->string('paper_id', 64)->unique()->after('id')->comment('试卷唯一标识符');

            // 添加学生和教师信息
            $table->string('student_id', 64)->nullable()->after('paper_id')->comment('学生ID');
            $table->string('teacher_id', 64)->nullable()->after('student_id')->comment('教师ID');

            // 添加试卷基本信息
            $table->string('paper_name', 255)->nullable()->after('teacher_id')->comment('试卷名称');
            $table->string('paper_type', 32)->nullable()->after('paper_name')->comment('试卷类型：auto_generated, manual, custom');
            $table->integer('question_count')->default(0)->after('paper_type')->comment('题目数量');

            // 添加试卷状态和难度
            $table->string('status', 32)->default('draft')->after('question_count')->comment('试卷状态：draft, completed, graded');
            $table->string('difficulty_category', 32)->default('基础')->after('status')->comment('难度分类：基础, 进阶, 竞赛');

            // 创建索引
            $table->index('student_id', 'idx_papers_student_id');
            $table->index('teacher_id', 'idx_papers_teacher_id');
            $table->index('status', 'idx_papers_status');
            $table->index('paper_type', 'idx_papers_type');
            $table->index('difficulty_category', 'idx_papers_difficulty');
        });
    }

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

            // 删除字段
            $table->dropColumn('paper_id');
            $table->dropColumn('student_id');
            $table->dropColumn('teacher_id');
            $table->dropColumn('paper_name');
            $table->dropColumn('paper_type');
            $table->dropColumn('question_count');
            $table->dropColumn('status');
            $table->dropColumn('difficulty_category');
        });
    }
};
