// engine/otel-requirements/slo-requirements.ts — SPEC-044
// Generates SLO/SLI acceptance criteria for services with implicit SLAs.
/** Default SLO values for production APIs */
export const DEFAULT_SLO = {
    availabilityTarget: 99.9,
    p99LatencyMs: 500,
    errorRatePercent: 0.1,
    windowMinutes: 5,
};
/**
 * Calculate monthly downtime minutes from availability target.
 * 99.9% availability = ~43.2 minutes downtime/month.
 */
export function calculateDowntimeMinutes(availabilityPercent) {
    const unavailability = 1 - availabilityPercent / 100;
    return Math.round(unavailability * 30 * 24 * 60);
}
/**
 * Generate SLO base criteria for services with implicit SLAs.
 */
export function generateSloBaseCriteria(config = DEFAULT_SLO) {
    const downtimeMinutes = calculateDowntimeMinutes(config.availabilityTarget);
    return [
        `Disponibilidad ≥ ${config.availabilityTarget}% (menos de ${downtimeMinutes} minutos de downtime por mes)`,
        `Latencia P99 < ${config.p99LatencyMs}ms bajo carga nominal documentada`,
        `Error rate < ${config.errorRatePercent}% de requests en ventana de ${config.windowMinutes} minutos`,
    ];
}
/**
 * Generate OTel resilience questions for challenge_spec.
 */
export function generateOtelResilienceQuestions() {
    return [
        '¿Qué ocurre cuando el OTel Collector no está disponible — el servicio continúa operando o crashea?',
        '¿Las métricas de latencia incluyen el tiempo de cold start o solo el tiempo de handler?',
        '¿El sampling rate configurado es adecuado para no saturar el backend de telemetría bajo carga máxima?',
    ];
}
/**
 * Detect if a project has implicit SLA (public API, auth service, payment gateway).
 */
export function hasImplicitSla(stack, apiContracts) {
    const stackLower = stack.map((s) => s.toLowerCase());
    const allLower = [...stackLower, ...apiContracts.map((c) => c.toLowerCase())];
    // Public APIs (OpenAPI/Swagger detected)
    if (allLower.some((s) => s.includes('openapi') || s.includes('swagger'))) {
        return true;
    }
    // Auth services
    if (allLower.some((s) => s.includes('auth') || s.includes('oauth') || s.includes('jwt'))) {
        return true;
    }
    // Payment gateways
    if (allLower.some((s) => s.includes('stripe') || s.includes('payment') || s.includes('checkout'))) {
        return true;
    }
    return false;
}
/**
 * Format criteria as HU.md checkbox lines.
 */
export function formatAsCriteria(criteria) {
    return criteria.map((c) => `- [ ] ${c}`);
}
/**
 * Build the SLO section for a HU.md file.
 */
export function buildSloSection(config = DEFAULT_SLO) {
    const criteria = generateSloBaseCriteria(config);
    return ['', '## SLO — Objetivos de Nivel de Servicio', '', ...formatAsCriteria(criteria)];
}
/**
 * Build the observability contract section for FICHA-TECNICA.md.
 */
export function buildObservabilityContractSection(signals, backend) {
    const lines = [
        '',
        '## Observability Contract',
        '',
        `**Señales requeridas:** ${signals.length > 0 ? signals.join(', ') : 'traces'}`,
        `**Backend de telemetría:** ${backend ?? 'no configurado — recomendado: Jaeger local para dev'}`,
        '**Sampling rate:** 100% en desarrollo, 10% en producción (o tail-based sampling)',
        '**Retention:** traces 7 días, metrics 30 días, logs 90 días',
    ];
    return lines;
}
//# sourceMappingURL=slo-requirements.js.map