<?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
    {
        Schema::create('resources', function (Blueprint $table) {
            $table->id();
            
            // Relación con versión de recursos
            $table->foreignId('resource_version_id')
                ->constrained('resource_versions')
                ->cascadeOnUpdate()
                ->cascadeOnDelete();
            
            // Campos base
            $table->string('title');
            $table->text('description')->nullable();
            $table->enum('type', ['file', 'external']);
            
            // URL/Archivo
            $table->string('url')->nullable();
            $table->string('path')->nullable();
            $table->string('mime')->nullable();
            $table->string('original_filename')->nullable();
            $table->bigInteger('size')->nullable();
            
            // Metadatos
            $table->integer('read_time')->nullable();
            
            // Visibilidad / Estado / Orden
            $table->boolean('is_public')->default(true);
            $table->enum('status', ['draft', 'published', 'archived'])->default('draft');
            $table->integer('sort_order')->nullable();
            
            // Auditoría
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->cascadeOnUpdate();
            
            $table->timestamps();
            
            // Índices
            $table->index(['resource_version_id', 'status']);
            $table->index('resource_version_id');
            $table->index('sort_order');
        });
    }

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