// Mobile screen content — Dashboard, Suppliers, Hotel Contacts, Air, Contact.
// Reads data exposed on window by the desktop page scripts.

// ---------- shared bits ----------
function MEyebrowTitle({ eyebrow, title, sub }) {
  return (
    <div style={{ marginBottom: 16 }}>
      <div className="rdb-eyebrow" style={{ marginBottom: 10 }}>{eyebrow}</div>
      <h1 style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 30, letterSpacing: "-0.02em", color: "var(--fg-1)", margin: "0 0 8px", lineHeight: 1.05 }}>{title}</h1>
      {sub && (
        <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <span style={{ width: 6, height: 6, borderRadius: "50%", background: "var(--success)" }} />
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 10, color: "var(--ink-6)" }}>{sub}</span>
        </div>
      )}
    </div>
  );
}

function MField({ icon, value, mono, color }) {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
      <Icon name={icon} size={13} color="var(--ink-6)" />
      <span style={{ fontFamily: mono ? "var(--font-mono)" : "var(--font-body)", fontSize: mono ? 12 : 13, color: color || "var(--fg-2)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{value}</span>
    </div>
  );
}

function MSearch({ value, onChange, placeholder }) {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 10, border: "1px solid var(--ink-3)", padding: "11px 14px", marginBottom: 16 }}>
      <Icon name="search" size={16} color="var(--ink-6)" />
      <input value={value} onChange={e => onChange(e.target.value)} placeholder={placeholder} style={{ background: "transparent", border: "none", outline: "none", color: "var(--fg-1)", fontFamily: "var(--font-body)", fontSize: 14, flex: 1 }} />
    </div>
  );
}

function MChips({ items, selected, onSelect }) {
  return (
    <div style={{ display: "flex", gap: 8, overflowX: "auto", paddingBottom: 18, margin: "0 -18px", paddingLeft: 18, paddingRight: 18 }}>
      {items.map(c => {
        const active = c === selected;
        return (
          <span key={c} onClick={() => onSelect(c)} style={{
            flexShrink: 0,
            fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 10, letterSpacing: "0.16em", textTransform: "uppercase",
            padding: "7px 13px",
            border: active ? "1px solid var(--rdb-gold)" : "1px solid var(--ink-3)",
            background: active ? "var(--rdb-gold)" : "transparent",
            color: active ? "#0D0D0D" : "var(--fg-2)", lineHeight: 1, cursor: "pointer",
          }}>{c}</span>
        );
      })}
    </div>
  );
}

