<?php

namespace App\Services;

use App\Models\Persona;
use App\Services\Image\ImageService;

/**
 * @phpstan-import-type ThumbSize from Image\ImageService
 */
class PersonaService
{
    protected ?Persona $persona;
    public function __construct(
        protected ImageService $imageService,
    ) {}

    public function setPersona(Persona $persona)
    {
        $this->persona = $persona;

        return $this;
    }

    /**
     * @param ThumbSize $size
     */
    public function getImageUrl(string $size = ''): string
    {

        $image = $this->persona->image;

        if (\is_null($image)) {
            return $this->getDefaultImage();
        }

        $thumbnail = $this->imageService
            ->setImageModel($image)
            ->getThumbnail($size);

        return $thumbnail->url;
    }

    public function getDefaultImage(): string
    {
        $fullName = urlencode(
            $this->persona->nombre . ' ' . $this->persona->apellido
        );

        return sprintf(
            'https://ui-avatars.com/api/?background=random&size=640&name=%s',
            $fullName,
        );
    }
}