<?php

namespace App\Mapper;

use App\ApiResource\PartnerApi;
use App\ApiResource\ProductApi;
use App\ApiResource\SaleApi;
use App\ApiResource\UserApi;
use App\ApiResource\PostingApi;
use App\Entity\Posting;
use App\Entity\Sale;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: Sale::class, to: SaleApi::class)]
class SaleEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper
    )
    {
    }

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

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

        return $dto;
    }

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

        $dto->owner = $this->microMapper->map($entity->getOwner(), UserApi::class, [
            MicroMapperInterface::MAX_DEPTH => 1,
        ]);

        $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,
        ]);

        $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->turnover = $entity->getTurnover();
        $dto->profit = $entity->getProfit();
        $dto->quantity = $entity->getQuantity();
        $dto->comment = $entity->getComment();
        $dto->createdAt = $entity->getCreatedAt();

        return $dto;
    }
}