// ---------- Dashboard ----------
function MobileDashboard({ user, onNav }) {
  const name = (user && user.name ? user.name.split(" ")[0] : "Amelia");
  const rows = [
    { id: "suppliers", icon: "building-2", title: "Suppliers & Partners", desc: "184 partners · 8 categories" },
    { id: "hotels",    icon: "bed-double", title: "Hotel Contacts",       desc: "Nationwide · always current" },
    { id: "air",       icon: "plane",      title: "Air",                  desc: "Carriers · booking desk" },
    { id: "contact",   icon: "life-buoy",  title: "Contact",              desc: "24 / 7 operations desk" },
  ];
  return (
    <div style={{ padding: "22px 18px 60px" }}>
      <div className="rdb-eyebrow" style={{ marginBottom: 10 }}>RDB Hospitality Group · Miami</div>
      <h1 style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 30, letterSpacing: "-0.02em", color: "var(--fg-1)", margin: "0 0 12px", lineHeight: 1.05 }}>Welcome back, <span data-ms-member-info="firstName">{name}</span>.</h1>
      <p style={{ fontFamily: "var(--font-body)", fontSize: 14, lineHeight: 1.6, color: "var(--fg-2)", margin: "0 0 18px" }}>
        Everything you need to work with RDB — travel suppliers, hotel contacts, and air — in one place.
      </p>

      <div style={{ display: "flex", alignItems: "center", gap: 14, border: "1px solid rgba(201,168,76,0.32)", background: "rgba(201,168,76,0.05)", padding: "12px 14px", marginBottom: 26 }}>
        <Icon name="badge-check" size={16} color="var(--rdb-gold)" />
        <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--fg-2)" }}>
          Backstage Pass · <span style={{ color: "var(--rdb-gold)" }}>Active</span> · Expires May 31
        </span>
      </div>

      <div className="rdb-eyebrow" style={{ color: "var(--ink-6)", marginBottom: 14 }}>Quick access</div>
      <div style={{ display: "flex", flexDirection: "column", gap: 1, background: "var(--ink-3)", border: "1px solid var(--ink-3)" }}>
        {rows.map(r => (
          <a key={r.id} onClick={() => onNav(r.id)} style={{ display: "flex", alignItems: "center", gap: 14, padding: "16px 16px", background: "var(--ink-1)", cursor: "pointer", textDecoration: "none" }}>
            <div style={{ width: 40, height: 40, border: "1px solid var(--ink-4)", display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
              <Icon name={r.icon} size={18} color="var(--rdb-gold)" />
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 16, color: "var(--fg-1)" }}>{r.title}</div>
              <div style={{ fontFamily: "var(--font-mono)", fontSize: 10, color: "var(--ink-6)", marginTop: 3, textTransform: "uppercase", letterSpacing: "0.04em" }}>{r.desc}</div>
            </div>
            <Icon name="arrow-up-right" size={18} color="var(--ink-5)" />
          </a>
        ))}
      </div>
    </div>
  );
}

// ---------- Suppliers (live, read-only from the RDB Google Sheet) ----------
function MobileSuppliers() {
  const { rows, status } = useSuppliers();
  const [search, setSearch] = React.useState("");
  const [cat, setCat] = React.useState("All");
  const cats = React.useMemo(() => supplierCategories(rows), [rows]);
  const q = search.trim().toLowerCase();
  const list = rows.filter(s =>
    (cat === "All" || (s.category || "").split(",").map(c => c.trim()).includes(cat)) &&
    (q === "" || s.company.toLowerCase().includes(q) || (s.category || "").toLowerCase().includes(q) || (s.notes || "").toLowerCase().includes(q))
  );
  return (
    <div style={{ padding: "22px 18px 60px" }}>
      <MEyebrowTitle eyebrow="Directory" title="Suppliers & Partners" />
      <div style={{ marginBottom: 14 }}><SyncBadge status={status} /></div>
      <MSearch value={search} onChange={setSearch} placeholder="Search company, category, or notes" />
      <MChips items={cats} selected={cat} onSelect={setCat} />
      <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
        {list.map((s, i) => <MSupplierCard key={s.company + i} s={s} />)}
      </div>
    </div>
  );
}

function MSupplierCard({ s }) {
  const cats = (s.category || "").split(",").map(c => c.trim()).filter(Boolean);
  return (
    <div style={{ border: "1px solid var(--ink-3)", background: "var(--ink-2)", padding: "16px 16px 18px" }}>
      <div style={{ display: "flex", alignItems: "center", gap: 11, marginBottom: 12 }}>
        <div style={{ width: 34, height: 34, background: "var(--ink-3)", display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 13, color: "var(--rdb-gold)", flexShrink: 0 }}>
          {s.company.split(" ").slice(0, 2).map(w => w[0]).join("")}
        </div>
        <div style={{ minWidth: 0, flex: 1 }}>
          <div style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 14, color: "var(--fg-1)", lineHeight: 1.25 }}>{s.company}</div>
          <div style={{ display: "flex", flexWrap: "wrap", gap: 4, marginTop: 5 }}>
            {cats.map(c => <span key={c} style={{ fontFamily: "var(--font-body)", fontSize: 9, fontWeight: 600, letterSpacing: "0.06em", textTransform: "uppercase", color: "var(--fg-2)", border: "1px solid var(--ink-4)", padding: "2px 6px" }}>{c}</span>)}
          </div>
        </div>
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 8, borderTop: "1px solid var(--ink-3)", paddingTop: 12 }}>
        {s.commission && <MField icon="percent" value={s.commission} />}
        {s.website && <ExtLink href={s.website}>{prettyUrl(s.website)}</ExtLink>}
      </div>
      {s.notes && <div style={{ fontFamily: "var(--font-body)", fontSize: 11.5, lineHeight: 1.5, color: "var(--fg-3)", marginTop: 12 }}>{s.notes}</div>}
    </div>
  );
}

