<?php

declare(strict_types=1);

namespace KupShop\PreordersBundle\Twig\Components\Preorders;

use KupShop\ComponentsBundle\Attributes\Component;
use KupShop\ComponentsBundle\Attributes\Version;
use KupShop\ComponentsBundle\Twig\BaseComponent;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\PreordersBundle\Dto\DateInfo;
use KupShop\PreordersBundle\Service\PreorderUtil;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;

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

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

    public function isExportAvailable(): bool
    {
        return $this->dateInfo->isExportAvailable();
    }

    public function getExportUrl(): string
    {
        $params = [
            'id_date' => $this->dateInfo->getId(),
        ];

        if (PreorderUtil::useMultipleDates()) {
            $params['export_preorder'] = 1;
        }

        return path('kupshop_preorders_export_user', $params);
    }

    public function getNextDiscountPrice(): ?Price
    {
        $activeLevel = $this->getActiveDiscountLevelIndex();
        $discountLevels = $this->dateInfo->getDiscountLevels();
        $total = $this->dateInfo->getTotalBasePrice();

        // no discount active yet
        if (null === $activeLevel && !empty($discountLevels)) {
            $priceFrom = $discountLevels[0]['price_from'] ?? null;
            if (!$priceFrom) {
                return null;
            }

            $remaining = toDecimal($priceFrom)->sub($total->getPriceWithoutVat());

            return new Price(
                value: $remaining,
                currency: $total->getCurrency(),
                vat: toDecimal(0),
            );
        }

        if (array_key_exists($nextLevelIndex = $activeLevel + 1, $discountLevels)) {
            $priceFrom = $discountLevels[$nextLevelIndex]['price_from'] ?? null;
            if (!$priceFrom) {
                return null;
            }

            $remaining = toDecimal($priceFrom)->sub($total->getPriceWithoutVat());

            return new Price(
                value: $remaining,
                currency: $total->getCurrency(),
                vat: toDecimal(0),
            );
        }

        return null;
    }

    private function getActiveDiscountLevelIndex(): ?int
    {
        $total = $this->dateInfo->getTotalBasePrice()->getPriceWithoutVat();
        $discountLevels = $this->dateInfo->getDiscountLevels();

        foreach ($discountLevels as $index => $discountLevel) {
            $priceFrom = $discountLevel['price_from'] ? toDecimal($discountLevel['price_from']) : null;
            $priceTo = $discountLevel['price_to'] ? toDecimal($discountLevel['price_to']) : null;

            if ((!$priceFrom || $priceFrom->lessThan($total)) && (!$priceTo || $total->lessThan($priceTo))) {
                return $index;
            }
        }

        return null;
    }

    public function getTermsAndConditions(): ?string
    {
        return str_replace('//', '/', createScriptURL([
            's' => 'page',
            'label' => 'shopping-rules',
        ]));
    }
}
