"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildGeneratedAppAutoFixSummary = buildGeneratedAppAutoFixSummary;
exports.buildResolveIOProjectAutoFixDetails = buildResolveIOProjectAutoFixDetails;
exports.buildGeneratedSlowQuerySummary = buildGeneratedSlowQuerySummary;
exports.buildResolveIOProjectSlowQueryDetails = buildResolveIOProjectSlowQueryDetails;
var common_1 = require("../util/common");
function normalizeText(value, fallback) {
    if (fallback === void 0) { fallback = ''; }
    var normalized = String(value || '').trim();
    return normalized || fallback;
}
function normalizeDuration(value) {
    var parsed = Number(value);
    if (!Number.isFinite(parsed) || parsed < 0) {
        return null;
    }
    return parsed;
}
function formatDuration(value) {
    var duration = normalizeDuration(value);
    if (duration === null) {
        return 'unknown';
    }
    return "".concat((0, common_1.round)(duration, 0).toLocaleString(), " ms");
}
function buildGeneratedAppAutoFixSummary(stage, context, extra) {
    var sourceApp = normalizeText(context === null || context === void 0 ? void 0 : context.source_app, 'your app');
    var environment = normalizeText(context === null || context === void 0 ? void 0 : context.environment, 'production');
    var severity = normalizeText(context === null || context === void 0 ? void 0 : context.severity, 'issue');
    if (stage === 'detected_autofix_enabled') {
        return [
            "What happened: We found a ".concat(severity, " in ").concat(sourceApp, "."),
            "Where: ".concat(environment, "."),
            'What happens next: Auto-fix is running. You will get another notice when it is done.'
        ].join('\n');
    }
    if (stage === 'detected_autofix_disabled') {
        return [
            "What happened: We found a ".concat(severity, " in ").concat(sourceApp, "."),
            "Where: ".concat(environment, "."),
            'What happens next: Auto-fix is disabled, so this needs a manual follow-up.'
        ].join('\n');
    }
    if (stage === 'completed_success') {
        return [
            "What happened: We fixed an issue in ".concat(sourceApp, "."),
            "Where: ".concat(environment, "."),
            'What to do: No action is needed unless you still see the same problem.'
        ].join('\n');
    }
    if (stage === 'blocked_library_issue') {
        return [
            "What happened: The issue in ".concat(sourceApp, " is tied to shared platform code."),
            'What happens next: Engineering has been notified to ship a platform-level update.',
            'What to do: Wait for the next platform update notice.'
        ].join('\n');
    }
    return [
        "What happened: We investigated an issue in ".concat(sourceApp, " but could not complete an automatic fix."),
        "Where: ".concat(environment, "."),
        "Why: ".concat((extra === null || extra === void 0 ? void 0 : extra.error) || (context === null || context === void 0 ? void 0 : context.error) || 'Manual review is required.'),
        'What to do: Ask the team to review this issue manually.'
    ].join('\n');
}
function buildResolveIOProjectAutoFixDetails(context) {
    var lines = [
        'Auto-fix completed for a ResolveIO project.',
        '',
        "Client: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.client_name, 'unknown')),
        "Issue Counter: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.issue_counter, 'n/a')),
        "Source App: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.source_app, 'unknown')),
        "Environment: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.environment, 'unknown')),
        "Severity: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.severity, 'error')),
        "Summary: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.summary, 'Auto-fix completed.')),
        "Branch: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.branch_name, 'unknown')),
        "PR URL: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.pr_url, 'n/a')),
        "Issue Hash: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.issue_hash, 'n/a')),
        "Log ID: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.log_id, 'n/a'))
    ];
    if (normalizeText(context === null || context === void 0 ? void 0 : context.notes)) {
        lines.push("Notes: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.notes)));
    }
    if (normalizeText(context === null || context === void 0 ? void 0 : context.error)) {
        lines.push("Error: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.error)));
    }
    return lines.join('\n');
}
function buildGeneratedSlowQuerySummary(stage, context, extra) {
    var collection = normalizeText(context === null || context === void 0 ? void 0 : context.collection, 'a collection');
    var sourceApp = normalizeText(context === null || context === void 0 ? void 0 : context.source_app, 'your app');
    var environment = normalizeText(context === null || context === void 0 ? void 0 : context.environment, 'production');
    var baselineDuration = normalizeDuration(context === null || context === void 0 ? void 0 : context.baseline_duration_ms);
    var afterDuration = normalizeDuration(context === null || context === void 0 ? void 0 : context.after_duration_ms);
    var hasDurations = baselineDuration !== null && afterDuration !== null && baselineDuration > 0;
    var improvementPct = hasDurations
        ? Math.max(0, ((baselineDuration - afterDuration) / baselineDuration) * 100)
        : null;
    if (stage === 'detected_auto_optimize_enabled') {
        return [
            "What happened: We found a slow operation in ".concat(collection, "."),
            "Where: ".concat(sourceApp, " (").concat(environment, ")."),
            'What happens next: Auto-optimization is running and we will send another update when done.'
        ].join('\n');
    }
    if (stage === 'completed_success') {
        return [
            "What happened: We optimized slow performance for ".concat(collection, "."),
            "Where: ".concat(sourceApp, " (").concat(environment, ")."),
            hasDurations
                ? "Estimated improvement: about ".concat((0, common_1.round)(improvementPct, 1), "% faster response time.")
                : 'Estimated improvement: performance is now better than before.',
            'What to do: No action is needed unless you still notice slow behavior.'
        ].join('\n');
    }
    return [
        "What happened: We could not safely complete automatic optimization for ".concat(collection, "."),
        "Where: ".concat(sourceApp, " (").concat(environment, ")."),
        "Why: ".concat((extra === null || extra === void 0 ? void 0 : extra.reason) || (extra === null || extra === void 0 ? void 0 : extra.notes) || (context === null || context === void 0 ? void 0 : context.reason) || (context === null || context === void 0 ? void 0 : context.notes) || 'Manual review is required.'),
        'What to do: Ask the team to review this slow operation manually.'
    ].join('\n');
}
function buildResolveIOProjectSlowQueryDetails(context) {
    var baselineDuration = normalizeDuration(context === null || context === void 0 ? void 0 : context.baseline_duration_ms);
    var afterDuration = normalizeDuration(context === null || context === void 0 ? void 0 : context.after_duration_ms);
    var hasDurations = baselineDuration !== null && afterDuration !== null && baselineDuration > 0;
    var improvementPct = hasDurations
        ? (0, common_1.round)(((baselineDuration - afterDuration) / baselineDuration) * 100, 1)
        : null;
    var runDurations = Array.isArray(context === null || context === void 0 ? void 0 : context.verification_run_durations_ms)
        ? context.verification_run_durations_ms
            .map(function (duration) { return formatDuration(duration); })
            .filter(Boolean)
        : [];
    var lines = [
        'Slow-query optimization completed for a ResolveIO project.',
        '',
        "Collection: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.collection, 'unknown')),
        "Client: ".concat(normalizeText((context === null || context === void 0 ? void 0 : context.client_name) || (context === null || context === void 0 ? void 0 : context.client_slug), 'unknown')),
        "Source App: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.source_app, 'unknown')),
        "Environment: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.environment, 'unknown')),
        "Query Hash: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.query_hash, 'n/a')),
        hasDurations
            ? "Performance Change: ".concat((0, common_1.round)(baselineDuration, 0), " ms -> ").concat((0, common_1.round)(afterDuration, 0), " ms (").concat(improvementPct, "%)")
            : 'Performance Change: available in slow-query logs.',
        "Verification Runs: ".concat(runDurations.join(', ') || 'n/a'),
        "Log ID: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.log_id, 'n/a'))
    ];
    if (normalizeText(context === null || context === void 0 ? void 0 : context.notes)) {
        lines.push("Notes: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.notes)));
    }
    if (normalizeText(context === null || context === void 0 ? void 0 : context.reason)) {
        lines.push("Reason: ".concat(normalizeText(context === null || context === void 0 ? void 0 : context.reason)));
    }
    return lines.join('\n');
}

//# sourceMappingURL=customer-notification-content.manager.js.map
