<?php

namespace App\Mapper;

use App\ApiResource\UserApi;
use App\ApiResource\PostingApi;
use App\Entity\User;
use App\Entity\Posting;
use App\Service\FileUrlService;
use Symfonycasts\MicroMapper\AsMapper;
use Symfonycasts\MicroMapper\MapperInterface;
use Symfonycasts\MicroMapper\MicroMapperInterface;

#[AsMapper(from: User::class, to: UserApi::class)]
class UserEntityToApiMapper implements MapperInterface
{
    public function __construct(
        private MicroMapperInterface $microMapper,
        private FileUrlService $fileUrlService
    )
    {
    }

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

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

        return $dto;
    }

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

        $dto->email = $entity->getEmail();
        $dto->firstName = $entity->getFirstName();
        $dto->lastName = $entity->getLastName();
        $dto->goals = $entity->getGoals();
        $dto->image = $entity->getImage();
        $dto->imageUrl = $this->fileUrlService->getFileUrl($entity->getImage());
        $dto->fullName = $entity->getFirstName() . " " . $entity->getLastName();

        return $dto;
    }
}
