<?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('academy_course_lessons', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('course_id')->constrained('academy_courses')->cascadeOnDelete();
            $table->foreignId('section_id')->nullable()->constrained('academy_course_sections')->nullOnDelete();
            $table->foreignId('lesson_id')->constrained('academy_lessons')->cascadeOnDelete();
            $table->unsignedInteger('order_num')->default(0);
            $table->boolean('is_required')->default(true);
            $table->string('access_override')->nullable();
            $table->foreignId('unlock_after_lesson_id')->nullable()->constrained('academy_lessons')->nullOnDelete();
            $table->timestamps();

            $table->unique(['course_id', 'lesson_id']);
            $table->index(['course_id', 'section_id', 'order_num']);
            $table->index(['lesson_id']);
            $table->index(['is_required']);
        });
    }

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