<?php

app_require('classes/economic.php');

use classes\economic;
use endpoints\economic_customers_endpoint;
use endpoints\customers\economic_customers_endpoint as economic_customers_resource_endpoint;

if (!class_exists('EconomicCreateCustomerInnerStub')) {
    class EconomicCreateCustomerInnerStub extends economic_customers_resource_endpoint
    {
        public array $lastPayload = [];

        public function __construct(private readonly object $stubResponse)
        {
        }

        public function create(array $data): object
        {
            $this->lastPayload = $data;

            return $this->stubResponse;
        }
    }
}

if (!class_exists('EconomicCreateCustomerEndpointStub')) {
    class EconomicCreateCustomerEndpointStub extends economic_customers_endpoint
    {
        public function __construct(public EconomicCreateCustomerInnerStub $inner)
        {
            $this->customers = $inner;
        }
    }
}

if (!class_exists('EconomicCreateCustomerProbe')) {
    class EconomicCreateCustomerProbe extends economic
    {
        public EconomicCreateCustomerInnerStub $inner;

        public function __construct(object $stubResponse)
        {
            $this->inner = new EconomicCreateCustomerInnerStub($stubResponse);
            $this->customers = new EconomicCreateCustomerEndpointStub($this->inner);
        }
    }
}

it('returns the raw upstream create response and preserves the requested payload', function (): void {
    $stubResponse = (object)[
        'customerNumber' => 42331123,
        'name' => 'Truckwash ApS',
    ];

    $probe = new EconomicCreateCustomerProbe($stubResponse);
    $result = $probe->createCustomer(42331123, 'Truckwash ApS', 37781258, 'jb@truckwash.dk', 42331123);

    expect($result)->toBe($stubResponse);
    expect($probe->inner->lastPayload['customerNumber'])->toBe(42331123);
    expect($probe->inner->lastPayload['corporateIdentificationNumber'])->toBe('37781258');
    expect($probe->inner->lastPayload['phone'])->toBe(42331123);
    expect($probe->inner->lastPayload['telephoneAndFaxNumber'])->toBe('42331123');
    expect($probe->inner->lastPayload['mobilePhone'])->toBe('42331123');
    expect(array_key_exists('ean', $probe->inner->lastPayload))->toBeFalse();
});

it('adds supported CVR company fields to the e-conomic customer payload', function (): void {
    $stubResponse = (object)[
        'customerNumber' => 42331123,
        'name' => 'Truckwash ApS',
    ];
    $companyInformation = (object)[
        'address' => 'Testvej 12',
        'zipcode' => 2630,
        'city' => 'Taastrup',
        'website' => 'https://truckwash.test',
        'industrycode' => 953190,
    ];

    $probe = new EconomicCreateCustomerProbe($stubResponse);
    $result = $probe->createCustomer(
        42331123,
        'Truckwash ApS',
        37781258,
        'invoice@truckwash.test',
        42331123,
        55667788,
        $companyInformation,
    );

    expect($result)->toBe($stubResponse);
    expect($probe->inner->lastPayload['address'])->toBe('Testvej 12');
    expect($probe->inner->lastPayload['zip'])->toBe('2630');
    expect($probe->inner->lastPayload['city'])->toBe('Taastrup');
    expect($probe->inner->lastPayload['website'])->toBe('https://truckwash.test');
    expect($probe->inner->lastPayload['telephoneAndFaxNumber'])->toBe('42331123');
    expect($probe->inner->lastPayload['mobilePhone'])->toBe('55667788');
    expect(array_key_exists('industrycode', $probe->inner->lastPayload))->toBeFalse();
});

it('adds a normalized EAN to the e-conomic customer payload when provided', function (): void {
    $stubResponse = (object)[
        'customerNumber' => 42331123,
        'name' => 'Truckwash ApS',
    ];

    $probe = new EconomicCreateCustomerProbe($stubResponse);
    $probe->createCustomer(
        42331123,
        'Truckwash ApS',
        37781258,
        'invoice@truckwash.test',
        42331123,
        null,
        null,
        '57 90-001234567',
    );

    expect($probe->inner->lastPayload['ean'])->toBe('5790001234567');
});

it('rejects EAN values longer than e-conomic accepts', function (): void {
    $stubResponse = (object)[
        'customerNumber' => 42331123,
        'name' => 'Truckwash ApS',
    ];

    $probe = new EconomicCreateCustomerProbe($stubResponse);
    $call = static fn() => $probe->createCustomer(
        42331123,
        'Truckwash ApS',
        37781258,
        'invoice@truckwash.test',
        42331123,
        null,
        null,
        '57900012345678',
    );

    expect($call)->toThrow(InvalidArgumentException::class, 'EAN must be at most 13 digits.');
    expect($probe->inner->lastPayload)->toBe([]);
});
