<?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('cultural_tour_images', function (Blueprint $table) {
            $table->id();

            // Relationships
            $table->unsignedBigInteger('cultural_tour_id');
            $table->unsignedBigInteger('disk_item_id'); // Reference to disk_items table

            // Image Properties for Tour
            $table->string('image_type')->default('gallery'); // gallery, featured, cover, thumbnail
            $table->integer('sort_order')->default(0);
            $table->boolean('is_featured')->default(false);
            $table->boolean('is_cover')->default(false);
            $table->boolean('show_in_gallery')->default(true);
            $table->boolean('show_in_slider')->default(false);

            // Image Metadata for Tour Context
            $table->string('title')->nullable(); // Tour-specific title
            $table->text('description')->nullable(); // Tour-specific description
            $table->string('alt_text')->nullable(); // Tour-specific alt text
            $table->json('tags')->nullable(); // Tour-specific tags

            // Tour Section Assignment
            $table->string('section')->nullable(); // itinerary, accommodation, activity, destination, etc.
            $table->integer('day_number')->nullable(); // For itinerary images
            $table->string('location')->nullable(); // For location-specific images

            // Display Settings
            $table->boolean('show_in_thumbnails')->default(true);
            $table->boolean('show_in_lightbox')->default(true);
            $table->boolean('watermark_enabled')->default(false);
            $table->string('caption')->nullable();

            // SEO & Marketing
            $table->boolean('use_for_seo')->default(false);
            $table->boolean('use_for_social_media')->default(false);
            $table->json('social_media_sizes')->nullable(); // Different sizes for social platforms

            // Analytics
            $table->unsignedInteger('view_count')->default(0);
            $table->unsignedInteger('click_count')->default(0);
            $table->timestamp('last_viewed_at')->nullable();

            // Status
            $table->boolean('is_active')->default(true);
            $table->boolean('is_approved')->default(true);
            $table->timestamp('approved_at')->nullable();
            $table->unsignedBigInteger('approved_by')->nullable();

            // Audit Fields
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign Keys
            $table->foreign('cultural_tour_id')->references('id')->on('cultural_tours')->onDelete('cascade');
            $table->foreign('disk_item_id')->references('id')->on('disk_items')->onDelete('cascade');
            $table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            // Indexes
            $table->index(['cultural_tour_id', 'is_active', 'sort_order']);
            $table->index(['cultural_tour_id', 'image_type']);
            $table->index(['cultural_tour_id', 'is_featured']);
            $table->index(['cultural_tour_id', 'is_cover']);
            $table->index(['cultural_tour_id', 'section', 'day_number']);
            $table->index(['disk_item_id']);
            $table->index(['sort_order']);
            $table->index(['created_at']);

            // Unique Constraints
            $table->unique(['cultural_tour_id', 'disk_item_id'], 'unique_cultural_tour_disk_item');
        });
    }

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