<?php

namespace App\Mapper;

use App\ApiResource\UserApi;
use App\ApiResource\UserZoneApi;
use App\ApiResource\ZoneApi;
use App\Entity\UserZone;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: UserZone::class, to: UserZoneApi::class)]
class UserZoneEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private readonly MicroMapperInterface $microMapper
    ) {}

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

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

        return $dto;
    }

    public function populate(object $from, object $to, array $context): object
    {
        assert($from instanceof UserZone);
        assert($to instanceof UserZoneApi);

        $to->dbId = $from->getId();

        $to->user = $to->userIri = $this->microMapper->map($from->getUser(), UserApi::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);

        $to->zone = $to->zoneIri = $this->microMapper->map($from->getZone(), ZoneApi::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);

        return $to;
    }
}
