import { GroupedUnitReport, VehicleReport } from "./reports"; export function generateHTMLReport(units: GroupedUnitReport[], startDate: Date, endDate: Date): string { const unitsWithReports = units.filter(unit => unit.vehicles.some( v => v.saloonReports > 0 || v.cabReports > 0 ) ); const body = unitsWithReports.map(renderUnitSection).join('\n'); return wrapHtml(body, startDate, endDate); } function renderUnitSection(unit: GroupedUnitReport): string { const total = unit.vehicles.length; return `

${unit.unitNumber}

${unit.vehicles.map((v, i) => renderCoach(v, i, total)).join('\n')}
${renderComments(unit.comments)}
`; } function renderCoach(vehicle: VehicleReport, index: number, total: number): string { const cabMarkerHTML = vehicle.hasCabZone ? `
${vehicle.cabReports}
` : ''; const cabAbove = index === 0; const cabBelow = index === total -1; return `
${vehicle.vehicleNumber}
${cabAbove ? cabMarkerHTML : ''}
${vehicle.saloonReports}
${cabBelow && !cabAbove ? cabMarkerHTML : ''}
`; } function renderComments(comments: string[]): string { const renderedComments = comments.length ? comments.map(c => `

${c}

`).join('\n') : '

No comments submitted.

'; return `

Comments

${renderedComments}
`; } function wrapHtml(content: string, startDate: Date, endDate: Date): string { const formatDate = (d: Date) => d.toISOString().split('T')[0]; return ` TrACreport - Report Output

TrACreport

Report period: ${formatDate(startDate)} - ${formatDate(endDate)}

Total reports received for each vehicle in the West fleet

158 cabs are not recorded as cab cooling is disabled for Guards

${content} ` }