// Builds the self-contained HTML report that the main process prints to PDF. import { SECTION_TITLES } from './auditor-core.js'; const esc = value => String(value ?? '').replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); const STATUS_COLOR = { Compliant: '#1a7f37', 'Non-Compliant': '#c4242f', NotApplicable: '#6e7781', Pending: '#9a6700', Error: '#8250df' }; const RISK_COLOR = { High: '#c4242f', Medium: '#bc4c00', Low: '#9a6700', Unknown: '#8250df', None: '#6e7781' }; const badge = (text, color) => `${esc(text)}`; export function buildReportHtml({ evidence, findings, summary, ai, sha256, model, sources }) { const host = evidence.host; const started = evidence.audit.started_at.replace('T', ' ').slice(0, 19); const counts = evidence.summary; const phone = evidence.source.kind === 'web' || ['ios', 'android'].includes(host.os_family); const EVIDENCE_LABEL = { detected: 'detected', 'self-reported': 'self-reported' }; const sectionHtml = ['baseline', 'gdpr', 'aiact', 'org'].map(sectionId => { const rows = evidence.results.filter(r => r.section === sectionId).map(r => { const f = findings[r.id]; let detail = ''; if (f && r.status !== 'Compliant' && r.status !== 'NotApplicable') { detail = `

${esc(f.finding)}

Risk: ${badge(f.risk.level, RISK_COLOR[f.risk.level] || '#6e7781')} ${esc(f.risk.reason)}

${f.recommendation.length ? `

What to do:

    ${f.recommendation.map(s => `
  1. ${esc(s)}
  2. `).join('')}
` : ''} ${f.verification ? `

How to verify: ${esc(f.verification)}

` : ''}

${f.references.map(esc).join(' · ')}

`; } const source = r.evidence ? `
${esc(EVIDENCE_LABEL[r.evidence])}` : ''; return `${esc(r.name)}${source}${badge(r.status, STATUS_COLOR[r.status])}${esc(r.actual)}${esc(r.expected)}${detail}`; }).join(''); return `

${esc(SECTION_TITLES[sectionId])}

${rows}
CheckStatusFoundRequired
`; }).join(''); const aiHtml = ai ? `

AI TOOLS FOUND — EU AI ACT CLASSIFICATION

${esc(ai.summary)}

${ai.tools.map(t => ``).join('')}
ToolApprovedDefault risk classGDPRAction
${esc(t.tool)}
${esc(t.kind)}
${t.approved ? 'Yes' : 'No'} ${esc(t.default_risk_class)}
High-risk if: ${esc(t.becomes_high_risk_if.join('; '))}
${esc(t.gdpr)}${esc(t.action)}

${esc(ai.log_retention)} The purpose of use was not stated; the classification must be confirmed for each actual use.

` : ''; return `Arx audit — ${esc(host.hostname)}
DEVSEIS ENDPOINT AUDIT · READ-ONLY

${esc(host.hostname)}

Operating system${esc(host.os_name)} ${esc(host.os_version)}${host.build ? ` (build ${esc(host.build)})` : ''} ${phone ? `Device${esc(host.device_model || '')} Browser${esc(host.browser || '')}` : `Serial number${esc(host.serial_number || '')}`} Audit date${esc(started)} ${phone ? `Checked byPhone self-check in the browser: ${evidence.results.filter(r => r.evidence === 'detected').length} results detected, ${evidence.results.filter(r => r.evidence === 'self-reported').length} self-reported` : `Run as${esc(evidence.audit.execution_user)}${evidence.audit.is_admin ? ' (administrator)' : ' (not administrator — some checks could not be read)'}`}
${summary.score.percent}%
${summary.score.compliant} of ${summary.score.assessed} assessed checks compliant
${Object.entries(counts).map(([k, v]) => `${badge(`${v} ${k}`, STATUS_COLOR[k])}`).join(' ')}

EXECUTIVE SUMMARY

${esc(summary.summary)}

${summary.next_steps.length ? `

Next steps

    ${summary.next_steps.map(s => `
  1. ${esc(s)}
  2. `).join('')}
` : ''} ${summary.top_gaps.length ? `

TOP GAPS

` : ''} ${sectionHtml} ${aiHtml}
${phone ? `This report records what the browser detected on this phone and the answers given by its user at the audit date, and compares them with the Devseis baseline. Results marked self-reported were not verified on the device; a browser cannot read a phone's security settings. It supports ISO 27001 internal audits, GDPR Article 32 and EU AI Act deployer duties; it is not a certification and not legal advice.` : `This report records the settings found on this computer at the audit date and compares them with the Devseis baseline. It supports ISO 27001 internal audits, GDPR Article 32 and EU AI Act deployer duties; it is not a certification and not legal advice. Organisational controls reflect the answers given in organisational.conf.`}
Evidence SHA-256: ${esc(sha256)}
Findings written by: ${esc(model || 'built-in report writer')} (${sources.model} by the AI model, ${sources.template} by the built-in writer)
${phone ? 'Phone self-check' : 'Collector'} ${esc(evidence.source.collector)} ${esc(evidence.source.collector_version)} · catalog ${esc(evidence.catalog_version)}
`; }