import React from 'react';
import type { AlertPayload } from './types.js';

/** Plain-text body: one section per firing check. */
export function buildAlertText(payload: AlertPayload): string {
  const header =
    payload.kind === 'digest'
      ? '[multideal-ops] daily digest'
      : `[multideal-ops] ${payload.severity.toUpperCase()} alert`;
  const lines = [header, ''];
  for (const r of payload.results) {
    const icon = r.severity === 'page' ? '🔴' : r.severity === 'warn' ? '⚠️' : '✅';
    lines.push(`${icon} ${r.title} [${r.severity}]${r.degraded ? ' (degraded)' : ''}`);
    lines.push(`   ${r.detail}`);
    if (r.metrics) {
      for (const [k, v] of Object.entries(r.metrics)) lines.push(`   - ${k}: ${v}`);
    }
    lines.push('');
  }
  return lines.join('\n');
}

/** React element for Resend (monospace lines, mirrors settlement-monitor buildAlertReactBody). */
export function buildAlertReact(payload: AlertPayload): React.ReactElement {
  const body = buildAlertText(payload);
  return React.createElement(
    'div',
    null,
    body
      .split('\n')
      .map((line, i) =>
        React.createElement(
          'p',
          { key: i, style: { margin: '4px 0', fontFamily: 'monospace' } },
          line,
        ),
      ),
  );
}
