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

            $table->unsignedBigInteger('international_tour_id');
            $table->unsignedBigInteger('disk_item_id');

            $table->string('image_type')->default('gallery');
            $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);

            $table->string('title')->nullable();
            $table->text('description')->nullable();
            $table->string('alt_text')->nullable();
            $table->json('tags')->nullable();

            $table->string('section')->nullable();
            $table->integer('day_number')->nullable();
            $table->string('location')->nullable();

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

            $table->boolean('use_for_seo')->default(false);
            $table->boolean('use_for_social_media')->default(false);
            $table->json('social_media_sizes')->nullable();

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

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

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

            $table->foreign('international_tour_id')->references('id')->on('international_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');

            $table->index(['international_tour_id', 'is_active', 'sort_order'], 'idx_iti_tour_active_sort');
            $table->index(['international_tour_id', 'image_type'], 'idx_iti_tour_type');
            $table->index(['international_tour_id', 'is_featured'], 'idx_iti_tour_featured');
            $table->index(['international_tour_id', 'is_cover'], 'idx_iti_tour_cover');
            $table->index(['international_tour_id', 'section', 'day_number'], 'idx_iti_tour_section_day');
            $table->index(['disk_item_id']);
            $table->index(['sort_order']);
            $table->index(['created_at']);

            $table->unique(['international_tour_id', 'disk_item_id'], 'unique_intl_tour_disk_item');
        });
    }

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