<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Round trip is redesigned as two linked one-way Booking rows (outbound
     * + return) rather than a flag + a lone return date on a single row —
     * the return leg needs its own route/time-slot/price/driver-vehicle
     * assignment, since it may run with a different vehicle than the
     * outbound leg (domain.md §2b). `is_round_trip` becomes a computed
     * accessor on the model (`linked_booking_id !== null`), so the column
     * is dropped rather than kept redundant.
     */
    public function up(): void
    {
        Schema::table('bookings', function (Blueprint $table) {
            $table->dropColumn(['is_round_trip', 'return_travel_date']);
            $table->foreignId('linked_booking_id')->nullable()->after('ev_route_id')
                ->constrained('bookings')->nullOnDelete();
            $table->boolean('is_return_leg')->default(false)->after('linked_booking_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('bookings', function (Blueprint $table) {
            $table->dropConstrainedForeignId('linked_booking_id');
            $table->dropColumn('is_return_leg');
            $table->boolean('is_round_trip')->default(false);
            $table->date('return_travel_date')->nullable();
        });
    }
};
