"use strict";
/**
 * Overview Routes (Phase 07)
 *
 * Dashboard overview API for counts and health summary.
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.overviewRoutes = void 0;
const express_1 = require("express");
const health_service_1 = require("./health-service");
const auth_handler_1 = require("../cliproxy/auth/auth-handler");
const version_1 = require("../utils/version");
const profile_registry_1 = __importDefault(require("../auth/profile-registry"));
const config_loader_facade_1 = require("../config/config-loader-facade");
exports.overviewRoutes = (0, express_1.Router)();
/**
 * GET /api/overview
 */
exports.overviewRoutes.get('/', async (_req, res) => {
    try {
        const config = (0, config_loader_facade_1.loadConfigSafe)();
        const profileCount = Object.keys(config.profiles).length;
        const cliproxyVariantCount = Object.keys(config.cliproxy || {}).length;
        // Count authenticated built-in providers (gemini, codex, agy, qwen, iflow)
        (0, auth_handler_1.initializeAccounts)();
        const authStatuses = (0, auth_handler_1.getAllAuthStatus)();
        const authenticatedProviderCount = authStatuses.filter((s) => s.authenticated).length;
        // Total CLIProxy = custom variants + authenticated providers
        const totalCliproxyCount = cliproxyVariantCount + authenticatedProviderCount;
        // Get quick health summary
        const health = await (0, health_service_1.runHealthChecks)();
        res.json({
            version: (0, version_1.getVersion)(),
            profiles: profileCount,
            cliproxy: totalCliproxyCount,
            cliproxyVariants: cliproxyVariantCount,
            cliproxyProviders: authenticatedProviderCount,
            accounts: getAccountCount(),
            health: {
                status: health.summary.errors > 0 ? 'error' : health.summary.warnings > 0 ? 'warning' : 'ok',
                passed: health.summary.passed,
                total: health.summary.total,
            },
        });
    }
    catch {
        res.json({
            version: (0, version_1.getVersion)(),
            profiles: 0,
            cliproxy: 0,
            cliproxyVariants: 0,
            cliproxyProviders: 0,
            accounts: 0,
            health: { status: 'error', passed: 0, total: 0 },
        });
    }
});
function getAccountCount() {
    try {
        const registry = new profile_registry_1.default();
        // Merge legacy (profiles.json) and unified (config.yaml) accounts
        const legacyProfiles = registry.getAllProfiles();
        const unifiedAccounts = registry.getAllAccountsUnified();
        // Count unique accounts (unified takes precedence for duplicates)
        const allNames = new Set([...Object.keys(legacyProfiles), ...Object.keys(unifiedAccounts)]);
        return allNames.size;
    }
    catch {
        return 0;
    }
}
//# sourceMappingURL=overview-routes.js.map