<?php

namespace App\Mapper;

use App\ApiResource\ProductApi;
use App\Entity\Product;
use App\Service\FileUrlService;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;

#[AsMapper(from: Product::class, to: ProductApi::class)]
class ProductEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private FileUrlService $fileUrlService
    ) {
    }

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

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

        return $dto;
    }

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

        $dto->name = $entity->getName();
        $dto->description = $entity->getDescription();
        $dto->image = $entity->getImage();
        $dto->imageUrl = $this->fileUrlService->getFileUrl($entity->getImage());
        return $dto;
    }
}