// ---------- Hotels (live, read-only from the RDB Google Sheet) ----------
const MOBILE_HOTELS_BATCH = 25;

function MobileHotels() {
  const { rows, status } = useHotels();
  const [search, setSearch] = React.useState("");
  const [stateF, setStateF] = React.useState("All");
  const [visible, setVisible] = React.useState(MOBILE_HOTELS_BATCH);
  const states = ["All"].concat(Array.from(new Set(rows.map(h => h.state).filter(Boolean))));
  const q = search.trim().toLowerCase();
  const list = rows.filter(h =>
    (stateF === "All" || h.state === stateF) &&
    (q === "" || h.hotel.toLowerCase().includes(q) || h.city.toLowerCase().includes(q) || (h.state || "").toLowerCase().includes(q))
  );
  // Only render `visible` cards — mounting the full national directory at once
  // froze the page. Reset the window whenever the search/filter changes.
  React.useEffect(() => { setVisible(MOBILE_HOTELS_BATCH); }, [q, stateF]);
  const shown = list.slice(0, visible);
  const subText = status === "live" ? rows.length + " properties · live" : status === "loading" ? "syncing…" : "offline sample";
  return (
    <div style={{ padding: "22px 18px 60px" }}>
      <MEyebrowTitle eyebrow="Directory" title="Hotel Contacts" sub={subText} />
      <MSearch value={search} onChange={setSearch} placeholder="Search hotel, city, or state" />
      <MChips items={states} selected={stateF} onSelect={setStateF} />
      <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
        {list.length === 0 && <div style={{ fontFamily: "var(--font-body)", fontSize: 13, color: "var(--ink-6)", padding: "20px 4px" }}>No properties match your search.</div>}
        {shown.map((h, i) => <MHotelCard key={h.hotel + h.city + i} h={h} />)}
      </div>
      {list.length > visible && (
        <button
          onClick={() => setVisible(v => v + MOBILE_HOTELS_BATCH)}
          style={{ width: "100%", marginTop: 16, padding: "13px 16px", background: "transparent", border: "1px solid var(--ink-3)", color: "var(--fg-1)", fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 12, letterSpacing: "0.06em", textTransform: "uppercase", borderRadius: 2, cursor: "pointer" }}
        >
          Load more · {Math.min(MOBILE_HOTELS_BATCH, list.length - visible)} of {list.length - visible} remaining
        </button>
      )}
    </div>
  );
}

function MHotelCard({ h }) {
  return (
    <div style={{ border: "1px solid var(--ink-3)", background: "var(--ink-2)", padding: "16px 16px 18px" }}>
      <div style={{ display: "flex", alignItems: "center", gap: 11, marginBottom: 12 }}>
        <div style={{ width: 34, height: 34, background: "var(--ink-3)", display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
          <Icon name="bed-double" size={16} color="var(--rdb-gold)" />
        </div>
        <div style={{ minWidth: 0 }}>
          <div style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 14, color: "var(--fg-1)", lineHeight: 1.25 }}>{h.hotel}</div>
          <div style={{ fontFamily: "var(--font-body)", fontSize: 11, color: "var(--ink-6)", marginTop: 2 }}>{h.city}</div>
        </div>
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 8, borderTop: "1px solid var(--ink-3)", paddingTop: 12 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <Icon name="mail" size={13} color="var(--ink-6)" />
          {h.email && h.email.indexOf("@") !== -1
            ? <CopyEmail email={h.email} />
            : <span style={{ fontFamily: "var(--font-mono)", fontSize: 12, color: "var(--ink-6)" }}>{h.email || "—"}</span>}
        </div>
        <MField icon="phone" value={h.phone || "—"} mono />
        <MField icon="map-pin" value={h.address || "—"} />
      </div>
    </div>
  );
}

