<?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::table('bookings', function (Blueprint $table) {
            // Account & Billing
            $table->string('account_name', 255)->nullable()->after('operator_id');
            $table->string('billing_contact', 255)->nullable()->after('account_name');

            // Booked By info
            $table->string('booked_by_first', 255)->nullable()->after('billing_contact');
            $table->string('booked_by_last', 255)->nullable()->after('booked_by_first');
            $table->string('booked_by_phone', 20)->nullable()->after('booked_by_last');
            $table->string('booked_by_email', 255)->nullable()->after('booked_by_phone');

            // Occasion & Group (booking level)
            $table->string('group_name', 255)->nullable()->after('booked_by_email');
            $table->string('occasion', 100)->nullable()->after('group_name');
            $table->string('greet_sign', 255)->nullable()->after('occasion');

            // Service & Route type
            $table->string('service_type', 100)->nullable()->after('greet_sign');
            $table->string('route_type', 20)->nullable()->after('service_type');

            // Extra time fields (stored as HH:MM strings)
            $table->string('do_time', 10)->nullable()->after('route_type');
            $table->string('gar_out_time', 10)->nullable()->after('do_time');
            $table->string('gar_in_time', 10)->nullable()->after('gar_out_time');
            $table->string('spot_time', 10)->nullable()->after('gar_in_time');

            // Pickup address details
            $table->string('pickup_city', 100)->nullable()->after('pickup_longitude');
            $table->string('pickup_state', 100)->nullable()->after('pickup_city');
            $table->string('pickup_zip', 20)->nullable()->after('pickup_state');
            $table->string('pickup_country', 100)->nullable()->after('pickup_zip');

            // Dropoff address details
            $table->string('dropoff_city', 100)->nullable()->after('dropoff_longitude');
            $table->string('dropoff_state', 100)->nullable()->after('dropoff_city');
            $table->string('dropoff_zip', 20)->nullable()->after('dropoff_state');
            $table->string('dropoff_country', 100)->nullable()->after('dropoff_zip');

            // Rate fields
            $table->decimal('flat_rate', 10, 2)->nullable()->after('tip_amount');
            $table->decimal('per_hour_rate', 10, 2)->nullable()->after('flat_rate');
            $table->decimal('per_hour_hours', 6, 2)->nullable()->after('per_hour_rate');
            $table->decimal('ot_wait_rate', 10, 2)->nullable()->after('per_hour_hours');
            $table->decimal('ot_wait_hours', 6, 2)->nullable()->after('ot_wait_rate');
            $table->decimal('extra_stops_rate', 10, 2)->nullable()->after('ot_wait_hours');
            $table->integer('extra_stops_count')->nullable()->after('extra_stops_rate');
            $table->decimal('per_mile_rate', 10, 2)->nullable()->after('extra_stops_count');
            $table->decimal('per_mile_miles', 10, 2)->nullable()->after('per_mile_rate');
            $table->decimal('std_gear_rate', 10, 2)->nullable()->after('per_mile_miles');

            // Dispatch & farm-out
            $table->text('dispatch_notes')->nullable()->after('special_requests');
            $table->string('efarm_status', 50)->nullable()->after('dispatch_notes');
            $table->string('efarm_option', 50)->nullable()->after('efarm_status');
            $table->string('farm_option', 50)->nullable()->after('efarm_option');
        });
    }

    public function down(): void
    {
        Schema::table('bookings', function (Blueprint $table) {
            $table->dropColumn([
                'account_name', 'billing_contact',
                'booked_by_first', 'booked_by_last', 'booked_by_phone', 'booked_by_email',
                'group_name', 'occasion', 'greet_sign',
                'service_type', 'route_type',
                'do_time', 'gar_out_time', 'gar_in_time', 'spot_time',
                'pickup_city', 'pickup_state', 'pickup_zip', 'pickup_country',
                'dropoff_city', 'dropoff_state', 'dropoff_zip', 'dropoff_country',
                'flat_rate', 'per_hour_rate', 'per_hour_hours',
                'ot_wait_rate', 'ot_wait_hours',
                'extra_stops_rate', 'extra_stops_count',
                'per_mile_rate', 'per_mile_miles',
                'std_gear_rate',
                'dispatch_notes',
                'efarm_status', 'efarm_option', 'farm_option',
            ]);
        });
    }
};
