import type { SetupConfig } from './config.js';

export function renderOperatorPackStatus(config: SetupConfig): string {
  const automation = config.automation ?? {};
  const backup = config.backup;
  const enabled = Boolean(
    backup?.enabled
    && automation.heartbeat?.enabled
    && automation.daily_briefing?.enabled,
  );

  if (!enabled) {
    return 'Operator Pack: disabled';
  }

  const notify = backup?.notify ?? (automation.heartbeat as { notify?: { channel?: string; peer?: string } } | undefined)?.notify;
  if (notify?.channel && notify?.peer) {
    return `Operator Pack: enabled (${notify.channel}/${notify.peer})`;
  }

  return 'Operator Pack: enabled';
}

export function renderSummary(config: SetupConfig): string {
  const lines: string[] = [];

  const models = config.models ?? {};
  const tiers = ['default', 'fast', 'complex', 'local']
    .filter(t => models[t])
    .map(t => `${t}:${models[t].provider}`)
    .join(', ');
  lines.push(`  Models:     ${tiers || 'none configured'}`);

  const channels: string[] = [];
  if (config.server?.port) {channels.push('webchat');}
  if (config.telegram) {channels.push('telegram');}
  if (config.discord) {channels.push('discord');}
  if (config.slack) {channels.push('slack');}
  if (config.whatsapp) {channels.push('whatsapp');}
  lines.push(`  Channels:   ${channels.join(', ') || 'none'}`);

  const embedding = config.memory?.embedding;
  const memoryStatus = embedding?.enabled ? `vector search (${embedding.provider})` : 'keyword search (no embeddings)';
  lines.push(`  Memory:     ${memoryStatus}`);

  const auto = config.automation ?? {};
  const autoFeatures: string[] = [];
  if (Array.isArray(auto.cron) && auto.cron.length > 0) {autoFeatures.push(`${auto.cron.length} cron jobs`);}
  if (Array.isArray(auto.webhooks) && auto.webhooks.length > 0) {autoFeatures.push('webhooks');}
  if (auto.gmail?.enabled) {autoFeatures.push('gmail');}
  if (auto.gcal?.enabled) {autoFeatures.push('gcal');}
  if (auto.gdocs?.enabled) {autoFeatures.push('gdocs');}
  if (auto.gdrive?.enabled) {autoFeatures.push('gdrive');}
  if (auto.gtasks?.enabled) {autoFeatures.push('gtasks');}
  if (auto.heartbeat?.enabled) {autoFeatures.push('heartbeat');}
  if (auto.daily_briefing?.enabled) {autoFeatures.push('daily-briefing');}
  if (auto.minio_sync?.enabled) {autoFeatures.push('minio-sync');}
  lines.push(`  Automation: ${autoFeatures.join(', ') || 'none'}`);

  const backup = config.backup;
  if (backup?.enabled) {
    const mode = typeof backup.schedule === 'string' && backup.schedule.trim().length > 0
      ? `cron ${backup.schedule}`
      : 'interval';
    lines.push(`  Backup:     enabled (${mode})`);
  } else {
    lines.push('  Backup:     disabled');
  }

  const secFeatures: string[] = [];
  secFeatures.push(`tools:${config.tools?.profile ?? 'full'}`);
  if (config.sandbox?.enabled) {secFeatures.push('sandbox');}
  if (config.pairing?.enabled) {secFeatures.push('pairing');}
  lines.push(`  Security:   ${secFeatures.join(', ')}`);

  const gw: string[] = [];
  gw.push(`port ${config.server?.port ?? 18800}`);
  if (config.server?.token) {gw.push('auth');}
  if (config.server?.lock) {gw.push('locked');}
  if (config.server?.tailscale?.serve) {gw.push('tailscale');}
  lines.push(`  Gateway:    ${gw.join(', ')}`);

  return lines.join('\n');
}
