<?php

require_once __DIR__ . '/SegmentSeatAvailabilityBootstrap.php';

use App\Libraries\BookingInventorySupport;
use PHPUnit\Framework\TestCase;

final class BookingInventorySupportDatesTest extends TestCase
{
    public function testRegularTripUsesSingleDateForInventory(): void
    {
        $dates = BookingInventorySupport::buildInventoryMatchDates('2026-07-14', '2026-07-14');

        $this->assertSame(['2026-07-14'], $dates);
    }

    public function testMidnightTripIncludesRunAndBoardingDates(): void
    {
        $dates = BookingInventorySupport::buildInventoryMatchDates('2026-07-14', '2026-07-15');

        $this->assertContains('2026-07-14', $dates);
        $this->assertContains('2026-07-15', $dates);
    }

    public function testMidnightTripAddsLookbackWindowForLegacyRows(): void
    {
        $dates = BookingInventorySupport::buildInventoryMatchDates('2026-07-14', '2026-07-15');

        // Protects midnight duplicate-seat checks when older rows were stored one day earlier.
        $this->assertContains('2026-07-13', $dates);
    }

    public function testNormalizeDepartureDatesKeepsProvidedValues(): void
    {
        $normalized = BookingInventorySupport::normalizeDepartureDates([
            'bus_start' => '2026-07-14',
            'boarding' => '2026-07-15',
            'all' => ['2026-07-14', '2026-07-15'],
        ]);

        $this->assertSame('2026-07-14', $normalized['bus_start']);
        $this->assertSame('2026-07-15', $normalized['boarding']);
        $this->assertSame(['2026-07-14', '2026-07-15'], $normalized['all']);
    }

    public function testResolveDepartureDatesDoesNotRecomputeBusStartFromBoarding(): void
    {
        $dates = BookingInventorySupport::resolveDepartureDates(
            75,
            238,
            '2026-07-14',
            '2026-07-15',
            8182
        );

        $this->assertSame('2026-07-14', $dates['bus_start']);
        $this->assertSame('2026-07-15', $dates['boarding']);
        $this->assertContains('2026-07-14', $dates['all']);
        $this->assertContains('2026-07-15', $dates['all']);
    }
}

