<?php

namespace Opencart\Extension\Alsendo\Models;

class OrderData
{
    private int $orderId;
    private string $companyName;
    private string $firstName;
    private string $lastName;
    private string $street;
    private string $buildingNumber;
    private string $apartmentNumber;
    private string $postalCode;
    private string $city;
    private string $contactPerson;
    private string $phoneNumber;
    private string $email;
    private array $products;

    public function __construct(array $orderInfo, array $products = [])
    {
        $this->orderId = (int)$orderInfo['order_id'];
        $this->companyName = $orderInfo['payment_company'] ?? '';
        $this->firstName = $orderInfo['shipping_firstname'] ?? $orderInfo['payment_firstname'] ?? '';
        $this->lastName = $orderInfo['shipping_lastname'] ?? $orderInfo['payment_lastname'] ?? '';
        
        $shippingAddress = $orderInfo['shipping_address_1'] ?? $orderInfo['payment_address_1'] ?? '';
        $this->parseAddress($shippingAddress);
        
        $this->postalCode = $orderInfo['shipping_postcode'] ?? $orderInfo['payment_postcode'] ?? '';
        $this->city = $orderInfo['shipping_city'] ?? $orderInfo['payment_city'] ?? '';
        $this->contactPerson = trim($this->firstName . ' ' . $this->lastName);
        $this->phoneNumber = $orderInfo['telephone'] ?? '';
        $this->email = $orderInfo['email'] ?? '';
        $this->products = $products;
    }

    private function parseAddress(string $address): void
    {
        $parts = preg_split('/\s+/', trim($address));
        
        if (count($parts) >= 2) {
            $lastPart = array_pop($parts);
            
            if (preg_match('/^(\d+)([a-zA-Z]?)(?:\/(\d+[a-zA-Z]?))?$/', $lastPart, $matches)) {
                $this->buildingNumber = $matches[1] . ($matches[2] ?? '');
                $this->apartmentNumber = $matches[3] ?? '';
                $this->street = implode(' ', $parts);
            } else {
                $this->street = $address;
                $this->buildingNumber = '';
                $this->apartmentNumber = '';
            }
        } else {
            $this->street = $address;
            $this->buildingNumber = '';
            $this->apartmentNumber = '';
        }
    }

    public function getOrderId(): string
    {
        return sprintf("Zamówienie #%d", $this->orderId);
    }

    public function getCompanyName(): string
    {
        return $this->companyName;
    }

    public function getFirstName(): string
    {
        return $this->firstName;
    }

    public function getLastName(): string
    {
        return $this->lastName;
    }

    public function getStreet(): string
    {
        return $this->street;
    }

    public function getBuildingNumber(): string
    {
        return $this->buildingNumber;
    }

    public function getApartmentNumber(): string
    {
        return $this->apartmentNumber;
    }

    public function getPostalCode(): string
    {
        return $this->postalCode;
    }

    public function getCity(): string
    {
        return $this->city;
    }

    public function getContactPerson(): string
    {
        return $this->contactPerson;
    }

    public function getPhoneNumber(): string
    {
        return $this->phoneNumber;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

    public function getProducts(): array
    {
        return $this->products;
    }

    public function toArray(): array
    {
        return [
            'order_id' => $this->orderId,
            'company_name' => $this->companyName,
            'first_name' => $this->firstName,
            'last_name' => $this->lastName,
            'street' => $this->street,
            'building_number' => $this->buildingNumber,
            'apartment_number' => $this->apartmentNumber,
            'postal_code' => $this->postalCode,
            'city' => $this->city,
            'contact_person' => $this->contactPerson,
            'phone_number' => $this->phoneNumber,
            'email' => $this->email
        ];
    }
} 