<?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_downloads table — downloadable resources (PDF, Excel,
     * Word, ZIP) attached to a Course or Course_Topic.
     */
    public function up(): void
    {
        Schema::create('self_paced_downloads', function (Blueprint $table) {
            $table->id();

            // Polymorphic-style nullable FKs — attached to course OR topic
            $table->foreignId('course_id')
                ->nullable()
                ->constrained('self_paced_courses')
                ->cascadeOnDelete();
            $table->foreignId('topic_id')
                ->nullable()
                ->constrained('self_paced_topics')
                ->cascadeOnDelete();

            $table->string('title', 191);
            $table->string('file_path');
            $table->string('file_name', 191);
            $table->unsignedBigInteger('file_size')->default(0)->comment('bytes');
            $table->string('mime_type', 100)->nullable();

            $table->timestamps();

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

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