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

            // Date Information
            $table->date('tour_date');
            $table->time('start_time')->nullable()->comment('Override start time if different from default');
            $table->time('end_time')->nullable()->comment('Override end time if different from default');

            // Status
            $table->enum('status', ['available', 'guaranteed', 'full', 'cancelled'])->default('available');
            $table->boolean('is_active')->default(true);
            $table->boolean('is_guaranteed')->default(false)->comment('Guaranteed departure confirmed');

            // Capacity
            $table->integer('max_participants')->nullable()->comment('Override max capacity if different from default');
            $table->integer('available_seats')->nullable();
            $table->integer('booked_seats')->default(0);
            $table->integer('reserved_seats')->default(0)->comment('Temporarily held seats');

            // Pricing (Override base prices if needed)
            $table->decimal('adult_price', 10, 2)->nullable()->comment('Override adult price');
            $table->decimal('child_price', 10, 2)->nullable()->comment('Override child price');
            $table->decimal('infant_price', 10, 2)->nullable()->comment('Override infant price');

            // Additional Information
            $table->text('notes')->nullable()->comment('Special notes for this date');
            $table->string('guide_name')->nullable();
            $table->string('guide_phone')->nullable();
            $table->unsignedBigInteger('vehicle_id')->nullable()->comment('Assigned vehicle from definitions');

            // Timestamps
            $table->timestamp('guaranteed_at')->nullable()->comment('When tour became guaranteed');
            $table->timestamp('cancelled_at')->nullable();
            $table->string('cancelled_reason')->nullable();
            $table->unsignedBigInteger('cancelled_by')->nullable();

            $table->timestamps();
            $table->softDeletes();

            // Foreign Keys
            $table->foreign('day_tour_id')->references('id')->on('day_tours')->onDelete('cascade');
            $table->foreign('cancelled_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('vehicle_id')->references('id')->on('definitions')->onDelete('set null');

            // Indexes
            $table->index(['day_tour_id', 'tour_date']);
            $table->index(['day_tour_id', 'status', 'is_active']);
            $table->index(['tour_date', 'status']);
            $table->index(['status', 'is_guaranteed']);

            // Unique constraint: one tour per date
            $table->unique(['day_tour_id', 'tour_date']);
        });
    }

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