/* Backentra — Core pipeline page. The one path everything hangs off: a lead becomes a
   scheduled job and the job becomes an invoice, without retyping. Content mirrors
   guidelines/features/core-pipeline.card.html so the site and the design system agree.
   window.BackentraPipeline */
const CP = window.BackentraDesignSystem_93cf3a;
const { Button: CBtn, Icon: CIcon } = CP;
const ccontainer = { maxWidth: "var(--container-max)", margin: "0 auto", padding: "0 32px" };
const ceyebrow = { fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 11, letterSpacing: ".08em", textTransform: "uppercase", color: "var(--text-muted)" };

const CP_STAGES = [
  { n: 1, icon: "target", title: "Lead", tag: ["Can arrive automatically", "accent"],
    body: "Someone gets in touch. A phone call, a form, a Local Services lead, a referral, or an office member typing it in. Where it came from is captured now, so nobody reconstructs it later.",
    caught: ["Contact and property address", "What they are asking for", "Where the lead came from", "Who owns it"] },
  { n: 2, icon: "calendar-check", title: "Estimate appointment", tag: ["Skippable", "info"],
    body: "Someone goes out to look. The appointment lands on the calendar against the lead, assigned to an estimator, and the client is told. Work priced from a photo or a phone call skips this entirely.",
    caught: ["Date, time and who is going", "Travel and the surrounding day", "Site notes and photos"] },
  { n: 3, icon: "file-signature", title: "Proposal and approval", tag: null,
    body: "The work gets priced and sent for signature. Line items come off the service catalogue, tax applies, and the client opens it on their phone, reads the scope and terms, and signs in the portal. Approval is what turns priced work into real work.",
    caught: ["Line items, quantities, rates", "Materials and labour", "Scope, terms, deposit", "Signature and timestamp"] },
  { n: 4, icon: "calendar-clock", title: "Scheduled job", tag: ["Assisted", "accent"],
    body: "Approved work lands in the schedule queue and gets a date and a crew. Your own scheduling rules are applied here, so a recommended date arrives with the reasoning attached rather than as a black box.",
    caught: ["Date, window, duration", "Crew and equipment assigned", "Route position for the day"] },
  { n: 5, icon: "receipt", title: "Invoice sent", tag: null,
    body: "The invoice is built from the approved proposal plus whatever changed on site, and goes out. No one rebuilds it from a notepad, because the priced work and the recorded work are already on the record.",
    caught: ["Amount, terms, due date", "What changed from the proposal", "Sent, viewed, chased"] },
  { n: 6, icon: "circle-check", title: "Payment received", tag: null, done: true,
    body: "Paid by card or ACH, reconciled against the invoice. This is where the record closes, and where job costing becomes real: revenue against the hours and materials the work actually used.",
    caught: ["Amount and method", "Date received", "True job cost against revenue"] },
];
const CP_CARRY = ["client", "property address", "contact preferences", "source", "scope of work", "line items", "price", "photos", "notes", "assigned crew", "hours", "materials", "signature"];
const CP_WHERE = [
  ["Lead", "Full intake and assignment", "Quick intake", "Request work"],
  ["Estimate appointment", "Book, reschedule, assign", "See it, drive to it", "See the booked time"],
  ["Proposal and approval", "Price, send, track", "Build and send on site", "Read and sign"],
  ["Scheduled job", "Queue, rules, crews", "See the day", "See the date"],
  ["Invoice sent", "Build, send, chase", "Send from the field", "Receive and read"],
  ["Payment received", "Reconcile and report", "Take payment on site", "Pay it"],
];
/* Each pipeline stage, and the actions that carry the record through it. */
const CP_MAP = [
  ["Lead", ["Collect the lead"]],
  ["Estimate appointment", ["Book the estimate appointment", "Go to the appointment"]],
  ["Proposal and approval", ["Submit the proposal", "Receive the approval"]],
  ["Scheduled job", ["Schedule the job", "Complete the job"]],
  ["Invoice sent", ["Send out the invoice"]],
  ["Payment received", ["Collect the payment"]],
];
const CP_TAG = { accent: ["var(--orange-50)", "var(--orange-700)"], info: ["var(--info-soft)", "var(--info)"] };

