<?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 gallery_images table to store individual images/media within galleries.
     * Each image has metadata: title, description, filename, path, display order.
     * Tracks when images are created and updated.
     *
     * @return void
     */
    public function up(): void
    {
        Schema::create('gallery_images', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Foreign key to gallery
            $table->foreignId('gallery_id')
                ->constrained('galleries')
                ->onDelete('cascade');

            // Image metadata
            $table->string('title', 191);
            $table->text('description')->nullable();

            // File information
            $table->string('image_path', 255);      // Path to file in storage
            $table->string('image_name', 191);       // Original filename
            $table->integer('file_size')->unsigned(); // Size in bytes
            $table->string('mime_type', 50);         // e.g., image/jpeg, image/png

            // Dimensions (extracted on upload)
            $table->integer('width')->nullable()->unsigned();
            $table->integer('height')->nullable()->unsigned();

            // Display settings
            $table->integer('sort_order')->default(0);

            // Audit trail
            $table->foreignId('uploaded_by')
                ->nullable()
                ->constrained('users')
                ->onDelete('set null');

            // Timestamps and soft deletes
            $table->timestamps();
            $table->softDeletes();

            // Indexes for performance
            $table->index('gallery_id');
            $table->index('sort_order');
            $table->index('uploaded_by');
            $table->index('created_at');
        });
    }

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