const { OrderTracker, Badge } = window.BecoDesignSystem_7188f3;

const PAST = [
  { id: "4788", when: "24/07", where: "Pastel do Zé", total: "R$ 31,50" },
  { id: "4712", when: "19/07", where: "Forno da Vila", total: "R$ 78,00" },
  { id: "4680", when: "12/07", where: "Beco Bar", total: "R$ 46,90" }
];

/** Drops down from your name, like a notification shade. Orders live inside it. */
function AccountPanel({ open, name, onClose, activeOrder, subs = [], onPauseSub, onSkipSub, onCancelSub }) {
  const [tab, setTab] = React.useState("pedidos");

  // ação de assinatura no vocabulário mono do sistema
  const subAction = (label, onClick, dashed) => (
    <button key={label} type="button" onClick={onClick} style={{
      background: "transparent", border: (dashed ? "1px dashed " : "1px solid ") + "var(--ink-600)",
      borderRadius: "var(--radius-xs)", padding: "5px 8px", cursor: "pointer",
      color: "var(--ink-300)", fontFamily: "var(--font-mono)", fontWeight: 700,
      fontSize: 8.5, letterSpacing: "0.12em", textTransform: "uppercase"
    }}>{label}</button>
  );
  return (
    <div style={{
      position: "absolute", top: 0, left: 0, right: 0, bottom: 0, zIndex: 420,
      transform: open ? "translateY(0)" : "translateY(-101%)",
      transition: "transform var(--dur-slow) var(--ease-out)",
      background: "var(--black)", borderBottom: "1px solid var(--white)",
      borderBottomLeftRadius: "var(--radius-xl)", borderBottomRightRadius: "var(--radius-xl)",
      display: "flex", flexDirection: "column", padding: "44px 16px 20px"
    }}>
      <div style={{ display: "flex", alignItems: "flex-end", justifyContent: "space-between" }}>
        <div style={{ fontFamily: "var(--font-display)", fontWeight: 900, fontStretch: "125%", textTransform: "uppercase", fontSize: 38, lineHeight: 0.94, letterSpacing: "-0.03em", color: "var(--white)" }}>
          {name}
        </div>
        <button type="button" onClick={onClose} aria-label="Fechar conta"
          style={{ background: "none", border: "none", color: "var(--ink-300)", cursor: "pointer", padding: 4 }}>
          <Icon d={P.x} size={20} />
        </button>
      </div>

      <div style={{ display: "flex", gap: 22, marginTop: 22 }}>
        {[["pedidos", "Pedidos"], ["dados", "Dados"]].map(([v, l]) => (
          <button key={v} type="button" onClick={() => setTab(v)}
            style={{
              background: "none", border: "none", padding: 0, cursor: "pointer",
              fontFamily: "var(--font-display)", fontWeight: 900, fontStretch: "125%", textTransform: "uppercase",
              fontSize: 15, color: "var(--white)", opacity: tab === v ? 1 : 0.3
            }}>{l}</button>
        ))}
      </div>

      <div style={{ flex: 1, overflowY: "auto", marginTop: 20, scrollbarWidth: "none" }}>
        {tab === "pedidos" ? (
          <div style={{ display: "flex", flexDirection: "column", gap: 26 }}>
            {activeOrder && (
              <div style={{ border: "1px solid var(--white)", borderRadius: "var(--radius-lg)", padding: 18 }}>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 14 }}>
                  <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 10, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--ink-300)" }}>
                    #{activeOrder.id} · {activeOrder.where}
                  </span>
                  <Badge tone="success" dot>a caminho</Badge>
                </div>
                <OrderTracker current={activeOrder.step} eta={activeOrder.eta} />
              </div>
            )}
            {/* ASSINATURAS — as fornadas que chegam sozinhas (Feira do Beco) */}
            {subs.length > 0 && (
              <div>
                <div style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 10, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--ink-400)", marginBottom: 10 }}>Assinaturas</div>
                {subs.map(s => {
                  const paused = s.status === "pausada";
                  return (
                    <div key={s.name} style={{ padding: "12px 0", borderTop: "1px solid var(--ink-700)", opacity: paused ? 0.55 : 1 }}>
                      <div style={{ display: "flex", alignItems: "baseline", gap: 10 }}>
                        <span style={{ flex: 1, fontFamily: "var(--font-display)", fontWeight: 900, fontStretch: "125%", textTransform: "uppercase", fontSize: 15, color: "var(--white)" }}>{s.name}</span>
                        <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 9.5, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--ink-300)" }}>{s.cadence}</span>
                      </div>
                      <div style={{ display: "flex", alignItems: "center", gap: 8, marginTop: 7 }}>
                        <span style={{ flex: 1, fontFamily: "var(--font-mono)", fontSize: 9, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--ink-400)" }}>
                          {paused ? "pausada" : s.status === "próxima pulada" ? "próxima pulada · depois " + s.next : "próxima · " + s.next}
                        </span>
                        {subAction(paused ? "retomar" : "pausar", () => onPauseSub && onPauseSub(s.name))}
                        {!paused && subAction(s.status === "próxima pulada" ? "despular" : "pular", () => onSkipSub && onSkipSub(s.name))}
                        {subAction("cancelar", () => onCancelSub && onCancelSub(s.name), true)}
                      </div>
                    </div>
                  );
                })}
              </div>
            )}
            <div>
              <div style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 10, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--ink-400)", marginBottom: 10 }}>Antes</div>
              {PAST.map(o => (
                <div key={o.id} style={{ display: "flex", alignItems: "baseline", gap: 12, padding: "12px 0", borderTop: "1px solid var(--ink-700)" }}>
                  <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--ink-400)", width: 42 }}>{o.when}</span>
                  <span style={{ flex: 1, fontFamily: "var(--font-display)", fontWeight: 900, fontStretch: "125%", textTransform: "uppercase", fontSize: 15, color: "var(--white)" }}>{o.where}</span>
                  <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 12, color: "var(--white)" }}>{o.total}</span>
                </div>
              ))}
            </div>
          </div>
        ) : (
          <div style={{ display: "flex", flexDirection: "column", gap: 2 }}>
            {[["Telefone", "(11) 90000-0000"], ["E-mail", "rafa@beco.com.br"], ["Pagamento", "Pix · chave salva"], ["Endereços", "3 salvos"]].map(([k, v]) => (
              <div key={k} style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", padding: "14px 0", borderBottom: "1px solid var(--ink-700)" }}>
                <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 10, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--ink-400)" }}>{k}</span>
                <span style={{ fontSize: 14, color: "var(--white)" }}>{v}</span>
              </div>
            ))}
            <button type="button" style={{
              marginTop: 22, display: "flex", alignItems: "center", gap: 8, background: "none", border: "none",
              padding: 0, cursor: "pointer", color: "var(--ink-300)",
              fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 11, letterSpacing: "0.12em", textTransform: "uppercase"
            }}><Icon d={P.logout} size={16} /> Sair</button>
          </div>
        )}
      </div>
    </div>
  );
}
Object.assign(window, { AccountPanel });
