<?php

namespace App\Mapper;

use App\ApiResource\ContactApi;
use App\ApiResource\ContactPartnerProductApi;
use App\ApiResource\PartnerProductApi;
use App\Entity\ContactPartnerProduct;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: ContactPartnerProduct::class, to: ContactPartnerProductApi::class)]
class ContactPartnerProductEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    )
    {
    }

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

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

        return $dto;
    }

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

        $dto->contact = $this->microMapper->map($entity->getContact(), ContactApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

        $dto->partnerProduct = $this->microMapper->map($entity->getPartnerProduct(), PartnerProductApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);
        $dto->productName = $entity->getPartnerProduct()?->getProduct()?->getName();

        $dto->createdAt = $entity->getCreatedAt();

        return $dto;
    }
}
