/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
export function renderReport(data, format) {
    if (format === 'html') {
        return renderHtml(data);
    }
    return renderMarkdown(data);
}
// ─── HTML rendering ─────────────────────────────────────────────────────────
function renderHtml(data) {
    const formattedDate = new Date(data.generatedAt).toLocaleString('en-US', {
        year: 'numeric',
        month: 'short',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
    });
    const sectionsHtml = data.sections.map(renderSectionHtml).join('\n');
    return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>${escHtml(data.title)}</title>
<style>
  :root {
    --color-critical:#c23934;--color-high:#dd7a01;--color-medium:#0176d3;
    --color-low:#2e844a;--color-info:#706e6b;
    --bg:#f3f2f2;--white:#fff;--border:#dddbda;--text:#3e3e3c;
  }
  *{box-sizing:border-box;margin:0;padding:0}
  body{font-family:'Salesforce Sans',Arial,sans-serif;background:var(--bg);color:var(--text);font-size:14px;line-height:1.5}
  header{background:var(--white);border-bottom:1px solid var(--border);padding:20px 24px}
  header h1{font-size:22px;font-weight:700;margin-bottom:4px}
  .meta{font-size:12px;color:var(--color-info)}
  main{padding:20px 24px;max-width:960px;margin:0 auto}
  .section{background:var(--white);border:1px solid var(--border);border-radius:8px;padding:20px;margin-bottom:16px}
  .section h2{font-size:16px;font-weight:600;margin-bottom:12px;padding-bottom:8px;border-bottom:1px solid var(--border)}
  .finding{padding:10px 12px;margin-bottom:8px;border-left:4px solid var(--border);border-radius:4px;background:var(--bg)}
  .finding-critical{border-left-color:var(--color-critical)}
  .finding-high{border-left-color:var(--color-high)}
  .finding-medium{border-left-color:var(--color-medium)}
  .finding-low{border-left-color:var(--color-low)}
  .finding-info{border-left-color:var(--color-info)}
  .finding h3{font-size:14px;font-weight:600;margin-bottom:4px}
  .severity{display:inline-block;padding:1px 6px;border-radius:10px;font-size:11px;font-weight:600;text-transform:uppercase;color:var(--white)}
  .severity-critical{background:var(--color-critical)}
  .severity-high{background:var(--color-high)}
  .severity-medium{background:var(--color-medium)}
  .severity-low{background:var(--color-low)}
  .severity-info{background:var(--color-info)}
  .metrics-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(180px,1fr));gap:12px}
  .metric-card{background:var(--bg);border-radius:6px;padding:12px;text-align:center}
  .metric-value{font-size:24px;font-weight:700;color:var(--color-medium)}
  .metric-label{font-size:12px;color:var(--color-info);margin-top:2px}
  .recommendation{padding:10px 12px;margin-bottom:8px;background:var(--bg);border-radius:4px}
  .recommendation h4{font-size:13px;font-weight:600;margin-bottom:4px}
  .prose{margin-top:8px}
  footer{text-align:center;padding:16px;font-size:11px;color:var(--color-info)}
</style>
</head>
<body>
<header>
  <h1>${escHtml(data.title)}</h1>
  <div class="meta">
    Generated: ${escHtml(formattedDate)}${data.taskId ? ` | Task: ${escHtml(data.taskId)}` : ''}${data.author ? ` | Author: ${escHtml(data.author)}` : ''}
  </div>
