<?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->date = $entity->getDate();
        $dto->isArrival = $entity->isArrival();
        $dto->createdAt = $entity->getCreatedAt();

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

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

        return $dto;
    }
}