<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('past_question_questions', function (Blueprint $table) {
            $table->id();

            $table->foreignId('past_question_id')
                ->constrained('past_questions')
                ->cascadeOnDelete();

            $table->longText('question');

            /*
                Example:
                [
                    {"key": "A", "text": "Option A"},
                    {"key": "B", "text": "Option B"},
                    {"key": "C", "text": "Option C"},
                    {"key": "D", "text": "Option D"},
                    {"key": "E", "text": "Option E"}
                ]
            */
            $table->json('answer_options')->nullable();

            // Store either A/B/C/D/E or full answer text
            $table->string('correct_answer')->nullable();

            $table->text('explanation')->nullable();

            $table->unsignedInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);

            $table->timestamps();

            $table->index(['past_question_id', 'sort_order']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('past_question_questions');
    }
};