<?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('quiz_answers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('quiz_id')->constrained()->cascadeOnDelete();
            $table->foreignId('quiz_question_id')->constrained()->cascadeOnDelete();
            $table->foreignId('participant_id')->constrained()->cascadeOnDelete();
            $table->foreignId('quiz_option_id')->constrained()->cascadeOnDelete();
            $table->boolean('is_correct')->default(false);
            $table->unsignedInteger('response_ms')->default(0);
            $table->unsignedInteger('score_awarded')->default(0);
            $table->timestamp('answered_at')->nullable();
            $table->timestamps();
            $table->unique(['participant_id', 'quiz_question_id']);
            $table->index(['quiz_id', 'quiz_question_id', 'is_correct', 'response_ms'], 'quiz_answers_rank_idx');
        });
    }

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