import { NextRequest, NextResponse } from "next/server";
import { getWelcomeSequence } from "@/actions/email-automation/welcome-sequence";
import { sendWelcomeEmail } from "@/lib/welcome-email-sender";
import type { WelcomeSequenceEntry } from "@/lib/welcome-email-sender";

const BRAND_IDS = [
  { id: "64294306-5f42-463d-a5e8-2ad6c81a96de", name: "Tuxedo Corn" },
  { id: "b1cb7a96-d82b-40b1-80b1-d6dd26c56e28", name: "Indian River Direct" },
];

async function processWelcomeSequencesForBrand(brandId: string): Promise<{ sent: number; failed: number; skipped: number }> {
  let sent = 0, failed = 0, skipped = 0;

  const result = await getWelcomeSequence(brandId);
  if (!result.success) {
    return { sent: 0, failed: 0, skipped: 0 };
  }
  const entries: WelcomeSequenceEntry[] = result.entries;

  const entryOutcomes = await Promise.all(
    entries.map(async (entry): Promise<"sent" | "failed" | "skipped"> => {
      const nextStep = entry.sequence_step + 1;
      if (nextStep > 4) return "skipped";
      if (entry.status === "unsubscribed" || entry.status === "bounced") return "skipped";

      try {
        const r = await sendWelcomeEmail(entry, nextStep);
        return r.success ? "sent" : "failed";
      } catch (e) {
        console.error(`[email-automation/welcome-sequence] entry ${entry.id}:`, e);
        return "failed";
      }
    })
  );
  sent = entryOutcomes.filter((o) => o === "sent").length;
  failed = entryOutcomes.filter((o) => o === "failed").length;
  skipped = entryOutcomes.filter((o) => o === "skipped").length;

  return { sent, failed, skipped };
}

export async function POST(req: NextRequest) {
  const authHeader = req.headers.get("authorization");
  const cronSecret = process.env.CRON_SECRET;

  if (!cronSecret) {
    return NextResponse.json({ error: "Server misconfiguration: CRON_SECRET not set" }, { status: 500 });
  }
  if (authHeader !== `Bearer ${cronSecret}`) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const results: Record<string, { sent: number; failed: number; skipped: number }> = {};

  await Promise.all(
    BRAND_IDS.map(async (brand) => {
      try {
        results[brand.name] = await processWelcomeSequencesForBrand(brand.id);
      } catch (e) {
        console.error(`[email-automation/welcome-sequence] brand ${brand.name}:`, e);
        results[brand.name] = { sent: 0, failed: 0, skipped: 0 };
      }
    })
  );

  const total = Object.values(results).reduce((acc, r) => ({ sent: acc.sent + r.sent, failed: acc.failed + r.failed, skipped: acc.skipped + r.skipped }), { sent: 0, failed: 0, skipped: 0 });

  return NextResponse.json({ ok: true, results, ...total });
}

export async function GET() {
  return NextResponse.json({
    status: "ok",
    path: "/api/email-automation/welcome-sequence",
    brands: BRAND_IDS.map(b => ({ id: b.id, name: b.name })),
    resend_configured: Boolean(process.env.RESEND_API_KEY),
    cron_secret_configured: Boolean(process.env.CRON_SECRET),
  });
}
