<?php

namespace App\Mapper;

use App\ApiResource\LocationApi;
use App\ApiResource\UserTripApi;
use App\ApiResource\UserTripLocationApi;
use App\Entity\UserTripLocation;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: UserTripLocation::class, to: UserTripLocationApi::class)]
class UserTripLocationEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    ) {
    }

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

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

        return $dto;
    }

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

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

        $dto->userTrip = $this->microMapper->map($entity->getUserTrip(), UserTripApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

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

        return $dto;
    }
}