/* global React, fmtMoney */
// =============================================================
// Chat widget — floating bottom-right, opens to full convo flow
// Bilingual, qualifying flow on homepage; pre-loaded car on car page
// =============================================================
const { useState: useChatState, useEffect: useChatEffect, useRef: useChatRef } = React;

const CHAT_TXT = {
  en: {
    open: "Chat",
    title: "Chat with Eddie",
    sub: "Office #2 · Nissan of Omaha",
    typing: "Eddie is typing…",
    placeholder: "Type a message",
    send: "Send",
    home_intro: "Hi! 👋 I'm here to help you find a car at Nissan of Omaha. What brings you in today?",
    quick_intent: ["Under $15K", "SUVs", "Trucks", "New Specials", "Trade-in"],
    ask_budget: "Got a budget in mind? Or a monthly payment range?",
    quick_budget: ["Under $15k", "$15–25k", "$25–35k", "$35k+", "I'll know when I see it"],
    ask_timeline: "When are you hoping to drive something home?",
    quick_timeline: ["This week", "This month", "Just looking"],
    ask_name: "Cool. What name should Eddie text?",
    ask_phone: "And the best number to reach you?",
    ask_time: "What's a good time for him to text you back?",
    quick_time: ["ASAP", "After 5pm", "This weekend"],
    done: "All set! Eddie will text you in 5 minutes. (You'd be lead #L0247.)",
    car_intro: (car) => `Hi! I see you're checking out the ${car.year} ${car.model} ${car.trim}. Want me to lock in ${car.pricing_type === "lease" ? fmtMoney(car.monthly_payment) + "/mo" : car.pricing_type === "apr" ? car.apr_rate + "% APR" : fmtMoney(car.sale_price)} or schedule a quick visit?`,
    car_quick: (car) => [
      car.pricing_type === "lease" ? "Lock the lease rate" : car.pricing_type === "apr" ? "Lock the APR" : "Lock this price",
      "Schedule a visit",
      "Just have a question",
      "Trade-in value",
    ],
    car_done: (car) => `Eddie will text you in 5 minutes about the ${car.model}.`,
  },
  es: {
    open: "Chat",
    title: "Chatea con Eddie",
    sub: "Office #2 · Nissan of Omaha",
    typing: "Eddie está escribiendo…",
    placeholder: "Tírame un mensaje",
    send: "Enviar",
    home_intro: "¡Quihúbole! 👋 Te ayudo a encontrar un carro aquí en Nissan of Omaha. ¿Qué andas buscando?",
    quick_intent: ["Menos de $15K", "SUVs", "Trocas", "Especiales nuevos", "Cambio"],
    ask_budget: "¿Tienes un budget en mente? ¿O un pago mensual?",
    quick_budget: ["Menos de $15k", "$15–25k", "$25–35k", "$35k+", "Cuando lo vea lo sé"],
    ask_timeline: "¿Cuándo te lo quieres llevar?",
    quick_timeline: ["Esta semana", "Este mes", "Nomás viendo"],
    ask_name: "Va. ¿A qué nombre te texteo?",
    ask_phone: "¿Y al mejor número pa' textearte?",
    ask_time: "¿A qué hora te cae bien que te texte?",
    quick_time: ["Ahora", "Después de las 5pm", "Este fin"],
    done: "¡Va! Eddie te texta en 5 minutos. (Serías el lead #L0247.)",
    car_intro: (car) => `¡Quihúbole! Veo que andas viendo el ${car.year} ${car.model} ${car.trim}. ¿Te aparto ${car.pricing_type === "lease" ? fmtMoney(car.monthly_payment) + "/mes" : car.pricing_type === "apr" ? car.apr_rate + "% APR" : fmtMoney(car.sale_price)} o te agendo una visita?`,
    car_quick: (car) => [
      car.pricing_type === "lease" ? "Aparta la renta" : car.pricing_type === "apr" ? "Aparta el APR" : "Aparta el precio",
      "Aparta visita",
      "Tengo una pregunta",
      "¿Cuánto vale el mío?",
    ],
    car_done: (car) => `Eddie te texta en 5 minutos sobre el ${car.model}.`,
  },
};

