<?php

namespace App\Mapper;

use App\ApiResource\UserTripApi;
use App\ApiResource\UserTripEventApi;
use App\Entity\Event;
use App\Entity\Location;
use App\Entity\UserTrip;
use App\Entity\UserTripEvent;
use App\Repository\UserTripEventRepository;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

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

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

        if ($dto->id) {
            $entity = $this->repository->find($dto->id);
            if (!$entity) {
                throw new \Exception('UserTripEvent not found');
            }
            return $entity;
        }

        // For new user trip events, we need userTrip and event
        if (!$dto->userTripIri || !$dto->eventIri || !$dto->locationIri) {
            throw new \Exception('UserTrip and Event are required for new user trip events');
        }

        $userTrip = $this->microMapper->map($dto->userTripIri, UserTrip::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);
        if (!$userTrip) {
            throw new \Exception('UserTrip not found');
        }

        $event = $this->microMapper->map($dto->eventIri, Event::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);
        if (!$event) {
            throw new \Exception('Event not found');
        }

        $location = $this->microMapper->map($dto->locationIri, Location::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);
        if (!$location) {
            throw new \Exception('Location not found');
        }

        return new UserTripEvent($userTrip, $event, $location, $dto->date);
    }

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

        $userTrip = $this->microMapper->map($dto->userTripIri, UserTrip::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);
        if (!$userTrip) {
            throw new \Exception('UserTrip not found');
        }
        $entity->setUserTrip($userTrip);

        $event = $this->microMapper->map($dto->eventIri, Event::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);
        if (!$event) {
            throw new \Exception('Event not found');
        }
        $entity->setEvent($event);

        $location = $this->microMapper->map($dto->locationIri, Location::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);
        if (!$location) {
            throw new \Exception('Location not found');
        }
        $entity->setLocation($location);

        $entity->setNote($dto->note);
        $entity->setDate($dto->date);

        return $entity;
    }
}