<?php

namespace Alsendo\AlsendoWrapper\Tests\Unit\Api\Zaslat;


use Alsendo\AlsendoWrapper\Api\Apaczka\Model\Order\Valuation\ApaczkaOrderValuation;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\Valuation\ZaslatOrderValuation;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\ZaslatOrderCreatedResponse;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\Order\ZaslatOrderResponse;
use Alsendo\AlsendoWrapper\Api\Zaslat\Model\ZaslatContact;
use Alsendo\AlsendoWrapper\ApiClientFactory;
use Alsendo\AlsendoWrapper\ApiClientInterface;
use Alsendo\AlsendoWrapper\Model\Address;
use Alsendo\AlsendoWrapper\Model\Cod;
use Alsendo\AlsendoWrapper\Model\Contact;
use Alsendo\AlsendoWrapper\Model\Notification;
use Alsendo\AlsendoWrapper\Model\NotificationDetail;
use Alsendo\AlsendoWrapper\Model\Order\Created\OrderCreatedResponse;
use Alsendo\AlsendoWrapper\Model\Order\OrderRequest;
use Alsendo\AlsendoWrapper\Model\Pickup;
use Alsendo\AlsendoWrapper\Model\Service\ServiceStructure;
use Alsendo\AlsendoWrapper\Model\Shipment;
use DateTimeImmutable;
use Dotenv\Dotenv;
use Faker\Factory;
use PHPUnit\Framework\TestCase;

class ApiZaslatTest extends TestCase
{
    private ApiClientInterface $apiClient;

    private $faker;

    protected function setUp(): void
    {
        // Ładowanie zmiennych z pliku .env
        $dotenv = Dotenv::createImmutable(__DIR__ . '/../../../../', '.env.test.local');
        $dotenv->load();
        $this->apiClient = (new ApiClientFactory())->create(
            ApiClientFactory::ZASLAT_TEST,
            [
                'api_key' => $_ENV['ZASLAT_API_KEY'],
            ]);

        $this->faker = Factory::create();
        $this->faker->addProvider(new \Faker\Provider\cs_CZ\Address($this->faker));
        $this->faker->addProvider(new \Faker\Provider\cs_CZ\PhoneNumber($this->faker));
        $this->faker->addProvider(new \Faker\Provider\cs_CZ\Person($this->faker));
    }

    /**
     */
    public function testSendOrder(): void
    {
        $orderRequest = new OrderRequest();
        // Set up the order request data
        $orderRequest->carrier = 'GLS';
        $orderRequest->currency = 'CZK';

        $orderRequest->address = new Address(
            new Contact(),
            new Contact(
                'CZ',
                'Jane Brown',
                'Kratka 20',
                '',
                '12000',
                '',
                'Praha',
                1,
                $this->faker->name(),
                $this->faker->email(),
                '+420605222555',
                ''
            )
        );

        $orderRequest->address->sender->id = 374956;

        $orderRequest->option = [
            '31' => 1
        ];

        $orderRequest->shipmentValue = 200;

        $orderRequest->cod = new Cod();

        $orderRequest->cod->amount = 0;
        $orderRequest->cod->bankAccount = '';

        $orderRequest->pickup = new Pickup();

        $orderRequest->pickup->type = 'OCCASIONAL';
        $orderRequest->pickup->date = (new DateTimeImmutable('+1 day'))->format('Y-m-d');
        $orderRequest->pickup->hoursFrom = '12:00';
        $orderRequest->pickup->hoursTo = '19:00';

        $shipment = new Shipment();
        $shipment->dimension1 = 10;
        $shipment->dimension2 = 10;
        $shipment->dimension3 = 10;
        $shipment->weight = 10;
        $shipment->isNstd = 0;
        $shipment->shipmentTypeCode = 'PACZKA';
        $orderRequest->shipment[] = $shipment;


        $orderRequest->content = 'Waciki';


        $response = $this->apiClient->sendOrder($orderRequest);

        $this->assertInstanceOf(ZaslatOrderCreatedResponse::class, $response);
    }

    public function testGetOrderValuation(): void
    {
        $orderRequest = new OrderRequest();
        // Set up the order request data
        $orderRequest->currency = 'CZK';

        $orderRequest->address = new Address(
            new Contact(),
            new Contact()
        );

        $orderRequest->address->receiver->countryCode = 'CZ';
        $orderRequest->address->sender->countryCode = 'CZ';

        $orderRequest->option = [
            '31' => 1
        ];

        $orderRequest->notification = new Notification(
            new NotificationDetail(
                1,
                1,
                1
            ),
            new NotificationDetail(
                1
            ),
            new NotificationDetail(
                1,
                0,
                1
            ),
            new NotificationDetail(
                1,
                0,
                1
            ),
        );

        $orderRequest->shipmentValue = 200;

        $orderRequest->cod = new Cod();

        $orderRequest->cod->amount = 500;
        $orderRequest->cod->bankAccount = '';

        $orderRequest->pickup = new Pickup();

        $orderRequest->pickup->type = 'OCCASIONAL';
        $orderRequest->pickup->date = (new DateTimeImmutable('+1 day'))->format('Y-m-d');
        $orderRequest->pickup->hoursFrom = '12:00';
        $orderRequest->pickup->hoursTo = '19:00';

        $shipment = new Shipment();
        $shipment->dimension1 = 10;
        $shipment->dimension2 = 10;
        $shipment->dimension3 = 10;
        $shipment->weight = 10;
        $shipment->isNstd = 0;
        $shipment->shipmentTypeCode = 'PACZKA';
        $orderRequest->shipment[] = $shipment;

        $orderRequest->content = 'Waciki';


        $response = $this->apiClient->getOrderValuation($orderRequest);
        fwrite(STDERR, var_export($response, true));
        $this->assertIsArray($response);
        $this->assertArrayHasKey(0, $response);
        $this->assertInstanceOf(ZaslatOrderValuation::class, $response[0]);
    }

    public function testGetOrder(): void
    {
        $response = $this->apiClient->getOrder('IZ07BFA2EE08');
        $this->assertInstanceOf(ZaslatOrderResponse::class, $response);
    }

    public function testGetContactsList(): void
    {
        $contacts = $this->apiClient->getContactList();

        $this->assertIsArray($contacts);

        foreach ($contacts as $contact) {
            $this->assertInstanceOf(ZaslatContact::class, $contact);
        }
    }



}