<?php

namespace App\Mapper;

use App\ApiResource\LocationApi;
use App\ApiResource\UserTripApi;
use App\ApiResource\UserTripWorkLogApi;
use App\Entity\UserTripWorkLog;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: UserTripWorkLog::class, to: UserTripWorkLogApi::class)]
class UserTripWorkLogEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    ) {
    }

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

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

        return $dto;
    }

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

        $dto->dbId = $entity->getId();
        $dto->startDate = $entity->getStartDate();
        $dto->endDate = $entity->getEndDate();
        $dto->createdAt = $entity->getCreatedAt();

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

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

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

        return $dto;
    }
}