<?php
declare(strict_types=1);

namespace Autogedal\BlackFriday\Block;

use Autogedal\BlackFriday\Model\Config;
use Autogedal\BlackFriday\Model\PageIdentity;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

/**
 * Shell of the landing page. It holds no product and no price: the list comes from black-friday/products,
 * so the page can stay in the full page cache (tag autogedal_bf, cleaned when the settings change).
 */
class Landing extends Template implements IdentityInterface
{
    public function __construct(
        Context $context,
        private readonly Config $config,
        private readonly Json $json,
        array $data = []
    ) {
        parent::__construct($context, $data);
    }

    public function getConfig(): Config
    {
        return $this->config;
    }

    public function getJsConfig(): string
    {
        $day = $this->config->getStartDayLabel();
        return $this->json->serialize([
            'start' => $this->config->getStartIso(),
            'end' => $this->config->getEndIso(),
            'startDay' => $day,
            'test' => $this->config->isTestMode(),
            'urls' => [
                'products' => $this->getUrl('black-friday/products/index'),
                'subscribe' => $this->getUrl('black-friday/subscribe/index'),
                'favorites' => $this->getUrl('black-friday/favorites/save'),
                'cartAdd' => $this->getUrl('checkout/cart/add'),
                'checkout' => $this->getUrl('checkout'),
            ],
        ]);
    }

    public function getIdentities(): array
    {
        return [PageIdentity::CACHE_TAG];
    }
}
