<?php

declare(strict_types=1);

namespace ImovelZapAi\Publishing\Infrastructure\Services;

use Illuminate\Support\Facades\Http;
use ImovelZapAi\Publishing\Application\Ports\MediaAccessibilityCheckerInterface;

final class HttpMediaAccessibilityChecker implements MediaAccessibilityCheckerInterface
{
    public function isAccessible(string $url): bool
    {
        if (trim($url) === '') {
            return false;
        }

        $timeout = (int) config('publishing.media.accessibility_timeout_seconds', 5);

        try {
            $response = Http::timeout($timeout)->head($url);

            return $response->successful();
        } catch (\Throwable) {
            return false;
        }
    }
}
