<?php

namespace App\Mapper;

use App\ApiResource\PartnerApi;
use App\ApiResource\PartnerFollowApi;
use App\ApiResource\UserApi;
use App\Entity\PartnerFollow;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: PartnerFollow::class, to: PartnerFollowApi::class)]
class PartnerFollowEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    )
    {
    }

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

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

        return $dto;
    }

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

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



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

        return $dto;
    }
}