function ChatWidget({ lang, open, onOpen, onClose, vehicle }) {
  const txt = CHAT_TXT[lang] || CHAT_TXT.en;
  const [messages, setMessages] = useChatState([]);
  const [typed, setTyped] = useChatState("");
  const [typing, setTyping] = useChatState(false);
  const [leadSubmitted, setLeadSubmitted] = useChatState(false);
  const scrollRef = useChatRef(null);

  // Seed opening message when widget opens or context changes
  useChatEffect(() => {
    if (!open) return;
    setLeadSubmitted(false);
    if (vehicle) {
      setMessages([{ from: "bot", text: txt.car_intro(vehicle), quick: txt.car_quick(vehicle) }]);
    } else {
      setMessages([{ from: "bot", text: txt.home_intro, quick: txt.quick_intent }]);
    }
  }, [open, vehicle, lang]);

  useChatEffect(() => {
    if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
  }, [messages, typing, open]);

  const submitLead = (lead, allMessages) => {
    if (!window.showroom || leadSubmitted) return;
    setLeadSubmitted(true);
    const payload = {
      name: lead.name || null,
      phone: lead.phone || null,
      source: vehicle ? "chat_car" : "chat_home",
      intent: lead.intent || (vehicle ? "car_inquiry" : null),
      budget: lead.budget || null,
      timeline: lead.timeline || null,
      vehicle_id: vehicle && vehicle.id && /^[0-9a-f-]{36}$/i.test(vehicle.id) ? vehicle.id : null,
      vehicle_label: vehicle ? `${vehicle.year} ${vehicle.make} ${vehicle.model} ${vehicle.trim || ""}`.trim() : null,
      transcript: allMessages,
      status: "new",
    };
    window.showroom.insertLead(payload).catch((err) => console.error("[chat] lead insert failed:", err));
  };

  const handle = async (val) => {
    const userMsg = { from: "user", text: val };
    const updatedMessages = [...messages, userMsg];
    setMessages(updatedMessages);
    setTyped("");
    setTyping(true);

    // Convert to API format (bot → assistant)
    const apiMessages = updatedMessages
      .filter((m) => m.from === "user" || m.from === "bot")
      .map((m) => ({ role: m.from === "user" ? "user" : "assistant", content: m.text }));

    try {
      const res = await fetch("/api/chat", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ messages: apiMessages, vehicle: vehicle || null, lang }),
      });
      const data = await res.json();
      setTyping(false);
      const botMsg = { from: "bot", text: data.message || (lang === "es" ? "Un momento…" : "One moment…") };
      const finalMessages = [...updatedMessages, botMsg];
      setMessages(finalMessages);
      if (data.lead && data.lead.name && data.lead.phone) {
        submitLead(data.lead, finalMessages);
      }
    } catch {
      setTyping(false);
      setMessages((m) => [...m, { from: "bot", text: lang === "es" ? "Algo salió mal. Mándale texto a Eddie directamente." : "Something went wrong. Text Eddie directly." }]);
    }
  };

  if (!open) {
    return (
      <button onClick={onOpen} aria-label="Open chat" style={{
        position: "fixed", right: 24, bottom: 24, zIndex: 60,
        background: "#0A1020", color: "#E5EAF2",
        border: "1.5px solid var(--ink)", borderRadius: 999,
        padding: "14px 22px",
        fontFamily: "var(--sans)", fontWeight: 600, fontSize: 14,
        cursor: "pointer",
        boxShadow: "var(--shadow-card-sm)",
        display: "flex", alignItems: "center", gap: 10,
      }}>
        <span style={{ width: 8, height: 8, borderRadius: "50%", background: "#5fd66e", boxShadow: "0 0 0 3px rgba(95,214,110,0.25)" }} />
        {txt.open}
      </button>
    );
  }

  return (
    <div style={{
      position: "fixed", right: 24, bottom: 24, zIndex: 60,
      width: 360, maxWidth: "calc(100vw - 32px)",
      height: 540, maxHeight: "calc(100vh - 48px)",
      background: "var(--paper)",
      border: "1.5px solid var(--ink)", borderRadius: 8,
      boxShadow: "8px 8px 0 var(--ink)",
      display: "flex", flexDirection: "column", overflow: "hidden",
    }}>
      <div style={{ padding: "14px 16px", borderBottom: "1.5px solid var(--ink)", background: "#0A1020", color: "#E5EAF2", display: "flex", alignItems: "center", gap: 10 }}>
        <div style={{ width: 32, height: 32, borderRadius: "50%", background: "var(--rust)", display: "grid", placeItems: "center", fontFamily: "var(--serif)", fontStyle: "italic", fontWeight: 500 }}>e</div>
        <div style={{ flex: 1 }}>
          <div className="serif" style={{ fontSize: 16, lineHeight: 1.1, fontWeight: 500 }}>{txt.title}</div>
          <div className="mono" style={{ fontSize: 10, letterSpacing: "0.1em", color: "rgba(238,241,245,0.7)", textTransform: "uppercase" }}>{txt.sub}</div>
        </div>
        <button onClick={onClose} aria-label="Close" style={{ background: "transparent", border: 0, color: "var(--paper)", cursor: "pointer", fontSize: 18, padding: 4 }}>✕</button>
      </div>
      <div ref={scrollRef} style={{ flex: 1, padding: 14, overflowY: "auto", display: "flex", flexDirection: "column", gap: 10 }}>
        {messages.map((m, i) => (
          <div key={i} style={{ display: "flex", flexDirection: "column", alignItems: m.from === "user" ? "flex-end" : "flex-start", gap: 8 }}>
            <div style={{
              maxWidth: "82%",
              padding: "10px 14px",
              borderRadius: m.from === "user" ? "14px 14px 2px 14px" : "14px 14px 14px 2px",
              background: m.from === "user" ? "var(--ink)" : "var(--paper-2)",
              color: m.from === "user" ? "var(--paper)" : "var(--ink)",
              fontSize: 14, lineHeight: 1.4,
              border: "1.5px solid var(--ink)",
            }}>{m.text}</div>
            {m.quick && i === messages.length - 1 && (
              <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
                {m.quick.map((q) => (
                  <button key={q} onClick={() => handle(q)} className="chip" style={{ cursor: "pointer", border: "1.5px solid var(--ink)" }}>{q}</button>
                ))}
              </div>
            )}
          </div>
        ))}
        {typing && (
          <div style={{ alignSelf: "flex-start", fontSize: 12, color: "var(--ink-3)", fontStyle: "italic" }}>{txt.typing}</div>
        )}
      </div>
      <div style={{ padding: "8px 12px", borderTop: "1.5px solid var(--ink)", fontSize: 10, lineHeight: 1.45, color: "var(--ink-3)" }}>
        {lang === "es"
          ? "Al continuar aceptas recibir SMS de Eddie Ponce (Rep. de Ventas, Nissan of Omaha) al número que compartas. STOP para darte de baja. Pueden aplicar cargos."
          : "By continuing you agree to receive SMS from Eddie Ponce (Sales Rep, Nissan of Omaha) at the number you share. Reply STOP to opt out. Msg & data rates may apply."}
      </div>
      <form onSubmit={(e) => { e.preventDefault(); if (typed.trim()) handle(typed.trim()); }}
        style={{ padding: 12, borderTop: "1.5px solid var(--ink)", display: "flex", gap: 8 }}>
        <input value={typed} onChange={(e) => setTyped(e.target.value)} placeholder={txt.placeholder} style={{ flex: 1 }} />
        <button type="submit" className="btn btn-primary btn-sm">{txt.send}</button>
      </form>
    </div>
  );
}
window.ChatWidget = ChatWidget;
