<?php

declare(strict_types=1);

namespace KupShop\PreordersBundle\Dto;

use KupShop\ComponentsBundle\Contracts\OrderInterface;
use KupShop\ComponentsBundle\Contracts\UserInfoInterface;
use KupShop\KupShopBundle\Util\Price\TotalPrice;
use KupShop\PreordersBundle\Entity\UserPreorder;
use Symfony\Contracts\Translation\TranslatorInterface;

class UserPreorderWrapper implements OrderInterface
{
    public function __construct(
        private readonly UserPreorder $userPreorder,
        private readonly TotalPrice $totalPrice,
        private readonly TranslatorInterface $translator,
    ) {
    }

    public function getObject(): object
    {
        return $this->userPreorder;
    }

    public function getId(): int
    {
        return $this->userPreorder->preorderDateId;
    }

    public function getUrl(): string
    {
        return path('kupshop_preorders_show_user', ['id_date' => $this->userPreorder->preorderDateId]);
    }

    public function getOrderNo(): string|int
    {
        return $this->getId();
    }

    public function getDateCreated(): ?\DateTimeInterface
    {
        $lastSaved = $this->userPreorder->getCustomData()->get('last_saved_at');
        if (!$lastSaved) {
            return null;
        }

        return \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $lastSaved);
    }

    public function getStatus(): int
    {
        // TODO: Implement getStatus() method.

        return match ($this->userPreorder->getStatus()) {
            'open' => 0,
            'received' => 1,
            'partiallyClosed' => 2,
            'closed' => 3,
        };
    }

    public function getStatusText(): string
    {
        return $this->translator->trans('status.'.$this->userPreorder->getStatus(), [], 'preorders');
    }

    public function getTotalPrice(): TotalPrice
    {
        return $this->totalPrice;
    }

    public function isEditable(): int
    {
        return (int) ($this->getStatus() === 'open');
    }

    public function getEditUrl(): string
    {
        return path('kupshop_preorders_show_active', ['id_date' => $this->userPreorder->preorderDateId]);
    }

    public function getSource(): string
    {
        return 'preorders';
    }

    public function hasProducts(): bool
    {
        return !empty($this->userPreorder->getItems(withProductPhotos: true));
    }

    public function getItems(): array
    {
        $items = $this->userPreorder->getItems(withProductPhotos: true);

        return array_map(fn ($item) => new UserPreorderItem($item), $items);
    }

    public function getItemsTable(): string
    {
        return 'preorders_items';
    }

    public function getCurrency(): string
    {
        return $this->userPreorder->getCustomData()->get('currency') ?? 'CZK';
    }

    public function getDeliveryUserInfo(): ?UserInfoInterface
    {
        return null;
    }

    public function getInvoiceUserInfo(): ?UserInfoInterface
    {
        return new UserInfo($this->userPreorder->getUser());
    }

    public function getHistory(): array
    {
        return $this->userPreorder->getMessages()->toArray();
    }

    public function getTitle(): string
    {
        $id = $this->userPreorder->getPreorderDate()['name']
            ?? $this->userPreorder->userId.'-'.$this->userPreorder->preorderDateId;

        return $this->translator->trans('preorder', [], 'preorders').' '.$id;
    }
}
