<?php

namespace App\Mapper;

use App\ApiResource\ContactApi;
use App\ApiResource\PartnerApi;
use App\ApiResource\PartnerProductApi;
use App\ApiResource\PostingApi;
use App\ApiResource\UserApi;
use App\Entity\Contact;
use App\Entity\Partner;
use App\Entity\PartnerProduct;
use App\Entity\Posting;
use App\Service\FileUrlService;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;
use Vich\UploaderBundle\Storage\StorageInterface;

#[AsMapper(from: Partner::class, to: PartnerApi::class)]
class PartnerEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper,
        private FileUrlService $fileUrlService
    ) {
    }

    public function load(object $from, string $toClass, array $context): object
    {
        $entity = $from;
        assert($entity instanceof Partner);

        $dto = new PartnerApi();
        $dto->id = $entity->getId();

        return $dto;
    }

    public function populate(object $from, object $to, array $context): object
    {
        $entity = $from;
        $dto = $to;
        assert($entity instanceof Partner);
        assert($dto instanceof PartnerApi);

        $dto->navisionId = $entity->getNavisionId();
        $dto->name = $entity->getName();
        $dto->nameAddition = $entity->getNameAddition();
        $dto->partnerType = $entity->getType();
        $dto->description = $entity->getDescription();
        $dto->street = $entity->getStreet();
        $dto->streetNo = $entity->getStreetNo();
        $dto->zip = $entity->getZip();
        $dto->city = $entity->getCity();
        $dto->country = $entity->getCountry();
        $dto->website = $entity->getWebsite();
        $dto->logo = $entity->getLogo();
        $dto->logoUrl = $this->fileUrlService->getFileUrl($entity->getLogo());
        $dto->addressAddition = $entity->getAddressAddition();
        $dto->language = $entity->getLanguage();
        $dto->currency  = $entity->getCurrency();
        $dto->paymentCondition = $entity->getPaymentCondition();
        $dto->deliveryCondition = $entity->getDeliveryCondition();
        $dto->businessGroup = $entity->getBusinessGroup();
        $dto->vatBusinessGroup = $entity->getVatBusinessGroup();
        $dto->creditLimit = $entity->getCreditLimit();
        $dto->routePoint = $entity->getRoutePoint();

        $dto->createdByIri = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);
        $dto->createdAt = $entity->getCreatedAt();

        return $dto;
    }
}
