<?php

app_require('classes/economic_v2_versioning_service.php');
app_require('classes/economic_v2_distribution_service.php');

use classes\economic_v2_distribution_service;
use classes\economic_v2_versioning_service;

if (!class_exists('EconomicV2DistributionServiceOrderOverrideVersioningDouble')) {
    class EconomicV2DistributionServiceOrderOverrideVersioningDouble extends economic_v2_versioning_service
    {
        public function __construct()
        {
        }

        public function resolveFixedPricingVersionAt(int $customer_number, string $timestamp): ?array
        {
            return null;
        }

        public function resolveVehicleSubscriptionVersionsAt(int $customer_number, string $timestamp): array
        {
            return [];
        }

        public function resolveDiscountOverrideAt(int $customer_number, bool $is_category, string|int $object_id, string $timestamp, ?int $department_id = null): ?array
        {
            return null;
        }

        public function runBestEffortBackfill(): array
        {
            return [];
        }
    }
}

if (!class_exists('EconomicV2DistributionServiceOrderOverrideDouble')) {
    class EconomicV2DistributionServiceOrderOverrideDouble extends economic_v2_distribution_service
    {
        public function __construct()
        {
            parent::__construct(new EconomicV2DistributionServiceOrderOverrideVersioningDouble());
        }

        public function exposeIsOrderEligible(array $order): bool
        {
            return $this->isOrderEligible($order);
        }

        protected function isDepartmentEligible(int $department_id): bool
        {
            return $department_id === 12;
        }
    }
}

it('inherits department eligibility when no order override is set', function (): void {
    $service = new EconomicV2DistributionServiceOrderOverrideDouble();

    expect($service->exposeIsOrderEligible([
        'department_id' => 12,
        'include_in_invoice' => null,
    ]))->toBeTrue();

    expect($service->exposeIsOrderEligible([
        'department_id' => 13,
        'include_in_invoice' => null,
    ]))->toBeFalse();
});

it('allows an order-level include override to overrule department exclusion', function (): void {
    $service = new EconomicV2DistributionServiceOrderOverrideDouble();

    expect($service->exposeIsOrderEligible([
        'department_id' => 13,
        'include_in_invoice' => '1',
    ]))->toBeTrue();
});

it('allows an order-level exclude override to overrule department inclusion', function (): void {
    $service = new EconomicV2DistributionServiceOrderOverrideDouble();

    expect($service->exposeIsOrderEligible([
        'department_id' => 12,
        'include_in_invoice' => '0',
    ]))->toBeFalse();
});
