<?php

namespace App\Http\Controllers;

use App\Services\LegGovDiscoveryService;
use App\Services\LegGovMetricsService;

class DashboardController extends Controller
{
    public function index(
        LegGovDiscoveryService $discovery,
        LegGovMetricsService $metrics
    ) {
        $apps = array_map(
            fn (array $app) => $metrics->enrich($app),
            $discovery->discover()
        );

        $totals = [
            'instalacoes' => count($apps),
            'online' => count(array_filter(
                $apps,
                fn ($a) => $a['status'] === 'online'
            )),
            'usuarios' => array_sum(array_map(
                fn ($a) => (int) ($a['active_users'] ?? 0),
                $apps
            )),
            'laravel' => count(array_filter(
                $apps,
                fn ($a) => $a['type'] === 'laravel'
            )),
            'php' => count(array_filter(
                $apps,
                fn ($a) => $a['type'] === 'php'
            )),
        ];

        return view('dashboard', compact('apps', 'totals'));
    }
}
