<?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_study_materials')) {
            Schema::create('exam_study_materials', function (Blueprint $table) {
                $table->id();
                $table->foreignId('exam_id')->constrained('exams')->onDelete('cascade');
                $table->string('title');
                $table->text('description')->nullable();
                $table->enum('type', ['pdf', 'video', 'link', 'document'])->default('pdf');
                $table->string('file_url')->nullable();
                $table->unsignedBigInteger('size')->nullable(); // File size in bytes
                $table->unsignedInteger('download_count')->default(0);
                $table->unsignedInteger('order')->default(0);
                $table->timestamps();
                
                $table->index(['exam_id', 'order']);
            });
        }
    }

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