<?php

namespace Opencart\Extension\Alsendo\DTO;

require_once(DIR_EXTENSION . 'alsendo/src/DTO/MoneyDTO.php');
require_once(DIR_EXTENSION . 'alsendo/src/DTO/OrderShipmentPackageSubmitDTO.php');

use Opencart\Extension\Alsendo\DTO\MoneyDTO;

class OrderShipmentSubmitDTO
{
    public string $senderAddressType;
    public ?string $senderCompanyName;
    public ?string $senderFullName;
    public string $senderStreet;
    public string $senderHouseNumber;
    public ?string $senderApartmentNumber;
    public string $senderCountry;
    public string $senderPostalCode;
    public string $senderCity;
    public string $senderPhoneNumber;
    public string $senderEmail;
    public string $senderBankAccountNumber;
    public ?string $senderBankCode = null;
    public ?string $senderAdditionalBankAccountNumber = null;
    public ?string $senderExternalId = null;
    public ?string $senderBlock = null;
    public ?string $senderEntrance = null;
    public ?string $senderFloor = null;
    public ?int $senderTemplateId = null;

    public string $shippingAddressType;
    public ?string $shippingCompanyName;
    public ?string $shippingFirstName;
    public ?string $shippingLastName;
    public string $shippingAddressOne;
    public ?string $shippingAddressTwo;
    //Ecolet
    public ?string $shippingStreet;
    public ?string $shippingNumber;
    public ?string $shippingBlock;
    public ?string $shippingEntrance;
    public ?string $shippingFloor;
    public ?string $shippingApartment;

    public string $shippingCountry;
    public string $shippingPostalCode;
    public string $shippingCity;
    public string $shippingPhoneNumber;
    public string $shippingEmail;
    public mixed $shippingPickupPoint;

    /** @var OrderShipmentPackageSubmitDTO[] */
    public array $packages;
    public ?string $packageContent;
    public ?MoneyDTO $packageCod;
    public ?MoneyDTO $packageDeclaredValue;
    public string $selectedPickupType;

    /** @var bool Ship via pickup point (Czechia only - sets pickupBranch for Zaslat API) */
    public bool $shipViaPickupPoint = false;

    /** @var bool Pickup request (Czechia only - Zaslat API) */
    public bool $pickupRequest = false;

    public ?int $shipmentSelectedService;

    public ?string $shipmentPreferredPickupDate;
    public ?string $shipmentPreferredPickupHourFrom;
    public ?string $shipmentPreferredPickupHourTo;

    public ?string $shipmentPickupPointDisplay;
    public mixed $shipmentPickupPoint;
    public int $orderId;

    public static function fromArray(array $data, array $templateIdToName = []): OrderShipmentSubmitDTO
    {
        $dto = new self();

        $dto->senderAddressType = $data['sender_address_type'] ?? '';
        $dto->senderCompanyName = !empty($data['sender_company_name']) ? $data['sender_company_name'] : null;
        $dto->senderFullName = !empty($data['sender_full_name']) ? $data['sender_full_name'] : null;
        $dto->senderStreet = $data['sender_street'] ?? '';
        $dto->senderHouseNumber = $data['sender_house_number'] ?? '';
        $dto->senderApartmentNumber = !empty($data['sender_apartment_number']) ? $data['sender_apartment_number'] : null;
        $dto->senderCountry = $data['sender_country'] ?? '';
        $dto->senderPostalCode = $data['sender_postal_code'] ?? '';
        $dto->senderCity = $data['sender_city'] ?? '';
        $dto->senderPhoneNumber = $data['sender_phone_number'] ?? '';
        $dto->senderEmail = $data['sender_email'] ?? '';
        $dto->senderBankAccountNumber = $data['sender_bank_account_number'] ?? '';
        $dto->senderBankCode = !empty($data['sender_bank_code']) ? $data['sender_bank_code'] : null;
        $dto->senderAdditionalBankAccountNumber = !empty($data['sender_additional_bank_account_number']) ? $data['sender_additional_bank_account_number'] : null;
        $dto->senderExternalId = !empty($data['sender_external_id']) ? $data['sender_external_id'] : null;
        $dto->senderBlock = !empty($data['sender_block']) ? $data['sender_block'] : null;
        $dto->senderEntrance = !empty($data['sender_entrance']) ? $data['sender_entrance'] : null;
        $dto->senderFloor = !empty($data['sender_floor']) ? $data['sender_floor'] : null;
        $dto->senderTemplateId = isset($data['sender_template_id']) && $data['sender_template_id'] !== '' ? (int)$data['sender_template_id'] : null;

        $dto->shippingAddressType = $data['shipping_address_type'] ?? '';
        $dto->shippingCompanyName = !empty($data['shipping_company_name']) ? $data['shipping_company_name'] : null;
        $dto->shippingFirstName = !empty($data['shipping_first_name']) ? $data['shipping_first_name'] : null;
        $dto->shippingLastName = !empty($data['shipping_last_name']) ? $data['shipping_last_name'] : null;
        $dto->shippingAddressOne = $data['shipping_address_one'] ?? '';
        $dto->shippingAddressTwo = !empty($data['shipping_address_two']) ? $data['shipping_address_two'] : null;
        $dto->shippingCountry = $data['shipping_country'] ?? '';
        $dto->shippingPostalCode = $data['shipping_postal_code'] ?? '';
        $dto->shippingCity = $data['shipping_city'] ?? '';
        $dto->shippingPhoneNumber = $data['shipping_phone_number'] ?? '';
        $dto->shippingEmail = $data['shipping_email'] ?? '';
        $dto->shippingPickupPoint = !empty($data['shipping_pickup_point']) ? json_decode(html_entity_decode($data['shipping_pickup_point']), true) : null;

        // Ecolet
        $dto->shippingStreet = $data['shipping_street'] ?? null;
        $dto->shippingNumber = $data['shipping_number'] ?? null;
        $dto->shippingBlock = $data['shipping_block'] ?? null;
        $dto->shippingEntrance = $data['shipping_entrance'] ?? null;
        $dto->shippingFloor = $data['shipping_floor'] ?? null;
        $dto->shippingApartment = $data['shipping_apartment'] ?? null;

        $dto->packages = OrderShipmentPackageSubmitDTO::multipleFromArray($data, $templateIdToName);
        $dto->packageContent = !empty($data['package_content']) ? $data['package_content'] : null;
        $dto->packageCod = !empty($data['package_cod']) ? MoneyDTO::fromFloat((float)$data['package_cod'], 'PLN') : null;
        $dto->packageDeclaredValue = !empty($data['package_declared_value']) ? MoneyDTO::fromFloat((float)$data['package_declared_value'], 'PLN') : null;
        $dto->selectedPickupType = $data['selected_pickup_type'] ?? '';

        $dto->shipViaPickupPoint = !empty($data['ship_via_pickup_point']) && $data['ship_via_pickup_point'] !== '0';

        $dto->pickupRequest = !empty($data['pickup_request']) && $data['pickup_request'] !== '0';

        $dto->shipmentSelectedService = !empty($data['shipment_selected_service']) ? (int)$data['shipment_selected_service'] : null;
        $dto->shipmentPickupPointDisplay = !empty($data['shipment_pickup_point_display']) ? $data['shipment_pickup_point_display'] : null;

        $dto->shipmentPreferredPickupDate = !empty($data['shipment_preferred_pickup_date']) ? $data['shipment_preferred_pickup_date'] : null;
        $dto->shipmentPreferredPickupHourFrom = !empty($data['shipment_preferred_pickup_hour_from']) ? $data['shipment_preferred_pickup_hour_from'] : null;
        $dto->shipmentPreferredPickupHourTo = !empty($data['shipment_preferred_pickup_hour_to']) ? $data['shipment_preferred_pickup_hour_to'] : null;

        $dto->shipmentPickupPoint = !empty($data['shipment_pickup_point']) ? json_decode(html_entity_decode($data['shipment_pickup_point']), true) : null;
        $dto->orderId = (int)($data['order_id'] ?? 0);

        return $dto;
    }