function PipelinePage({ onNav, onJoin }) {
  React.useEffect(() => {
    window.BackentraSetMeta && window.BackentraSetMeta({
      path: "/how-it-works",
      title: "How It Works — Backentra",
      description: "A lead becomes a scheduled job and the job becomes an invoice, without anyone retyping it. The six stages that carry a job from first call to paid.",
    });
  }, []);
  return (
    <div style={{ background: "var(--surface-card)", minHeight: "100vh" }}>
      <window.BackentraSiteHeader current="pipeline" onNav={onNav} />

      <section style={{ background: "var(--gray-100)", padding: "56px 0 48px" }}>
        <div style={{ ...ccontainer, maxWidth: 760, textAlign: "center" }}>
          <div style={{ ...ceyebrow, marginBottom: 12 }}>Features · Core pipeline</div>
          <h1 style={{ fontSize: 46, lineHeight: 1.05, margin: "0 0 16px" }}>One record, from the first call to paid.</h1>
          <p style={{ fontSize: 17.5, lineHeight: 1.6, color: "var(--text-body)", margin: "0 auto 26px", maxWidth: 640 }}>
            A lead becomes a scheduled job, and the job becomes an invoice, without anyone retyping it. That is the whole idea, and everything else in Backentra hangs off it.
          </p>
          <CBtn variant="primary" size="lg" onClick={onJoin} iconRight={<CIcon name="arrow-right" size={20} />}>Join the list</CBtn>
        </div>
      </section>

      {/* the spine */}
      <section style={{ padding: "56px 0", background: "var(--surface-card)" }}>
        <div style={{ ...ccontainer, maxWidth: 900 }}>
          <div style={{ marginBottom: 30 }}>
            <div style={{ ...ceyebrow, marginBottom: 12 }}>The six stages</div>
            <h2 style={{ fontSize: 32, margin: "0 0 10px" }}>How the work moves.</h2>
            <p style={{ fontSize: 15.5, lineHeight: 1.6, color: "var(--text-muted)", margin: 0, maxWidth: 620 }}>
              Not every job goes through all six. Small work is often priced without a site visit, and repeat work goes straight to scheduling. It is a spine, not a gate.
            </p>
          </div>
          {CP_STAGES.map((s, i) => (
            <div key={s.n} style={{ display: "grid", gridTemplateColumns: "54px minmax(0,1fr)", gap: 20 }}>
              <div style={{ display: "flex", flexDirection: "column", alignItems: "center" }}>
                <span style={{ width: 40, height: 40, flex: "none", borderRadius: "var(--radius-pill)", background: s.done ? "var(--success)" : "var(--navy-900)", color: "#fff", display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--font-display)", fontWeight: 800, fontSize: 15 }}>{s.n}</span>
                {i < CP_STAGES.length - 1 && <span style={{ flex: 1, width: 2, background: "var(--border)", margin: "6px 0" }} />}
              </div>
              <div style={{ paddingBottom: i < CP_STAGES.length - 1 ? 28 : 0, minWidth: 0 }}>
                <div style={{ display: "flex", alignItems: "center", gap: 11, flexWrap: "wrap", marginBottom: 8 }}>
                  <CIcon name={s.icon} size={19} color="var(--accent-text)" />
                  <h3 style={{ fontSize: 21, margin: 0 }}>{s.title}</h3>
                  {s.tag && <span style={{ display: "inline-flex", alignItems: "center", height: 22, padding: "0 10px", borderRadius: "var(--radius-pill)", background: CP_TAG[s.tag[1]][0], color: CP_TAG[s.tag[1]][1], fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 10.5 }}>{s.tag[0]}</span>}
                </div>
                <p style={{ fontSize: 15.5, lineHeight: 1.6, color: "var(--text-body)", margin: "0 0 14px" }}>{s.body}</p>
                <div style={{ display: "flex", flexWrap: "wrap", gap: 8 }}>
                  {s.caught.map((c) => (
                    <span key={c} style={{ display: "inline-flex", alignItems: "center", gap: 7, padding: "6px 12px", borderRadius: "var(--radius-md)", background: "var(--gray-50)", border: "1px solid var(--border)", fontSize: 13, color: "var(--text-body)" }}>
                      <CIcon name="check" size={13} color="var(--accent-text)" /> {c}
                    </span>
                  ))}
                </div>
              </div>
            </div>
          ))}
        </div>
      </section>

      {/* pipeline beside workflow */}
      <section style={{ padding: "0 0 56px", background: "var(--surface-card)" }}>
        <div style={{ ...ccontainer, maxWidth: 900 }}>
          <div style={{ marginBottom: 22 }}>
            <div style={{ ...ceyebrow, marginBottom: 12 }}>Pipeline and workflow</div>
            <h2 style={{ fontSize: 30, margin: "0 0 10px" }}>Two different things, side by side.</h2>
            <p style={{ fontSize: 15.5, lineHeight: 1.6, color: "var(--text-muted)", margin: 0, maxWidth: 620 }}>
              A pipeline stage is a state the record sits in. A workflow step is an action someone takes to move it. Reporting runs on the left column. Daily work happens in the right one.
            </p>
          </div>
          <div style={{ border: "1px solid var(--border)", borderRadius: "var(--radius-lg)", overflow: "hidden" }}>
            <div style={{ display: "grid", gridTemplateColumns: "minmax(0,1fr) minmax(0,1.15fr)", background: "var(--gray-50)", borderBottom: "1px solid var(--border)" }}>
              <div style={{ ...ceyebrow, fontSize: 10, padding: "12px 18px", color: "var(--accent-text)" }}>Pipeline stage · a state</div>
              <div style={{ ...ceyebrow, fontSize: 10, padding: "12px 18px", borderLeft: "1px solid var(--border)" }}>Workflow · what someone does</div>
            </div>
            {CP_MAP.map(([stage, steps], i) => (
              <div key={stage} style={{ display: "grid", gridTemplateColumns: "minmax(0,1fr) minmax(0,1.15fr)", borderTop: i ? "1px solid var(--border)" : "none" }}>
                <div style={{ padding: "15px 18px", display: "flex", alignItems: "center", gap: 11, background: "var(--orange-50)" }}>
                  <span style={{ width: 26, height: 26, flex: "none", borderRadius: "var(--radius-pill)", background: i === CP_MAP.length - 1 ? "var(--success)" : "var(--navy-900)", color: "#fff", display: "inline-flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--font-display)", fontWeight: 800, fontSize: 12 }}>{i + 1}</span>
                  <span style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 15, color: "var(--text-strong)" }}>{stage}</span>
                </div>
                <div style={{ padding: "15px 18px", borderLeft: "1px solid var(--border)", display: "flex", flexDirection: "column", gap: 8 }}>
                  {steps.map((st) => (
                    <div key={st} style={{ display: "flex", alignItems: "center", gap: 9, fontSize: 14.5, color: "var(--text-body)" }}>
                      <CIcon name="arrow-right" size={14} color="var(--text-subtle)" style={{ flex: "none" }} /> {st}
                    </div>
                  ))}
                </div>
              </div>
            ))}
          </div>
          <p style={{ fontSize: 13.5, lineHeight: 1.6, color: "var(--text-muted)", margin: "14px 0 0" }}>
            Nine actions, six states. Two stages take two actions to clear, which is exactly where work stalls and why the two are worth tracking separately.
          </p>
        </div>
      </section>

      {/* what carries */}
      <section style={{ padding: "56px 0", background: "var(--navy-900)" }}>
        <div style={{ ...ccontainer, maxWidth: 860, textAlign: "center" }}>
          <div style={{ ...ceyebrow, color: "var(--accent-text)", marginBottom: 12 }}>Entered once</div>
          <h2 style={{ color: "#fff", fontSize: 32, margin: "0 0 12px" }}>Nobody retypes any of this.</h2>
          <p style={{ color: "var(--gray-300)", fontSize: 16, lineHeight: 1.6, margin: "0 auto 26px", maxWidth: 600 }}>
            Each of these is captured at the earliest stage that knows it, and carries the rest of the way. That is the specific thing that makes the pipeline worth having.
          </p>
          <div style={{ display: "flex", flexWrap: "wrap", gap: 9, justifyContent: "center" }}>
            {CP_CARRY.map((c) => (
              <span key={c} style={{ display: "inline-flex", alignItems: "center", height: 32, padding: "0 14px", borderRadius: "var(--radius-pill)", background: "rgba(255,255,255,0.08)", border: "1px solid var(--border-inverse)", color: "#fff", fontFamily: "var(--font-mono)", fontSize: 12.5 }}>{c}</span>
            ))}
          </div>
        </div>
      </section>

      {/* where each stage lives */}
      <section style={{ padding: "56px 0", background: "var(--surface-card)" }}>
        <div style={ccontainer}>
          <div style={{ maxWidth: 620, marginBottom: 26 }}>
            <div style={{ ...ceyebrow, marginBottom: 12 }}>Across the surfaces</div>
            <h2 style={{ fontSize: 32, margin: "0 0 10px" }}>The same pipeline, wherever you are.</h2>
            <p style={{ fontSize: 15.5, lineHeight: 1.6, color: "var(--text-muted)", margin: 0 }}>
              The office does everything. The field app is deliberately narrower, built for the day of work. Clients see only their own side.
            </p>
          </div>
          <div style={{ overflowX: "auto", border: "1px solid var(--border)", borderRadius: "var(--radius-lg)" }}>
            <table style={{ width: "100%", borderCollapse: "collapse", minWidth: 720 }}>
              <thead>
                <tr>{["Stage", "Web app", "Mobile app", "Client portal"].map((h) => (
                  <th key={h} style={{ ...ceyebrow, fontSize: 10, textAlign: "left", padding: "12px 16px", background: "var(--gray-50)", borderBottom: "1px solid var(--border)" }}>{h}</th>
                ))}</tr>
              </thead>
              <tbody>
                {CP_WHERE.map((row, i) => (
                  <tr key={row[0]}>
                    <td style={{ padding: "11px 16px", borderTop: i ? "1px solid var(--border)" : "none", fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 13.5, color: "var(--text-strong)", whiteSpace: "nowrap" }}>{row[0]}</td>
                    {row.slice(1).map((v, j) => <td key={j} style={{ padding: "11px 16px", borderTop: i ? "1px solid var(--border)" : "none", fontSize: 13.5, color: "var(--text-body)" }}>{v}</td>)}
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      </section>

      {/* honest note + cta */}
      <section style={{ padding: "0 0 72px", background: "var(--surface-card)" }}>
        <div style={ccontainer}>
          <div style={{ display: "flex", gap: 13, padding: "18px 22px", borderRadius: "var(--radius-lg)", background: "var(--warning-soft)", border: "1px dashed var(--warning)", marginBottom: 26 }}>
            <CIcon name="triangle-alert" size={18} color="var(--warning)" style={{ flex: "none", marginTop: 1 }} />
            <div style={{ minWidth: 0 }}>
              <div style={{ fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 14.5, color: "var(--text-strong)", marginBottom: 5 }}>Still being built</div>
              <p style={{ margin: 0, fontSize: 13.5, lineHeight: 1.6, color: "var(--text-body)" }}>
                This is the pipeline we are building, not a product you can use today. Stages, screens and behavior are all subject to change before launch, and we will say plainly what is ready when we get there.
              </p>
            </div>
          </div>
          <div style={{ background: "var(--accent-solid)", borderRadius: "var(--radius-2xl)", padding: "48px 44px", display: "flex", alignItems: "center", justifyContent: "space-between", gap: 32, flexWrap: "wrap" }}>
            <div>
              <h2 style={{ color: "#fff", fontSize: 30, margin: "0 0 8px" }}>Want to see it when it's ready?</h2>
              <p style={{ color: "rgba(255,255,255,0.9)", fontSize: 16, margin: 0 }}>Founding members get walked through it before anyone else.</p>
            </div>
            <CBtn variant="secondary" size="lg" onClick={onJoin} iconRight={<CIcon name="arrow-right" size={20} />}>Join the list</CBtn>
          </div>
        </div>
      </section>

      <window.BackentraSiteFooter onNav={onNav} />
    </div>
  );
}

window.BackentraPipeline = PipelinePage;
