<?php

namespace Tricks\MediaLibraryOverrides\Data;

use Illuminate\Database\Eloquent\Model;
use Spatie\LaravelData\Data;

class ImageData extends Data
{
    public function __construct(
        public string $url,
        public ?string $alt,
        public ?ImageDimensionData $dimensions = null,
    ) {
    }

    public static function fromMultiple(string $url, ?string $alt, ?int $dimensions = null): self
    {
        return new self($url, $alt, $dimensions);
    }

    public static function fromModel(
        Model $model,
        string $collection,
        string $conversion = '',
        ?string $alt = null
    ): ImageData {
        $media = $model->getFirstMedia($collection);

        return new self(
            $model->getMediaUrlForImageData($media, $collection, $conversion),
            $alt,
            $media->hasCustomProperty('dimension-'.$conversion)
                ? ImageDimensionData::from($media->getCustomProperty('dimension-'.$conversion))
                : null);
    }
}
