<?php

namespace KupShop\ComponentsBundle\Twig\Components\Account\OrderDetail;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\Components\Account\UserInfoTrait;
use KupShop\ComponentsBundle\Twig\DataProvider\OrderDataTrait;
use KupShop\KupShopBundle\Context\CountryContext;
use KupShop\KupShopBundle\Util\Contexts;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Components/components/Account/OrderDetail/UserInfo/UserInfo.1.html.twig')]
#[Component(1, [
    new Version(1, newJs: null),
])]
class UserInfo extends BaseComponent
{
    use OrderDataTrait;
    use UserInfoTrait;

    public function __construct(private readonly TranslatorInterface $translator)
    {
    }

    public function getUserInfo(): array
    {
        if (!isset($this->userInfo)) {
            $this->userInfo = [
                $this->translator->trans('info.contact.details', [], 'users') => [],
                $this->translator->trans('info.billing.address', [], 'users') => [],
                $this->translator->trans('info.delivery.address', [], 'users') => [],
                $this->translator->trans('info.company.data', [], 'users') => [],
            ];

            $countries = Contexts::get(CountryContext::class)->getAll();

            if ($invoice = $this->order->getInvoiceUserInfo()) {
                $contactKey = $this->translator->trans('info.contact.details', [], 'users');
                $this->userInfo[$contactKey] = $this->printAddress([
                    'name' => [$invoice->getName(), $invoice->getSurname()],
                    'email' => [$invoice->getEmail()],
                    'phone' => [$invoice->getPhone()],
                ]);

                $country = $countries[$invoice->getCountry()] ?? null;
                $addressKey = $this->translator->trans('info.billing.address', [], 'users');
                $this->userInfo[$addressKey] = $this->printAddress([
                    'street' => [$invoice->getStreet()],
                    'custom_address' => [$invoice->getCustomAddress()],
                    'city' => [$invoice->getZip(), $invoice->getCity()],
                    'state' => [$invoice->getState()],
                    'country' => [$country?->getName() ?? ''],
                ]);
            }

            if ($delivery = $this->order->getDeliveryUserInfo()) {
                $country = $countries[$delivery->getCountry()] ?? null;
                $deliveryKey = $this->translator->trans('info.delivery.address', [], 'users');
                $this->userInfo[$deliveryKey] = $this->printAddress([
                    'name' => [$delivery->getName(), $delivery->getSurname()],
                    'firm' => [$delivery->getFirm()],
                    'street' => [$delivery->getStreet()],
                    'custom_address' => [$delivery->getCustomAddress()],
                    'city' => [$delivery->getZip(), $delivery->getCity()],
                    'state' => [$delivery->getState()],
                    'country' => [$country?->getName() ?? ''],
                ]);
            }

            $targetInfo = $delivery ?? $invoice;
            if ($targetInfo) {
                $companyKey = $this->translator->trans('info.company.data', [], 'users');
                $this->userInfo[$companyKey] = array_filter([
                    'firm' => $targetInfo->getFirm(),
                    'ico' => $targetInfo->getIco() ? "IČO: {$targetInfo->getIco()}" : '',
                    'dic' => $targetInfo->getDic() ? "DIČ: {$targetInfo->getDic()}" : '',
                ]);
            }

            $this->userInfo = array_filter($this->userInfo);
        }

        return $this->userInfo;
    }
}
