<?php

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

return new class extends Migration
{
    /**
     * Add nullable topic_id FK to self_paced_exam_questions.
     * Allows exam questions to be associated with a specific CourseTopic.
     * Requirements: 3.6
     */
    public function up(): void
    {
        Schema::table('self_paced_exam_questions', function (Blueprint $table) {
            $table->foreignId('topic_id')
                ->nullable()
                ->after('course_id')
                ->constrained('self_paced_topics')
                ->nullOnDelete();

            $table->index('topic_id');
        });
    }

    public function down(): void
    {
        Schema::table('self_paced_exam_questions', function (Blueprint $table) {
            $table->dropForeign(['topic_id']);
            $table->dropIndex(['topic_id']);
            $table->dropColumn('topic_id');
        });
    }
};
