<?php

namespace App\Mapper;

use App\ApiResource\ContactApi;
use App\ApiResource\PartnerApi;
use App\ApiResource\TaskApi;
use App\Entity\Contact;
use App\Entity\Partner;
use App\Entity\Product;
use App\Entity\Task;
use App\Entity\User;
use App\Repository\TaskRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: TaskApi::class, to: Task::class)]
class TaskApiToEntityMapper implements MapperInterface
{
    public function __construct(
        private TaskRepository    $repository,
        private Security             $security,
        private MicroMapperInterface $microMapper,
    )
    {

    }

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

        $assignedTo = $this->microMapper->map($dto->assignedToIri, User::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

        if ($dto->id) {
            $entity = $this->repository->find($dto->id);
            if (!$entity) {
                throw new \Exception('Task not found');
            }
            $entity->setAssignedTo($assignedTo);
        } else {
            $user = $this->security->getUser();

            if ($dto->productIri === null) {
                throw new \Exception('Product missing');
            }
            $product = $this->microMapper->map($dto->productIri, Product::class, [
                MicroMapperInterface::MAX_DEPTH => 1,
            ]);
            assert($product instanceof Product);
            assert($user instanceof User);
            assert($assignedTo instanceof User);
            $entity = new Task($user, $assignedTo, $product);
        }

        if (!$entity) {
            throw new \Exception('Task not found');
        }

        return $entity;
    }

    public function populate(object $from, object $to, array $context): object
    {
        $dto = $from;
        assert($dto instanceof TaskApi);
        $entity = $to;
        assert($entity instanceof Task);
        $entity->setHeadline($dto->headline);
        $entity->setDescription($dto->description);
        $entity->setDueAt($dto->dueAt);
        $entity->setPartner(null);
        if ($dto->partnerIri !== null) {
            assert($dto->partnerIri instanceof PartnerApi);
            $entity->setPartner($this->microMapper->map($dto->partnerIri, Partner::class, [
                MicroMapperInterface::MAX_DEPTH => 1,
            ]));
        }

        $entity->setContact(null);
        if ($dto->contactIri !== null) {
            assert($dto->contactIri instanceof ContactApi);
            $entity->setContact($this->microMapper->map($dto->contactIri, Contact::class, [
                MicroMapperInterface::MAX_DEPTH => 1,
            ]));
        }

        $entity->setPrio($dto->prio);
        $entity->setCompleted($dto->completed);
        return $entity;
    }
}