// ---------- Static (Air / Contact) ----------
function MobileStatic({ page }) {
  const c = (window.STATIC_CONTENT && window.STATIC_CONTENT[page]) || {};
  const contacts = c.contacts || (c.contact ? [c.contact] : []);
  return (
    <div style={{ padding: "22px 18px 60px" }}>
      <MEyebrowTitle eyebrow={c.eyebrow} title={c.title} />
      <p style={{ fontFamily: "var(--font-body)", fontSize: 14, lineHeight: 1.6, color: "var(--fg-2)", margin: "0 0 8px" }}>{c.intro}</p>
      <div style={{ height: 1, background: "var(--rdb-gold-soft)", margin: "22px 0 26px" }} />

      <div style={{ display: "flex", flexDirection: "column", gap: 28 }}>
        {(c.sections || []).map((s, i) => (
          <section key={i}>
            <h2 style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 18, color: "var(--fg-1)", margin: "0 0 12px" }}>{s.h}</h2>
            {s.body && <p style={{ fontFamily: "var(--font-body)", fontSize: 14, lineHeight: 1.65, color: "var(--fg-2)", margin: 0 }}>{s.body}</p>}
            {s.list && (
              <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 11 }}>
                {s.list.map((li, j) => (
                  <li key={j} style={{ display: "flex", alignItems: "flex-start", gap: 11, fontFamily: "var(--font-body)", fontSize: 14, lineHeight: 1.5, color: "var(--fg-2)" }}>
                    <span style={{ width: 5, height: 5, borderRadius: "50%", background: "var(--rdb-gold)", marginTop: 8, flexShrink: 0 }} />
                    <span>{li}</span>
                  </li>
                ))}
              </ul>
            )}
          </section>
        ))}

        {c.resources && c.resources.length > 0 && (
          <div style={{ border: "1px solid var(--ink-3)", background: "var(--ink-1)" }}>
            <div style={{ padding: "14px 18px", borderBottom: "1px solid var(--ink-3)", fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 10, letterSpacing: "0.32em", color: "var(--ink-6)", textTransform: "uppercase" }}>Downloads</div>
            {c.resources.map((r, i) => <window.ResourceItem key={i} r={r} last={i === c.resources.length - 1} />)}
          </div>
        )}

        {contacts.map((ct, i) => <window.ContactCard key={i} c={ct} />)}
      </div>
    </div>
  );
}

