<?php

namespace App\Mapper;

use App\ApiResource\TripApi;
use App\Entity\Trip;
use App\Repository\TripRepository;
use App\Repository\VesselRepository;
use App\Repository\LocationRepository;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: TripApi::class, to: Trip::class)]
class TripApiToEntityMapper implements MapperInterface
{
    public function __construct(
        private TripRepository $repository,
        private VesselRepository $vesselRepository,
        private LocationRepository $locationRepository,
    ) {
    }

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

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

        // For new trips, we need the vessel and locations
        if (!$dto->vesselIri->id) {
            throw new \Exception('Vessel is required for new trips');
        }
        if (!$dto->startLocationIri->id || !$dto->endLocationIri->id) {
            throw new \Exception('Start and end locations are required for new trips');
        }

        $vessel = $this->vesselRepository->find($dto->vesselIri->id);
        if (!$vessel) {
            throw new \Exception('Vessel not found');
        }

        $startLocation = $this->locationRepository->find($dto->startLocationIri->id);
        if (!$startLocation) {
            throw new \Exception('Start location not found');
        }

        $endLocation = $this->locationRepository->find($dto->endLocationIri->id);
        if (!$endLocation) {
            throw new \Exception('End location not found');
        }

        return new Trip(
            $vessel,
            $startLocation,
            $endLocation,
            $dto->startDate,
            $dto->endDate
        );
    }

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

        $entity->setStartDate($dto->startDate);
        $entity->setEndDate($dto->endDate);
        $entity->setNote($dto->note);

        if ($dto->vesselIri->id) {
            $vessel = $this->vesselRepository->find($dto->vesselIri->id);
            if (!$vessel) {
                throw new \Exception('Vessel not found');
            }
            $entity->setVessel($vessel);
        }

        if ($dto->startLocationIri->id) {
            $startLocation = $this->locationRepository->find($dto->startLocationIri->id);
            if (!$startLocation) {
                throw new \Exception('Start location not found');
            }
            $entity->setStartLocation($startLocation);
        }

        if ($dto->endLocationIri->id) {
            $endLocation = $this->locationRepository->find($dto->endLocationIri->id);
            if (!$endLocation) {
                throw new \Exception('End location not found');
            }
            $entity->setEndLocation($endLocation);
        }


        return $entity;
    }
}