<?php

namespace App\Mapper;

use App\ApiResource\LocationApi;
use App\ApiResource\ZoneApi;
use App\Entity\Location;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: Location::class, to: LocationApi::class)]
class LocationEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    ) {
    }

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

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

        return $dto;
    }

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

        $dto->dbId = $entity->getId();
        $dto->name = $entity->getName();
        $dto->zoneName = $entity->getZone()->getName();
        $dto->code = $entity->getCode();
        $dto->isZone = $entity->isZone();
        $dto->isPlace = $entity->isPlace();
        $dto->isPort = $entity->isPort();
        $dto->isStartEnd = $entity->isStartEnd();
        $dto->createdAt = $entity->getCreatedAt();

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

        return $dto;
    }
}