    public function toArray(): array
    {
        return [
            'sender_address_type' => $this->senderAddressType,
            'sender_company_name' => $this->senderCompanyName,
            'sender_full_name' => $this->senderFullName,
            'sender_street' => $this->senderStreet,
            'sender_house_number' => $this->senderHouseNumber,
            'sender_apartment_number' => $this->senderApartmentNumber,
            'sender_country' => $this->senderCountry,
            'sender_postal_code' => $this->senderPostalCode,
            'sender_city' => $this->senderCity,
            'sender_phone_number' => $this->senderPhoneNumber,
            'sender_email' => $this->senderEmail,
            'sender_bank_account_number' => $this->senderBankAccountNumber,
            'sender_bank_code' => $this->senderBankCode,
            'sender_additional_bank_account_number' => $this->senderAdditionalBankAccountNumber,
            'sender_external_id' => $this->senderExternalId,
            'sender_block' => $this->senderBlock,
            'sender_entrance' => $this->senderEntrance,
            'sender_floor' => $this->senderFloor,
            'sender_template_id' => $this->senderTemplateId,

            'shipping_address_type' => $this->shippingAddressType,
            'shipping_company_name' => $this->shippingCompanyName,
            'shipping_first_name' => $this->shippingFirstName,
            'shipping_last_name' => $this->shippingLastName,
            'shipping_address_one' => $this->shippingAddressOne,
            'shipping_address_two' => $this->shippingAddressTwo,
            'shipping_country' => $this->shippingCountry,
            'shipping_postal_code' => $this->shippingPostalCode,
            'shipping_city' => $this->shippingCity,
            'shipping_phone_number' => $this->shippingPhoneNumber,
            'shipping_email' => $this->shippingEmail,
            'shipping_pickup_point' => is_array($this->shippingPickupPoint) ? json_encode($this->shippingPickupPoint) : '',

            // Ecolet
            'shipping_street' => $this->shippingStreet,
            'shipping_number' => $this->shippingNumber,
            'shipping_block' => $this->shippingBlock,
            'shipping_entrance' => $this->shippingEntrance,
            'shipping_floor' => $this->shippingFloor,
            'shipping_apartment' => $this->shippingApartment,

            'packages' => array_map(fn($item) => $item->toArray(), $this->packages),
            'package_content' => $this->packageContent,
            'package_cod' => $this->packageCod ? $this->packageCod->toFloat() : '',
            'package_declared_value' => $this->packageDeclaredValue ? $this->packageDeclaredValue->toFloat() : '',
            'selected_pickup_type' => $this->selectedPickupType,
            'ship_via_pickup_point' => $this->shipViaPickupPoint ? '1' : '0',
            'pickup_request' => $this->pickupRequest ? '1' : '0',

            'shipment_selected_service' => $this->shipmentSelectedService,
            'shipment_preferred_pickup_date' => $this->shipmentPreferredPickupDate,
            'shipment_pickup_point_display' => $this->shipmentPickupPointDisplay ?? null,
            'shipment_pickup_point' => is_array($this->shipmentPickupPoint) ? json_encode($this->shipmentPickupPoint) : '',
            'order_id' => $this->orderId
        ];
    }
} 