</header>
<main>
${sectionsHtml}
</main>
<footer>Generated by setup-agents report renderer</footer>
</body>
</html>`;
}
function renderSectionHtml(section) {
    const parts = [];
    parts.push('<div class="section">');
    parts.push(`  <h2>${escHtml(section.title)}</h2>`);
    if (section.metrics && section.metrics.length > 0) {
        parts.push('  <div class="metrics-grid">');
        for (const metric of section.metrics) {
            const displayValue = typeof metric.value === 'number' ? metric.value.toString() : metric.value;
            const unit = metric.unit ? ` ${escHtml(metric.unit)}` : '';
            const trend = metric.trend ? ` (${metric.trend === 'up' ? '↑' : metric.trend === 'down' ? '↓' : '→'})` : '';
            parts.push('    <div class="metric-card">');
            parts.push(`      <div class="metric-value">${escHtml(displayValue)}${unit}${trend}</div>`);
            parts.push(`      <div class="metric-label">${escHtml(metric.label)}</div>`);
            parts.push('    </div>');
        }
        parts.push('  </div>');
    }
    if (section.findings && section.findings.length > 0) {
        for (const finding of section.findings) {
            parts.push(`  <div class="finding finding-${finding.severity}">`);
            parts.push(`    <h3><span class="severity severity-${finding.severity}">${finding.severity}</span> ${escHtml(finding.title)}</h3>`);
            parts.push(`    <p>${escHtml(finding.description)}</p>`);
            if (finding.location) {
                parts.push(`    <p><strong>Location:</strong> ${escHtml(finding.location)}</p>`);
            }
            if (finding.evidence) {
                parts.push(`    <p><strong>Evidence:</strong> ${escHtml(finding.evidence)}</p>`);
            }
            parts.push('  </div>');
        }
    }
    if (section.recommendations && section.recommendations.length > 0) {
        for (const rec of section.recommendations) {
            const effort = rec.effort ? ` [${rec.effort}]` : '';
            parts.push('  <div class="recommendation">');
            parts.push(`    <h4><span class="severity severity-${rec.priority}">${rec.priority}</span> ${escHtml(rec.title)}${effort}</h4>`);
            parts.push(`    <p>${escHtml(rec.description)}</p>`);
            parts.push('  </div>');
        }
    }
    if (section.prose) {
        parts.push(`  <div class="prose">${escHtml(section.prose)}</div>`);
    }
    parts.push('</div>');
    return parts.join('\n');
}
// ─── Markdown rendering ─────────────────────────────────────────────────────
function renderMarkdown(data) {
    const lines = [];
    lines.push(`# ${data.title}`);
    lines.push('');
    const meta = [];
    meta.push(`**Generated:** ${data.generatedAt}`);
    if (data.taskId)
        meta.push(`**Task:** ${data.taskId}`);
    if (data.author)
        meta.push(`**Author:** ${data.author}`);
    lines.push(meta.join(' | '));
    lines.push('');
    lines.push('---');
    lines.push('');
    for (const section of data.sections) {
        lines.push(...renderSectionMarkdown(section));
        lines.push('');
    }
    return lines.join('\n');
}
function renderSectionMarkdown(section) {
    const lines = [];
    lines.push(`## ${section.title}`);
    lines.push('');
    if (section.metrics && section.metrics.length > 0) {
        lines.push('| Metric | Value |');
        lines.push('|--------|-------|');
        for (const metric of section.metrics) {
            const displayValue = typeof metric.value === 'number' ? metric.value.toString() : metric.value;
            const unit = metric.unit ? ` ${metric.unit}` : '';
            const trend = metric.trend ? ` (${metric.trend === 'up' ? '↑' : metric.trend === 'down' ? '↓' : '→'})` : '';
            lines.push(`| ${metric.label} | ${displayValue}${unit}${trend} |`);
        }
        lines.push('');
    }
    if (section.findings && section.findings.length > 0) {
        lines.push('### Findings');
        lines.push('');
        for (const finding of section.findings) {
            const severity = finding.severity.toUpperCase();
            lines.push(`- **[${severity}]** ${finding.title}`);
            lines.push(`  ${finding.description}`);
            if (finding.location)
                lines.push(`  - Location: \`${finding.location}\``);
            if (finding.evidence)
                lines.push(`  - Evidence: ${finding.evidence}`);
        }
        lines.push('');
    }
    if (section.recommendations && section.recommendations.length > 0) {
        lines.push('### Recommendations');
        lines.push('');
        for (const rec of section.recommendations) {
            const priority = rec.priority.toUpperCase();
            const effort = rec.effort ? ` [effort: ${rec.effort}]` : '';
            lines.push(`- **[${priority}]** ${rec.title}${effort}`);
            lines.push(`  ${rec.description}`);
        }
        lines.push('');
    }
    if (section.prose) {
        lines.push(section.prose);
        lines.push('');
    }
    return lines;
}
function escHtml(s) {
    return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}
//# sourceMappingURL=report-renderer.js.map