<?php

namespace App\Services\Integrations;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class ExternalIncidentPublisher
{
    public function postToSauti(array $payload): bool
    {
        return $this->post('sauti', $payload);
    }

    public function postToGbvPortal(array $payload): bool
    {
        return $this->post('gbv_portal', $payload);
    }

    private function post(string $integration, array $payload): bool
    {
        if (! config("ngbvd_integrations.{$integration}.enabled")) {
            return false;
        }

        $baseUrl = config("ngbvd_integrations.{$integration}.base_url");

        if (! $baseUrl) {
            Log::warning('NGBVD integration skipped because base URL is missing', [
                'integration' => $integration,
            ]);
            return false;
        }

        $response = Http::timeout((int) config("ngbvd_integrations.{$integration}.timeout", 20))
            ->acceptJson()
            ->post($baseUrl, $payload);

        if (! $response->successful()) {
            Log::warning('NGBVD integration post failed', [
                'integration' => $integration,
                'status' => $response->status(),
                'body' => $response->body(),
            ]);
            return false;
        }

        return true;
    }
}
