<?php

namespace App\Services\Http;

use Exception;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;

class HttpFetcher
{
    /**
     * @throws Exception
     */
    public static function fetchHtml(string $url): string
    {
        try {
            $response = Http::get($url);

            if (! $response->successful()) {
                throw new Exception("Failed to fetch URL: {$url} - Status: {$response->status()}");
            }

            return $response->body();
        } catch (Exception $e) {
            logger()->error('HTTP fetch failed', [
                'url' => $url,
                'error' => $e->getMessage(),
            ]);

            throw $e;
        }
    }

    /**
     * @param  array<int, string>  $urls
     * @return array<int, array<string, mixed>>
     */
    public static function fetchMultipleUrls(array $urls): array
    {
        try {
            $responses = Http::pool(function ($pool) use ($urls) {
                foreach ($urls as $url) {
                    $pool->get($url);
                }
            });

            return collect($responses)
                ->filter(fn ($response, $index) => isset($urls[$index]))
                ->reject(fn ($response, $index) => $response instanceof Exception)
                ->map(function ($response, $index) use ($urls) {
                    $url = $urls[$index];
                    /** @var Response $response */
                    try {
                        if ($response->successful()) {
                            return [
                                'url' => $url,
                                'html' => $response->body(),
                                'success' => true,
                            ];
                        } else {
                            return [
                                'url' => $url,
                                'html' => null,
                                'success' => false,
                                'status' => $response->status(),
                            ];
                        }
                    } catch (Exception) {
                        return [
                            'url' => $url,
                            'html' => null,
                            'success' => false,
                            'error' => 'Exception occurred',
                        ];
                    }
                })
                ->toArray();
        } catch (Exception $e) {
            logger()->error('Multiple URL fetch failed', ['error' => $e->getMessage()]);

            return [];
        }
    }
}
