<?php

namespace App\Modules\Admin\IntegrationsHealth\Services;

use App\Modules\Admin\IntegrationsHealth\Repositories\IntegrationObservabilityRepository;
use App\Modules\Integrations\Platform\Services\ApiSecurityMonitorService;
use Illuminate\Support\Carbon;

final class IntegrationsHealthService
{
    public function __construct(
        private readonly IntegrationObservabilityRepository $obs,
        private readonly ApiSecurityMonitorService $security,
    ) {}

    /**
     * @return array<string, mixed>
     */
    public function snapshot(): array
    {
        $since24 = Carbon::now()->subHours(24);
        $since1 = Carbon::now()->subHour();

        return [
            'dead_letters_24h' => $this->obs->deadLetterCount($since24),
            'dead_letter_samples' => $this->obs->recentDeadLetters(20),
            'retry_storms_1h' => $this->obs->retryStormCount($since1),
            'slow_webhooks' => $this->obs->slowWebhookSignals($since24),
            'failing_webhook_streams_24h' => $this->obs->failingWebhookEndpoints($since24),
            'api_security' => $this->security->summarize(24),
        ];
    }
}
