<?php

namespace App\Mapper;

use App\ApiResource\PartnerApi;
use App\ApiResource\PostingApi;
use App\Entity\Contact;
use App\Entity\Partner;
use App\Entity\User;
use App\Entity\Posting;
use App\Repository\PartnerRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: PartnerApi::class, to: Partner::class)]
class PartnerApiToEntityMapper implements MapperInterface
{
    public function __construct(
        private PartnerRepository $repository
    )
    {

    }

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

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

        return $entity;
    }

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

        $entity->setName($dto->name);
        $entity->setType($dto->partnerType);
        $entity->setStreet($dto->street);
        $entity->setStreetNo($dto->streetNo);
        $entity->setZip($dto->zip);
        $entity->setCity($dto->city);
        $entity->setCountry($dto->country);
        $entity->setWebsite($dto->website);
        $entity->setLogo($dto->logo);

        return $entity;
    }
}
