<?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
    {
        if (!Schema::hasTable('exam_answers')) {
            Schema::create('exam_answers', function (Blueprint $table) {
                $table->id();
                $table->foreignId('attempt_id')->constrained('exam_attempts')->onDelete('cascade');
                $table->foreignId('question_id')->constrained('exam_questions')->onDelete('cascade');
                $table->text('answer')->nullable(); // User's answer
                $table->boolean('is_correct')->nullable(); // Null until graded
                $table->unsignedInteger('points_awarded')->nullable();
                $table->timestamp('answered_at')->nullable();
                $table->timestamps();
                
                $table->unique(['attempt_id', 'question_id']);
                $table->index('attempt_id');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('exam_answers');
    }
};
