<?php

namespace App\Mapper;

use App\ApiResource\ShippingCompanyApi;
use App\ApiResource\VesselApi;
use App\Entity\Vessel;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: Vessel::class, to: VesselApi::class)]
class VesselEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    ) {
    }

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

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

        return $dto;
    }

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

        $dto->dbId = $entity->getId();
        $dto->name = $entity->getName();
        $dto->code = $entity->getCode();
        $dto->length = $entity->getLength();
        $dto->breadth = $entity->getBreadth();
        $dto->draft = $entity->getDraft();
        $dto->grossTonnage = $entity->getGrossTonnage();
        $dto->imoNo = $entity->getImoNo();
        $dto->callSign = $entity->getCallSign();
        $dto->createdAt = $entity->getCreatedAt();

        $dto->companyIri = $dto->company = $this->microMapper->map($entity->getCompany(), ShippingCompanyApi::class, [
            MicroMapperInterface::MAX_DEPTH => 5,
        ]);

        return $dto;
    }
}
