<?php

namespace App\Mapper;

use App\ApiResource\MediaObjectApi;
use App\Entity\MediaObject;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Vich\UploaderBundle\Storage\StorageInterface;

#[AsMapper(from: MediaObject::class, to: MediaObjectApi::class)]
class MediaObjectEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private StorageInterface $storage
    ) {
    }

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

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

        return $dto;
    }

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

        $dto->dbId = $entity->getId();
        $dto->filePath = $entity->getFilePath();
        $dto->contentUrl = $this->storage->resolveUri($entity, 'file');
        $dto->createdAt = $entity->getCreatedAt();

        return $dto;
    }
}