<?php

declare(strict_types=1);

final class MyronOgImageValidator
{
    /**
     * @return array{valid:bool,message:string,width:?int,height:?int}
     */
    public function validateDimensions(string $imagePath): array
    {
        $imagePath = trim($imagePath);
        if ($imagePath === '') {
            return ['valid' => true, 'message' => '', 'width' => null, 'height' => null];
        }
        if (preg_match('#^https?://#i', $imagePath) === 1) {
            return ['valid' => true, 'message' => 'Could not verify dimensions', 'width' => null, 'height' => null];
        }

        $path = $imagePath;
        if (str_starts_with($path, '/') && !is_file($path) && function_exists('myronProjectRoot')) {
            $path = rtrim(myronProjectRoot(), DIRECTORY_SEPARATOR) . $path;
        }
        if (!is_file($path)) {
            return ['valid' => true, 'message' => 'Could not verify dimensions', 'width' => null, 'height' => null];
        }

        $size = @getimagesize($path);
        if (!is_array($size)) {
            return ['valid' => true, 'message' => 'Could not verify dimensions', 'width' => null, 'height' => null];
        }
        $width = (int) ($size[0] ?? 0);
        $height = (int) ($size[1] ?? 0);
        if ($width < 200 || $height < 200) {
            return ['valid' => false, 'message' => 'Open Graph image is smaller than the minimum 200x200 pixels.', 'width' => $width, 'height' => $height];
        }
        if ($width < 1200 || $height < 630) {
            return ['valid' => true, 'message' => 'Open Graph image is smaller than the recommended 1200x630 pixels.', 'width' => $width, 'height' => $height];
        }
        return ['valid' => true, 'message' => 'Open Graph image dimensions are valid.', 'width' => $width, 'height' => $height];
    }
}