// ---------- Air (dedicated mobile layout) ----------
function MobileAir() {
  const procedures = window.AIR_PROCEDURES || [];
  const contracts = window.AIR_CONTRACTS || [];
  const ct = window.AIR_CONTACT || {};
  return (
    <div style={{ padding: "22px 18px 60px" }}>
      <MEyebrowTitle eyebrow="Backstage · Travel" title="Air" />
      <p style={{ fontFamily: "var(--font-body)", fontSize: 14, lineHeight: 1.6, color: "var(--fg-2)", margin: "0 0 18px" }}>
        Air runs on carrier-specific rules. Procedures, contracts, and the air desk — all on this page.
      </p>

      {/* Rules callout */}
      <div style={{ borderLeft: "2px solid var(--rdb-gold)", background: "rgba(201,168,76,0.05)", padding: "14px 16px", marginBottom: 26 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 8 }}>
          <Icon name="alert-triangle" size={13} color="var(--rdb-gold)" />
          <span style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 10, letterSpacing: "0.2em", textTransform: "uppercase", color: "var(--rdb-gold)" }}>Carrier rules apply</span>
        </div>
        <p style={{ fontFamily: "var(--font-body)", fontSize: 12.5, lineHeight: 1.6, color: "var(--fg-2)", margin: 0 }}>
          Airline rules override standard supplier protocol — check the carrier's contract before booking.
        </p>
      </div>

      {/* Procedures */}
      <div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 14 }}>
        <h2 style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 18, color: "var(--fg-1)", margin: 0 }}>Booking Procedures</h2>
        <span style={{ fontFamily: "var(--font-mono)", fontSize: 8, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--ink-6)", border: "1px solid var(--ink-3)", padding: "3px 6px" }}>Draft</span>
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 1, background: "var(--ink-3)", border: "1px solid var(--ink-3)", marginBottom: 28 }}>
        {procedures.map(p => (
          <div key={p.n} style={{ display: "flex", gap: 14, background: "var(--ink-1)", padding: "14px 16px" }}>
            <span style={{ fontFamily: "var(--font-mono)", fontSize: 12, color: "var(--rdb-gold)", flexShrink: 0, paddingTop: 2 }}>{p.n}</span>
            <div>
              <div style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 13.5, color: "var(--fg-1)", marginBottom: 4 }}>{p.h}</div>
              <div style={{ fontFamily: "var(--font-body)", fontSize: 12, lineHeight: 1.55, color: "var(--fg-3)" }}>{p.body}</div>
            </div>
          </div>
        ))}
      </div>

      {/* Contracts */}
      <div style={{ border: "1px solid var(--ink-3)", background: "var(--rdb-black, #0D0D0D)", marginBottom: 28 }}>
        <div style={{ padding: "14px 16px", borderBottom: "1px solid var(--ink-3)", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
          <span style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 10, letterSpacing: "0.28em", color: "var(--ink-6)", textTransform: "uppercase" }}>Air Contracts</span>
          <Icon name="pin" size={12} color="var(--rdb-gold)" />
        </div>
        {contracts.map((c, i) => <window.ContractItem key={c.name} c={c} last={i === contracts.length - 1} />)}
      </div>

      {/* Air desk */}
      <div style={{ border: "1px solid var(--ink-3)", padding: "18px 18px 20px" }}>
        <div style={{ fontFamily: "var(--font-body)", fontWeight: 600, fontSize: 10, letterSpacing: "0.28em", color: "var(--rdb-gold)", textTransform: "uppercase", marginBottom: 12 }}>{ct.label}</div>
        <div style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 16, color: "var(--fg-1)", marginBottom: 12 }}>{ct.name}</div>
        <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
          <MField icon="phone" value={ct.phone} mono />
          <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
            <Icon name="mail" size={13} color="var(--ink-6)" />
            <CopyEmail email={ct.email} />
          </div>
          <MField icon="clock" value={ct.hours} />
        </div>
      </div>
    </div>
  );
}

// ---------- Partner Deals (locked) ----------
function MobileLocked() {
  return (
    <div style={{ padding: "80px 32px", display: "flex", flexDirection: "column", alignItems: "center", textAlign: "center" }}>
      <div style={{ width: 56, height: 56, border: "1px solid var(--rdb-gold-soft)", display: "flex", alignItems: "center", justifyContent: "center", marginBottom: 24 }}>
        <Icon name="lock" size={20} color="var(--rdb-gold)" />
      </div>
      <div className="rdb-eyebrow" style={{ marginBottom: 12 }}>Future feature</div>
      <h1 style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 26, color: "var(--fg-1)", margin: "0 0 12px" }}>Partner Deals</h1>
      <p style={{ fontFamily: "var(--font-body)", fontSize: 13.5, lineHeight: 1.6, color: "var(--fg-2)", margin: 0, maxWidth: 280 }}>
        Preferred partner rates and deal structures — in progress.
      </p>
    </div>
  );
}

Object.assign(window, { MobileDashboard, MobileSuppliers, MobileHotels, MobileStatic, MobileAir, MobileLocked });
