<?php

namespace App\Mapper;

use App\ApiResource\LocationApi;
use App\ApiResource\TripApi;
use App\ApiResource\TripLocationApi;
use App\Entity\TripLocation;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: TripLocation::class, to: TripLocationApi::class)]
class TripLocationEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    ) {
    }

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

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

        return $dto;
    }

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

        $dto->dbId = $entity->getId();
        $dto->arrivalDateTime = $entity->getArrivalDateTime();
        $dto->departureDateTime = $entity->getDepartureDateTime();
        $dto->createdAt = $entity->getCreatedAt();

        $dto->tripIri = $dto->trip = $this->microMapper->map($entity->getTrip(), TripApi::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);

        $dto->locationIri = $dto->location = $this->microMapper->map($entity->getLocation(), LocationApi::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);

        return $dto;
    }
}