<?php

declare(strict_types=1);

namespace App\Services\Vision;

use App\Models\Artwork;
use App\Services\ThumbnailService;

final class ArtworkVisionImageUrl
{
    public function fromArtwork(Artwork $artwork, ?string $size = null): ?string
    {
        return $this->fromHash(
            (string) ($artwork->hash ?? ''),
            (string) ($artwork->thumb_ext ?: 'webp'),
            $size ?? (string) config('vision.image_variant', 'md')
        );
    }

    public function fromHash(?string $hash, ?string $ext = 'webp', string $size = 'md'): ?string
    {
        $clean = strtolower((string) preg_replace('/[^a-z0-9]/', '', (string) $hash));
        if ($clean === '') {
            return null;
        }

        return ThumbnailService::fromHash($clean, $ext, $size);
    }
}
