<?php

namespace App\Mapper;

use App\ApiResource\ContactApi;
use App\ApiResource\PostingApi;
use App\ApiResource\SaleApi;
use App\Entity\Contact;
use App\Entity\Partner;
use App\Entity\Product;
use App\Entity\Sale;
use App\Entity\User;
use App\Entity\Posting;
use App\Repository\PostingRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: PostingApi::class, to: Posting::class)]
class PostingApiToEntityMapper implements MapperInterface
{
    public function __construct(
        private PostingRepository    $repository,
        private Security             $security,
        private MicroMapperInterface $microMapper,
    )
    {

    }

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

        if ($dto->id) {
            $entity = $this->repository->find($dto->id);
        } else {
            $user = $this->security->getUser();
            assert($user instanceof User);
            if ($dto->partnerIri === null) {
                throw new \Exception('Partner missing');
            }
            $partner = $this->microMapper->map($dto->partnerIri, Partner::class, [
                MicroMapperInterface::MAX_DEPTH => 1,
            ]);
            if ($dto->productIri === null) {
                throw new \Exception('Product missing');
            }
            $product = $this->microMapper->map($dto->productIri, Product::class, [
                MicroMapperInterface::MAX_DEPTH => 1,
            ]);
            $contact = null;
            if ($dto->contactIri) {
                assert($dto->contactIri instanceof ContactApi);
                $contact = $this->microMapper->map($dto->contactIri, Contact::class, [
                    MicroMapperInterface::MAX_DEPTH => 1,
                ]);
            }
            $sale = null;
            if ($dto->saleIri) {
                assert($dto->saleIri instanceof SaleApi);
                $sale = $this->microMapper->map($dto->saleIri, Sale::class, [
                    MicroMapperInterface::MAX_DEPTH => 1,
                ]);
            }
            assert($partner instanceof Partner);
            assert($contact === null || $contact instanceof Contact);
            assert($sale === null || $sale instanceof Sale);
            $entity = new Posting($user, $partner, $product, $contact, $sale);
        }

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

        return $entity;
    }

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

        $entity->setHeadline($dto->headline);
        $entity->setMessage($dto->message);

        return $entity;
    }
}
