"""Builds the actual email/SMS/console message bodies for an alert. Kept separate from alert_service.py's dispatch loop so the message format can be tested in isolation. """ from __future__ import annotations from app.core.config import Settings from app.models.alert import Alert from app.models.company import Company from app.models.enums import NotificationType from app.notifications.base import NotificationMessage def build_alert_message( notification_type: NotificationType, destination_value: str, company: Company, alert: Alert, settings: Settings, ) -> NotificationMessage: dashboard_link = f"{settings.frontend_url}/alerts" if notification_type == NotificationType.SMS: text = ( f"CI Alert [{alert.severity.value.upper()}]: {alert.title}. " f"Confidence {alert.confidence:.0%}. View details: {dashboard_link}" ) return NotificationMessage( destination_value=destination_value, subject=alert.title, body_text=text[:480] ) subject = f"[{alert.severity.value.upper()}] {company.name}: {alert.title}" text_lines = [ f"Company: {company.name}", f"Alert: {alert.title}", f"Severity: {alert.severity.value.title()}", f"Confidence: {alert.confidence:.0%}", "", "What changed:", alert.summary, "", "Why it matters:", alert.why_it_matters, "", f"View in dashboard: {dashboard_link}", f"Manage notification preferences: {settings.frontend_url}/settings", ] body_text = "\n".join(text_lines) body_html = ( f"

{subject}

" f"

Company: {company.name}
" f"Severity: {alert.severity.value.title()}
" f"Confidence: {alert.confidence:.0%}

" f"

What changed:
{alert.summary}

" f"

Why it matters:
{alert.why_it_matters}

" f'

View in dashboard

' f'

' f'Manage notification preferences

' ) return NotificationMessage( destination_value=destination_value, subject=subject, body_text=body_text, body_html=body_html, )