<?php

namespace App\Mapper;

use App\ApiResource\CommentApi;
use App\ApiResource\ContactApi;
use App\ApiResource\PartnerApi;
use App\ApiResource\ProductApi;
use App\ApiResource\TaskApi;
use App\ApiResource\TaskNoteApi;
use App\ApiResource\UserApi;
use App\ApiResource\PostingApi;
use App\Entity\Comment;
use App\Entity\Task;
use App\Entity\TaskNote;
use Symfony\Bundle\SecurityBundle\Security;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: Task::class, to: TaskApi::class)]
class TaskEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    )
    {
    }

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

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

        return $dto;
    }

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

        $dto->headline = $entity->getHeadline();
        $dto->description = $entity->getDescription();
        $dto->createdBy = $this->microMapper->map($entity->getCreatedBy(), UserApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);
        $dto->assignedTo = $this->microMapper->map($entity->getAssignedTo(), UserApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);
        $dto->assignedToIri = $this->microMapper->map($entity->getAssignedTo(), UserApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);
        $dto->dueAt = $entity->getDueAt();

        if ($entity->getPartner()) {
            $dto->partner = $this->microMapper->map($entity->getPartner(), PartnerApi::class, [
                MicroMapperInterface::MAX_DEPTH => 1,
            ]);
            $dto->partnerIri = $this->microMapper->map($entity->getPartner(), PartnerApi::class, [
                MicroMapperInterface::MAX_DEPTH => 1,
            ]);
        }

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

        $dto->product = $this->microMapper->map($entity->getProduct(), ProductApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);
        $dto->productIri = $this->microMapper->map($entity->getProduct(), ProductApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

        $dto->numTaskNotes = count($entity->getTaskNotes());

        $dto->prio = $entity->getPrio();
        $dto->completed = $entity->getCompleted();
        $dto->createdAt = $entity->getCreatedAt();

        return $dto;
    }
}
