<?php

require_once __DIR__ . '/SegmentSeatAvailabilityBootstrap.php';

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

final class OfferTripEligibilityTest extends TestCase
{
    public function testCalculateFixedDiscount(): void
    {
        $offer = (object) ['discount_type' => 1, 'discount' => 10];
        $result = OfferTripEligibility::calculateSeatFare($offer, 532.0);

        $this->assertSame(10.0, $result['discountAmount']);
        $this->assertSame(522.0, $result['finalFare']);
    }

    public function testCalculatePercentWithCap(): void
    {
        $offer = (object) [
            'discount_type' => 2,
            'discount' => 10,
            'discount_cap' => 25,
            'min_discount' => null,
            'max_discount' => null,
        ];
        $result = OfferTripEligibility::calculateSeatFare($offer, 400.0);

        $this->assertSame(25.0, $result['discountAmount']);
        $this->assertSame(375.0, $result['finalFare']);
    }

    public function testDetailMatchesSubtripFromJsonList(): void
    {
        $detail = (object) [
            'company_id' => '3',
            'main_trip_id' => 74,
            'sub_trip_id' => '[235]',
        ];

        $ref = new ReflectionClass(OfferTripEligibility::class);
        $method = $ref->getMethod('detailMatches');
        $method->setAccessible(true);

        $this->assertTrue($method->invoke(null, $detail, 74, 235, 3));
        $this->assertFalse($method->invoke(null, $detail, 74, 238, 3));
        $this->assertFalse($method->invoke(null, $detail, 75, 235, 3));
    }
}
