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

            // Agency relationship
            $table->unsignedBigInteger('agency_id');
            $table->unsignedBigInteger('product_id')->nullable();

            // Basic Information
            $table->string('code', 20);
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description');
            $table->text('short_description')->nullable();
            $table->text('places_to_visit')->nullable();
            $table->text('inclusions')->nullable();
            $table->text('exclusions')->nullable();
            $table->text('cancellation_policy')->nullable();
            $table->text('terms_conditions')->nullable();

            // Media
            $table->string('video_url')->nullable();
            $table->json('documents')->nullable();

            // Tour Configuration
            $table->enum('tour_type', ['domestic', 'international'])->default('domestic');
            $table->boolean('has_guide')->default(false);
            $table->json('languages')->nullable();

            // Timing & Schedule (Day Tour Specific)
            $table->time('start_time')->comment('Tour start time');
            $table->time('end_time')->comment('Tour end time');
            $table->integer('duration_hours')->nullable()->comment('Duration in hours');
            $table->json('recurring_days')->nullable()->comment('Days of week tour operates [0=Sunday, 1=Monday, etc.]');

            // Locations
            $table->unsignedBigInteger('departure_city_id');
            $table->unsignedBigInteger('arrival_city_id');
            $table->json('destinations')->nullable();
            $table->decimal('departure_latitude', 10, 8)->nullable();
            $table->decimal('departure_longitude', 11, 8)->nullable();
            $table->decimal('arrival_latitude', 10, 8)->nullable();
            $table->decimal('arrival_longitude', 11, 8)->nullable();

            // Capacity & Age Restrictions
            $table->integer('min_participants')->default(1);
            $table->integer('max_participants')->nullable();
            $table->integer('min_age')->nullable();
            $table->integer('max_age')->nullable();
            $table->boolean('child_friendly')->default(true);
            $table->boolean('infant_allowed')->default(true);

            // Difficulty
            $table->enum('difficulty_level', ['easy', 'moderate', 'challenging', 'extreme'])->default('easy');
            $table->text('difficulty_description')->nullable();

            // Pricing (Base prices - can be overridden per date)
            $table->decimal('adult_price', 10, 2)->default(0);
            $table->decimal('child_price', 10, 2)->default(0);
            $table->decimal('infant_price', 10, 2)->default(0);
            $table->string('currency', 3)->default('TRY');

            // Booking Configuration
            $table->boolean('allows_custom_booking')->default(false)->comment('Allow customers to request custom dates');
            $table->integer('min_bookings_for_guaranteed')->default(1)->comment('Minimum bookings needed for guaranteed departure');
            $table->boolean('auto_confirm_guaranteed')->default(true)->comment('Auto-confirm when min bookings reached');
            $table->boolean('instant_booking')->default(false);
            $table->integer('booking_deadline_hours')->default(24);
            $table->integer('cancellation_deadline_hours')->default(24);
            $table->boolean('refundable')->default(true);

            // SEO & Marketing
            $table->string('meta_title')->nullable();
            $table->text('meta_description')->nullable();
            $table->string('meta_keywords')->nullable();
            $table->json('tags')->nullable();
            $table->boolean('is_featured')->default(false);
            $table->boolean('is_bestseller')->default(false);
            $table->boolean('is_new')->default(false);
            $table->integer('priority')->default(0);

            // Status & Visibility
            $table->boolean('is_active')->default(true);
            $table->boolean('is_public')->default(true);
            $table->boolean('is_bookable')->default(true);
            $table->timestamp('published_at')->nullable();
            $table->timestamp('expires_at')->nullable();

            // Analytics
            $table->unsignedInteger('view_count')->default(0);
            $table->unsignedInteger('booking_count')->default(0);
            $table->unsignedInteger('wishlist_count')->default(0);
            $table->decimal('rating_average', 3, 2)->default(0);
            $table->unsignedInteger('rating_count')->default(0);

            // External Integration
            $table->json('external_ids')->nullable();
            $table->json('api_data')->nullable();

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

            // Foreign Keys
            $table->foreign('agency_id')->references('id')->on('agencies')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->foreign('departure_city_id')->references('id')->on('cities')->onDelete('restrict');
            $table->foreign('arrival_city_id')->references('id')->on('cities')->onDelete('restrict');
            $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(['agency_id', 'is_active']);
            $table->index(['product_id', 'is_active']);
            $table->index(['is_active', 'is_public', 'is_bookable']);
            $table->index(['tour_type', 'is_active']);
            $table->index(['departure_city_id', 'arrival_city_id']);
            $table->index(['is_featured', 'priority']);
            $table->index(['published_at', 'expires_at']);

            // Full-text search
            $table->fullText(['name', 'description', 'short_description']);
        });
    }

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