<?php

declare(strict_types=1);

namespace KupShop\ReturnsBundle\Deliveries;

use KupShop\ReclamationsBundle\Entity\ReclamationEntity;
use KupShop\ReturnsBundle\Deliveries\Utils\ReturnDelivery;
use KupShop\ReturnsBundle\Entity\ReturnEntity;
use KupShop\ReturnsBundle\Page\CPReturnDeliveryPage;
use KupShop\ReturnsBundle\Util\ReturnsUtil;
use Symfony\Contracts\Service\Attribute\Required;

class CPReturnDelivery extends ReturnDelivery
{
    #[Required]
    public ReturnsUtil $returnsUtil;

    protected string $name = 'Česká pošta';
    public string $defaultIcon = '../../common/static/deliveries/balikovna.svg';

    /**
     * @required
     */
    public function setPage(CPReturnDeliveryPage $page): void
    {
        $this->page = $page;
    }

    public function getBaseLabelData(ReturnEntity|ReclamationEntity $entity): array
    {
        $baseLabelData = parent::getBaseLabelData($entity);
        // save authCode to return delivery carrier_id field, which should always contain id of the return delivery package
        if ($entity instanceof ReclamationEntity) {
            $baseLabelData['carrier_id'] = $this->returnsUtil->getAuthCodeForReclamation($entity);
        } else {
            $baseLabelData['carrier_id'] = $this->returnsUtil->getAuthCode($entity->getId());
        }

        return $baseLabelData;
    }

    public function isEnabled(): bool
    {
        return parent::isEnabled() && findModule(\Modules::RETURNS, \Modules::SUB_CP_AUTH_CODES);
    }
}
