<?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('gallery_albums', function (Blueprint $table) {
            $table->id();
            $table->foreignId('association_id')->constrained()->cascadeOnDelete();
            $table->foreignId('created_by')->constrained('users')->cascadeOnDelete();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('cover_image_path')->nullable();
            $table->enum('source_type', ['event', 'project', 'general'])->default('general');
            $table->unsignedBigInteger('source_id')->nullable();
            $table->boolean('is_published')->default(true);
            $table->boolean('allow_member_uploads')->default(false);
            $table->unsignedInteger('display_order')->default(0);
            $table->unsignedInteger('media_count')->default(0);
            $table->timestamps();

            $table->index(['association_id', 'is_published']);
            $table->index(['source_type', 'source_id']);
        });

        Schema::create('gallery_media', function (Blueprint $table) {
            $table->id();
            $table->foreignId('gallery_album_id')->constrained()->cascadeOnDelete();
            $table->foreignId('uploaded_by')->constrained('users')->cascadeOnDelete();
            $table->enum('media_type', ['image', 'video', 'link'])->default('image');
            $table->string('file_path')->nullable();
            $table->string('external_url')->nullable();
            $table->string('thumbnail_path')->nullable();
            $table->text('caption')->nullable();
            $table->unsignedBigInteger('file_size')->nullable();
            $table->unsignedInteger('display_order')->default(0);
            $table->boolean('is_approved')->default(true);
            $table->timestamps();

            $table->index(['gallery_album_id', 'is_approved']);
        });
    }

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