/**
 * Daily integration-health alert job.
 *
 * Calls checkIntegrationHealth(), summarizes, and posts an Adaptive Card to
 * morning-summary-webhooks ONLY when something needs attention. Quiet days
 * stay quiet — no spam.
 */

import postgresClient from '@/lib/services/postgres-client';
import {
  checkIntegrationHealth,
  summarize,
  type IntegrationHealth,
  type HealthSummary,
} from '@/lib/services/integration-health';

interface WebhookRow {
  id: number;
  label: string;
  webhook_url: string;
  enabled: boolean;
}

export interface HealthAlertResult {
  summary: HealthSummary;
  items: IntegrationHealth[];
  alertSent: boolean;
  webhooksDelivered: number;
}

function buildHealthAdaptiveCard(items: IntegrationHealth[], summary: HealthSummary): object {
  const failed = items.filter((i) => i.status === 'auth_failed' || i.status === 'unreachable');
  const expired = items.filter(
    (i) => i.tokenExpiry && i.tokenExpiry.daysRemaining <= 0
  );
  const expiringSoon = items.filter(
    (i) => i.tokenExpiry && i.tokenExpiry.daysRemaining > 0 && i.tokenExpiry.daysRemaining <= 14
  );

  const facts: Array<{ title: string; value: string }> = [];
  for (const i of failed) {
    facts.push({
      title: i.name,
      value: `${i.status === 'auth_failed' ? '⚠ AUTH FAILED' : '⚠ UNREACHABLE'} — ${i.error?.slice(0, 120) ?? 'no detail'}`,
    });
  }
  for (const i of expired) {
    facts.push({
      title: i.name,
      value: `🔑 token EXPIRED ${Math.abs(i.tokenExpiry!.daysRemaining).toFixed(0)} days ago (${i.tokenExpiry!.envVar})`,
    });
  }
  for (const i of expiringSoon) {
    facts.push({
      title: i.name,
      value: `🔑 token expires in ${i.tokenExpiry!.daysRemaining.toFixed(0)} days (${i.tokenExpiry!.envVar})`,
    });
  }

  return {
    type: 'AdaptiveCard',
    $schema: 'http://adaptivecards.io/schemas/adaptive-card.json',
    version: '1.4',
    body: [
      {
        type: 'TextBlock',
        size: 'Large',
        weight: 'Bolder',
        text: 'Pulse — Integration Health Alert',
      },
      {
        type: 'TextBlock',
        spacing: 'None',
        isSubtle: true,
        wrap: true,
        text: `${summary.failed} failing · ${summary.expired} expired · ${summary.expiringWithin14Days} expiring within 14 days`,
      },
      {
        type: 'FactSet',
        facts,
      },
      {
        type: 'TextBlock',
        spacing: 'Medium',
        isSubtle: true,
        wrap: true,
        text: `Generated ${new Date().toISOString()}. ${summary.ok}/${summary.total} integrations healthy.`,
      },
    ],
  };
}

async function getEnabledWebhooks(): Promise<WebhookRow[]> {
  const r = await postgresClient.query<WebhookRow>(
    `SELECT id, label, webhook_url, enabled
       FROM morning_summary_webhooks
      WHERE enabled = true`
  );
  return r.rows;
}

async function deliverCard(card: object, webhooks: WebhookRow[]): Promise<number> {
  const envelope = {
    type: 'message',
    attachments: [
      {
        contentType: 'application/vnd.microsoft.card.adaptive',
        contentUrl: null,
        content: card,
      },
    ],
  };
  let delivered = 0;
  await Promise.all(
    webhooks.map(async (w) => {
      try {
        const res = await fetch(w.webhook_url, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify(envelope),
        });
        if (res.ok) delivered += 1;
        else console.warn(`[integration-health-alerts] ${w.label} responded ${res.status}`);
      } catch (err) {
        console.warn(
          `[integration-health-alerts] ${w.label} delivery failed:`,
          err instanceof Error ? err.message : err
        );
      }
    })
  );
  return delivered;
}

export async function runIntegrationHealthAlertJob(): Promise<HealthAlertResult> {
  const items = await checkIntegrationHealth({ skipCache: true });
  const summary = summarize(items);

  if (!summary.hasIssues) {
    return { summary, items, alertSent: false, webhooksDelivered: 0 };
  }

  const webhooks = await getEnabledWebhooks();
  if (webhooks.length === 0) {
    console.log(
      '[integration-health-alerts] issues found but no morning-summary-webhooks configured; skipping delivery'
    );
    return { summary, items, alertSent: false, webhooksDelivered: 0 };
  }

  const card = buildHealthAdaptiveCard(items, summary);
  const delivered = await deliverCard(card, webhooks);
  return {
    summary,
    items,
    alertSent: delivered > 0,
    webhooksDelivered: delivered,
  };
}
