<?php

declare(strict_types=1);

namespace KupShop\PreordersBundle\Twig\Components\Preorders;

use KupShop\CatalogBundle\ProductList\ProductCollection;
use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataProvider;
use KupShop\PreordersBundle\Dto\DateDateInfo;
use KupShop\PreordersBundle\Dto\DateInfo;
use KupShop\PreordersBundle\Service\PreorderUtil;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

#[AsTwigComponent(template: '@Preorders/components/Products/Products.1.html.twig')]
#[Component(1, [
    new Version(1),
])]
class Products extends BaseComponent
{
    public DateInfo $dateInfo;

    private ProductCollection $products;

    public function __construct(
        private readonly ProductDataProvider $productDataProvider,
    ) {
    }

    public function getDates(): array
    {
        if ($this->dateInfo instanceof DateDateInfo) {
            // fetch only current date (single date on page)
            return [$this->dateInfo->getUserPreorder()->getPreorderDate()];
        }

        // get all valid dates in current preorder
        return $this->dateInfo->getValidDates();
    }

    public function getProducts(): ProductCollection
    {
        if (!isset($this->products)) {
            $this->productDataProvider->addProducts($productCollection = $this->dateInfo->getProducts());

            $this->products = $productCollection;
        }

        return $this->products;
    }

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

    public function getActionUrl(): string
    {
        if (PreorderUtil::useMultipleDates()) {
            return path('kupshop_preorder_update_items', [
                'next' => $this->dateInfo->getId(),
            ]);
        }

        return path('kupshop_preorder_update_items', ['id_date' => $this->dateInfo->getId()]);
    }
}
