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

            // Relationships
            $table->unsignedBigInteger('agency_id');
            $table->unsignedBigInteger('product_id')->nullable()->unique();

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

            // Timing
            $table->time('departure_time');
            $table->time('return_time');
            $table->integer('duration_hours')->default(8);

            // Locations
            $table->unsignedBigInteger('departure_city_id');
            $table->unsignedBigInteger('destination_city_id')->nullable();
            $table->json('visit_locations')->nullable();
            $table->decimal('distance_km', 8, 2)->nullable();

            // Tour Configuration
            $table->string('transportation_type')->nullable();
            $table->boolean('has_guide')->default(true);
            $table->json('languages')->nullable();

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

            // Meals & Services
            $table->boolean('meal_included')->default(false);
            $table->string('meal_type')->nullable();
            $table->boolean('beverages_included')->default(false);
            $table->boolean('entrance_fees_included')->default(true);

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

            // Pricing
            $table->decimal('base_price_adult', 10, 2);
            $table->decimal('base_price_child', 10, 2)->nullable();
            $table->decimal('base_price_infant', 10, 2)->nullable();
            $table->string('currency', 3)->default('TRY');

            // Booking
            $table->boolean('instant_booking')->default(false);
            $table->integer('booking_deadline_hours')->default(24);
            $table->integer('cancellation_deadline_hours')->default(24);
            $table->text('cancellation_policy')->nullable();

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

            // SEO
            $table->string('meta_title')->nullable();
            $table->text('meta_description')->nullable();
            $table->json('tags')->nullable();

            // Status
            $table->boolean('is_active')->default(true);
            $table->boolean('is_bookable')->default(true);
            $table->boolean('is_featured')->default(false);
            $table->timestamp('published_at')->nullable();

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

            // Audit
            $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('destination_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(['departure_city_id', 'destination_city_id']);
            $table->index(['is_active', 'is_bookable', 'published_at']);
            $table->fullText(['name', 'description', 'short_description']);
        });
    }

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