<?php

namespace App\Mapper;

use App\ApiResource\PartnerApi;
use App\ApiResource\PartnerProductApi;
use App\ApiResource\ProductApi;
use App\Entity\PartnerProduct;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: PartnerProduct::class, to: PartnerProductApi::class)]
class PartnerProductEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    )
    {
    }

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

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

        return $dto;
    }

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

        $dto->partnerIri = $this->microMapper->map($entity->getPartner(), PartnerApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

        $dto->partner = $this->microMapper->map($entity->getPartner(), PartnerApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

        $dto->productIri = $dto->product = $this->microMapper->map($entity->getProduct(), ProductApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

        $dto->product = $dto->product = $this->microMapper->map($entity->getProduct(), ProductApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

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

        return $dto;
    }
}
