import { Router } from "express";
import { serverLogger } from "../../utils/serverLogger.js";
import { getDatabase } from "../database.js";
/**
 * Create health and status routes
 * Provides system health checks, status information, and discovery endpoints
 */ export function createHealthRoutes(appState) {
    const router = Router();
    /**
   * Comprehensive health check
   * GET /health
   */ router.get("/health", async (req, res)=>{
        try {
            const db = getDatabase();
            const stats = db.getStats();
            const trackCount = stats.total;
            const { getDLNAServer } = await import("../dlna/dlnaServer.js");
            const dlnaServer = getDLNAServer(appState);
            res.json({
                status: "ok",
                timestamp: new Date().toISOString(),
                services: {
                    database: trackCount !== undefined ? "ok" : "error",
                    dlna: dlnaServer.getStatus().running ? "ok" : "stopped",
                    tunnel: appState.getTunnelConfig() ? "configured" : "not_configured"
                },
                library: {
                    tracks: trackCount || 0,
                    lastScanPath: appState.getLastScanPath() || null
                },
                uptime: process.uptime()
            });
        } catch (error) {
            serverLogger.error("Health check failed", {
                error: error instanceof Error ? error.message : String(error),
                stack: error instanceof Error ? error.stack : undefined,
                errorType: typeof error,
                errorKeys: error && typeof error === 'object' ? Object.keys(error) : []
            });
            res.status(500).json({
                status: "error",
                error: "Health check failed",
                message: error instanceof Error ? error.message : String(error)
            });
        }
    });
    /**
   * Simple ping endpoint
   * GET /health/ping
   */ router.get("/health/ping", (req, res)=>{
        res.json({
            status: "ok"
        });
    });
    /**
   * Status endpoint with capabilities
   * GET /status
   */ router.get("/status", async (req, res)=>{
        const db = getDatabase();
        const stats = db.getStats();
        res.json({
            status: "ok",
            version: "1.0.0",
            trackCount: stats.total || 0,
            hasApiKey: !!appState.getApiKey()
        });
    });
    /**
   * Auto-discovery endpoint (no auth required)
   * GET /api/info
   */ router.get("/api/info", (req, res)=>{
        res.json({
            name: "SOUND Music Server",
            version: "1.0.0",
            requiresAuth: !!appState.getApiKey(),
            endpoints: {
                stream: "/stream",
                search: "/api/search",
                artwork: "/artwork/:hash"
            }
        });
    });
    /**
   * Get tunnel status
   * GET /api/tunnelStatus
   */ router.get("/api/tunnelStatus", async (req, res)=>{
        const tunnelConfig = await appState.getTunnelConfig();
        // Note: HybridConnectionManager saves tunnel URL with key 'url', not 'hostname'
        const tunnelUrl = tunnelConfig?.url || tunnelConfig?.hostname || null;
        res.json({
            enabled: !!tunnelConfig,
            hostname: tunnelUrl
        });
    });
    return router;
}

//# sourceMappingURL=healthRoutes.js.map