<?php

namespace App\Mapper;

use App\ApiResource\EventApi;
use App\ApiResource\LocationApi;
use App\ApiResource\UserApi;
use App\ApiResource\UserTripApi;
use App\ApiResource\UserTripEventApi;
use App\Entity\UserTripEvent;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: UserTripEvent::class, to: UserTripEventApi::class)]
class UserTripEventEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    ) {
    }

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

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

        return $dto;
    }

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

        $dto->dbId = $entity->getId();
        $dto->date = $entity->getDate();
        $dto->note = $entity->getNote();
        $dto->createdAt = $entity->getCreatedAt();

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

        $dto->eventIri = $dto->event = $this->microMapper->map($entity->getEvent(), EventApi::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);

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

        $dto->user = $this->microMapper->map($entity->getUserTrip()->getUser(), UserApi::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);

        return $dto;
    }
}