// survey-send — sends a survey to one or more recipients via email-send.
// Creates survey_sends rows + dispatches branded email with one-click answer link.
import { serve } from "https://deno.land/std@0.190.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";
import { getServiceClient } from '../_shared/supabase-clients.ts';

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
};

interface Recipient {
  email: string;
  name?: string;
  related_entity_type?: string;
  related_entity_id?: string;
  lead_id?: string;
}

interface Body {
  campaign_id: string;
  recipients: Recipient[];
  public_base_url?: string; // optional override (e.g. https://demo.flowwink.com)
}

const NPS_BUTTONS = (token: string, base: string) => {
  const link = (n: number) =>
    `<a href="${base}/s/${token}?score=${n}" style="display:inline-block;width:36px;height:36px;line-height:36px;text-align:center;margin:2px;border:1px solid #d1d5db;border-radius:8px;color:#0f172a;text-decoration:none;font-weight:600;font-family:system-ui,sans-serif">${n}</a>`;
  return Array.from({ length: 11 }, (_, i) => link(i)).join("");
};

const renderHtml = (args: {
  intro: string;
  campaign_name: string;
  token: string;
  base: string;
  kind: string;
  question: string;
}) => {
  const buttons =
    args.kind === "nps"
      ? NPS_BUTTONS(args.token, args.base)
      : `<a href="${args.base}/s/${args.token}" style="background:#0f172a;color:white;padding:12px 24px;border-radius:8px;text-decoration:none;font-family:system-ui,sans-serif">Give feedback</a>`;

  return `<div style="font-family:system-ui,-apple-system,sans-serif;max-width:560px;margin:0 auto;padding:32px 16px;color:#0f172a">
    <h2 style="margin:0 0 16px;font-size:20px">${args.campaign_name}</h2>
    <p style="margin:0 0 16px;color:#475569">${args.intro}</p>
    <p style="margin:0 0 16px;font-weight:600">${args.question}</p>
    <div style="margin:16px 0;text-align:center">${buttons}</div>
    ${
      args.kind === "nps"
        ? `<div style="display:flex;justify-content:space-between;font-size:12px;color:#94a3b8;max-width:430px;margin:0 auto"><span>Not at all likely</span><span>Extremely likely</span></div>`
        : ""
    }
    <p style="margin:32px 0 0;font-size:12px;color:#94a3b8">If you can't see the buttons, <a href="${args.base}/s/${args.token}">open the survey</a>.</p>
  </div>`;
};

serve(async (req) => {
  if (req.method === "OPTIONS") return new Response(null, { headers: corsHeaders });

  try {
    const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
    const SERVICE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
    const supabase = getServiceClient();

    const body: Body = await req.json();
    if (!body.campaign_id || !Array.isArray(body.recipients) || body.recipients.length === 0) {
      return new Response(JSON.stringify({ error: "campaign_id and recipients[] are required" }), {
        status: 400,
        headers: { ...corsHeaders, "Content-Type": "application/json" },
      });
    }

    // Load campaign + template
    const { data: campaign, error: cErr } = await supabase
      .from("survey_campaigns")
      .select("*, survey_templates(*)")
      .eq("id", body.campaign_id)
      .maybeSingle();
    if (cErr || !campaign) {
      return new Response(JSON.stringify({ error: "campaign_not_found" }), {
        status: 404,
        headers: { ...corsHeaders, "Content-Type": "application/json" },
      });
    }
    const template = (campaign as any).survey_templates;
    const primary = (template?.questions ?? []).find((q: any) =>
      q.type === "nps" || q.type === "csat" || q.id === "score"
    );
    const question = primary?.label ?? "How was your experience?";

    // Determine public base URL
    let base = body.public_base_url;
    if (!base) {
      const { data: ss } = await supabase
        .from("site_settings")
        .select("value")
        .eq("key", "branding")
        .maybeSingle();
      base = (ss?.value as any)?.public_url || "";
    }
    if (!base) base = req.headers.get("origin") || "";

    const sends: any[] = [];
    const errors: any[] = [];

    for (const r of body.recipients) {
      // 1) create send row (token auto-generated by default)
      const { data: send, error: sErr } = await supabase
        .from("survey_sends")
        .insert({
          campaign_id: body.campaign_id,
          recipient_email: r.email.toLowerCase().trim(),
          recipient_name: r.name ?? null,
          related_entity_type: r.related_entity_type ?? null,
          related_entity_id: r.related_entity_id ?? null,
          lead_id: r.lead_id ?? null,
        })
        .select()
        .single();

      if (sErr || !send) {
        errors.push({ email: r.email, error: sErr?.message ?? "insert_failed" });
        continue;
      }

      // 2) send email via unified email-send
      const html = renderHtml({
        intro: campaign.email_intro,
        campaign_name: campaign.name,
        token: send.token,
        base,
        kind: template?.kind ?? "nps",
        question,
      });

      const emailRes = await fetch(`${SUPABASE_URL}/functions/v1/email-send`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${SERVICE_KEY}`,
        },
        body: JSON.stringify({
          to: r.email,
          subject: campaign.email_subject,
          html,
          tags: { module: "surveys", campaign_id: body.campaign_id, send_id: send.id },
        }),
      });

      if (!emailRes.ok) {
        const txt = await emailRes.text();
        errors.push({ email: r.email, error: `email_send_failed: ${txt}` });
        continue;
      }

      await supabase.from("survey_sends").update({ sent_at: new Date().toISOString() }).eq("id", send.id);
      sends.push({ id: send.id, email: r.email, token: send.token });
    }

    return new Response(JSON.stringify({ success: true, sends, errors }), {
      status: 200,
      headers: { ...corsHeaders, "Content-Type": "application/json" },
    });
  } catch (e: unknown) {
    const msg = e instanceof Error ? e.message : "Unknown error";
    return new Response(JSON.stringify({ error: msg }), {
      status: 500,
      headers: { ...corsHeaders, "Content-Type": "application/json" },
    });
  }
});
