// tools/create-spec/event-spec-adapter.ts — Event-driven spec enrichment (SPEC-031)
// Adds DLQ and idempotence criteria to specs for event-driven features.
import { detectBrokers, getBrokerConfig, requiresIdempotence, supportsDLQ, } from '../../engine/event-detector.js';
/**
 * Detect whether the project uses a known event broker.
 * Returns true when at least one non-unknown broker is detected.
 */
export function isEventDrivenProject(knowledge) {
    const brokers = detectBrokers(knowledge.stack);
    return brokers.length > 0 && brokers[0] !== 'unknown';
}
/**
 * Build an event-driven section for the HU.md of a spec that produces or
 * consumes events. Includes:
 * - Idempotence criterion (mandatory when broker is at-least-once)
 * - DLQ criteria (retry backoff, max retries, monitoring, re-injection)
 */
export function buildEventHuSection(specTitle, knowledge) {
    const brokers = detectBrokers(knowledge.stack);
    const primaryBroker = brokers[0] ?? 'unknown';
    if (primaryBroker === 'unknown') {
        return '';
    }
    const brokerConfig = getBrokerConfig(primaryBroker);
    const brokerName = brokerConfig?.displayName ?? primaryBroker;
    const lines = [
        '',
        `## Event-Driven Contracts (${brokerName})`,
        '',
        `> Auto-generated by Planu for ${brokerName}-based features (SPEC-031).`,
        '',
        '### Contratos de Eventos',
        '',
        `- [ ] Un contrato formal de evento (JSON Schema) debe ser creado en \`data/projects/{hash}/event-contracts/\` antes de implementar. El contrato incluye: nombre del evento, version, schema del payload, producer, consumers conocidos, y garantia de entrega (${brokerConfig?.deliveryGuarantee ?? 'at-least-once'})`,
    ];
    // Idempotence criterion (when broker delivers at-least-once)
    if (requiresIdempotence(primaryBroker)) {
        lines.push('', '### Idempotencia', '', `- [ ] El consumer de \`${specTitle}\` DEBE ser idempotente: procesar el mismo evento dos veces debe producir el mismo resultado que procesarlo una vez. Describir el mecanismo de deduplicacion (event ID en DB, idempotency key, ventana de tiempo).`);
    }
    // DLQ criteria (when broker supports DLQ)
    if (supportsDLQ(primaryBroker)) {
        const dlqSupport = brokerConfig?.dlqSupport ?? 'DLQ';
        lines.push('', '### Dead Letter Queue (DLQ)', '', `- [ ] Cuando el consumer falla al procesar un evento, implementar retry con backoff exponencial (initial: 1s, multiplier: 2, max: 5 reintentos). Tras 5 reintentos fallidos, el evento va a la DLQ (${dlqSupport}).`, `- [ ] La DLQ \`${brokerName.toLowerCase().replace(/\s+/g, '-')}-dlq\` tiene retension de 7 dias, alerta cuando acumula >100 eventos, y un responsable definido.`, `- [ ] Existe un proceso documentado de reinyeccion manual de eventos desde la DLQ, incluyendo validacion del payload antes de reintentar.`);
    }
    return lines.join('\n');
}
//# sourceMappingURL=event-spec-adapter.js.map