<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * Creates the self_paced_exam_results table — the auto-graded outcome of an
     * Exam_Attempt. One result per attempt (unique constraint on attempt_id).
     */
    public function up(): void
    {
        Schema::create('self_paced_exam_results', function (Blueprint $table) {
            $table->id();

            $table->foreignId('attempt_id')
                ->unique()
                ->constrained('self_paced_exam_attempts')
                ->cascadeOnDelete();
            $table->foreignId('enrollment_id')
                ->constrained('self_paced_enrollments')
                ->cascadeOnDelete();
            $table->foreignId('user_id')
                ->constrained('users')
                ->cascadeOnDelete();
            $table->foreignId('course_id')
                ->constrained('self_paced_courses')
                ->cascadeOnDelete();

            $table->integer('total_questions');
            $table->integer('correct_answers');
            $table->decimal('score_percentage', 5, 2);
            $table->boolean('passed')->default(false);
            $table->integer('passing_threshold');

            // Path to generated result slip PDF
            $table->string('result_slip_path')->nullable();

            $table->timestamps();

            $table->index('enrollment_id');
            $table->index('user_id');
            $table->index('course_id');
            $table->index('passed');
        });
    }

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