<?php

declare(strict_types=1);

namespace ImovelZapAi\SuperAdmin\Infrastructure\Http\Controllers\Web;

use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use ImovelZapAi\SuperAdmin\Application\Queries\GetPlatformMetricsQuery;
use ImovelZapAi\SuperAdmin\Application\Services\PlatformHealthProbe;
use ImovelZapAi\SuperAdmin\Infrastructure\Persistence\Eloquent\PlatformIncidentModel;
use ImovelZapAi\SuperAdmin\Domain\Enums\PlatformIncidentStatus;

final class PlatformDashboardController
{
    public function __invoke(
        Request $request,
        GetPlatformMetricsQuery $metrics,
        PlatformHealthProbe $health,
    ): View {
        $days = max(1, min(90, (int) $request->query('days', 30)));

        return view('super-admin.dashboard.index', [
            'metrics' => $metrics->execute($days),
            'health' => $health->probe(persist: false),
            'openIncidents' => PlatformIncidentModel::query()
                ->whereIn('status', [
                    PlatformIncidentStatus::Open,
                    PlatformIncidentStatus::Investigating,
                ])
                ->orderByDesc('created_at')
                ->limit(5)
                ->get(),
            'days' => $days,
        ]);
    }
}
