import { requireAuth } from "../security/http-auth.js";
function loadNetworkHosts(sqlite) {
    if (!sqlite)
        return [];
    const rows = sqlite.prepare("SELECT * FROM network_hosts ORDER BY last_seen_at DESC").all();
    return rows.map((row) => ({
        ip: row.ip,
        mac: row.mac,
        hostname: row.hostname,
        vendor: null,
        alive: true,
        openPorts: JSON.parse(row.open_ports),
        sshReachable: row.ssh_reachable === 1,
        agentStatus: row.agent_status,
        osVersion: row.os_version,
        providers: row.providers ? JSON.parse(row.providers) : undefined,
        lastSeen: row.last_seen_at,
    }));
}
export function registerEnvironmentRoutes(app, config, deps) {
    app.get("/api/environment/snapshot", async (request, reply) => {
        const user = await requireAuth(request, reply, config.jwtSecret);
        if (!user)
            return;
        const assistants = deps.assistantProfileService?.list(user.id) ?? [];
        const nodes = deps.ws?.getNodeRegistry().nodes ?? [];
        const projects = deps.projectService?.list("active", user.id) ?? [];
        const sessionCounts = deps.projectService?.getActiveSessionCounts(user.id) ?? new Map();
        const environmentProjects = projects.map((project) => ({
            id: project.id,
            title: project.title,
            rootPath: project.rootPath,
            nodeId: project.nodeId,
            status: project.status,
            sessionCount: sessionCounts.get(project.id) ?? 0,
            lastActiveAt: project.lastActiveAt,
        }));
        const connectedNodes = new Map((deps.ws?.getFsNodes() ?? []).map((node) => [node.id, node.name]));
        const repositories = (deps.repoService?.list(user.id) ?? []).map((repo) => ({
            id: repo.id,
            name: repo.name,
            localPath: repo.localPath,
            defaultBranch: repo.defaultBranch,
            githubUrl: repo.githubUrl,
            deviceId: repo.deviceId,
            nodeName: repo.deviceId ? connectedNodes.get(repo.deviceId) ?? null : "Gateway",
            connected: repo.deviceId ? connectedNodes.has(repo.deviceId) : true,
            updatedAt: repo.updatedAt,
        }));
        const providerConnectors = deps.providerRegistry
            ? await deps.providerRegistry.checkAll().then((providers) => providers.map((provider) => ({
                id: provider.id,
                kind: "provider",
                name: provider.name,
                status: provider.available ? "ready" : "offline",
                details: provider.unavailableReason ?? null,
            })))
            : [];
        const nodeConnectors = nodes.map((node) => ({
            id: node.id,
            kind: "node",
            name: node.name,
            status: node.lifecycle === "ready" ? "ready" : "offline",
            details: `${node.platform} · ${node.role}`,
        }));
        const snapshot = {
            serverTime: new Date().toISOString(),
            assistants,
            nodes,
            projects: environmentProjects,
            repositories,
            networkHosts: loadNetworkHosts(deps.sqlite),
            connectors: [...providerConnectors, ...nodeConnectors],
        };
        return { snapshot };
    });
}
//# sourceMappingURL=environment.js.map