// Inner pages: Treatments, Programs, Contact (TypeScript).
// Loaded as a global script — no import/export. Published on window at the end.

const TreatmentsPage: React.FC<PageProps> = ({ onBook }) => {
  const { categories, treatments } = window.RIO3_DATA;
  const [cat, setCat] = React.useState("all");

  const groups =
    cat === "all"
      ? categories.map((c) => ({ cat: c, list: treatments.filter((t) => t.cat === c.id) }))
      : categories
          .filter((c) => c.id === cat)
          .map((c) => ({ cat: c, list: treatments.filter((t) => t.cat === c.id) }));

  return (
    <>
      <section className="page-head" aria-label="Treatments overview">
        <div className="shell">
          <div className="page-head-grid">
            <div>
              <span className="eyebrow">Menu of care</span>
              <h1>Every therapy. <em>Every detail.</em></h1>
              <p className="lede">
                The full menu of {treatments.length} treatments offered at Rio3.
                Filter by category, or call us and we&apos;ll match the right
                protocol at your consultation.
              </p>
            </div>
            <div className="page-head-side">
              <div className="page-meta">
                <div className="row"><span className="label">Treatments</span><span>{treatments.length} on the menu</span></div>
                <div className="row"><span className="label">Categories</span><span>{categories.length}</span></div>
                <div className="row"><span className="label">New patients</span><span>Intake required before therapy</span></div>
              </div>
              <button className="btn btn-primary" onClick={() => onBook && onBook("treatment")}>
                Book a visit <Arrow />
              </button>
            </div>
          </div>
        </div>
      </section>

      <section className="page-body" aria-label="Treatment list">
        <div className="shell">
          <div className="filter-row" role="group" aria-label="Filter by category">
            <button
              className={`filter-pill ${cat === "all" ? "active" : ""}`}
              onClick={() => setCat("all")}
            >
              All <span className="fp-count">{treatments.length}</span>
            </button>
            {categories.map((c) => (
              <button
                key={c.id}
                className={`filter-pill ${cat === c.id ? "active" : ""}`}
                onClick={() => setCat(c.id)}
              >
                {c.label} <span className="fp-count">{treatments.filter((t) => t.cat === c.id).length}</span>
              </button>
            ))}
          </div>

          {groups.map((g) => (
            <div key={g.cat.id} className="menu-group">
              <div className="menu-group-head">
                <h2>{g.cat.label}</h2>
                <span className="mg-count">{g.list.length} treatment{g.list.length === 1 ? "" : "s"}</span>
              </div>
              <div className="menu-rows">
                {g.list.map((t, i) => (
                  <article key={t.id} className="menu-row">
                    <span className="mr-idx">{String(i + 1).padStart(2, "0")}</span>
                    <div className="mr-main">
                      <h3>{t.name}</h3>
                      <p className="mr-summary">{t.summary}</p>
                      <ul className="mr-includes">
                        {t.includes.map((inc) => <li key={inc}>{inc}</li>)}
                      </ul>
                    </div>
                    <div className="mr-side">
                      <span className="mr-duration">{t.duration}</span>
                    </div>
                  </article>
                ))}
              </div>
            </div>
          ))}

          <div className="menu-note">
            <p>
              All treatment courses begin with a first appointment — a comprehensive
              intake with vitals, history and a protocol plan.
            </p>
            <button className="btn btn-ghost" onClick={() => onBook && onBook("consultation")}>
              Call to book <Arrow />
            </button>
          </div>
        </div>
      </section>
    </>
  );
};

const ProgramsPage: React.FC<PageProps> = ({ onConsult }) => {
  const { programs } = window.RIO3_DATA;
  return (
    <>
      <section className="page-head" aria-label="Programs overview">
        <div className="shell">
          <div className="page-head-grid">
            <div>
              <span className="eyebrow">Programs</span>
              <h1>Structured care, <em>start to finish.</em></h1>
              <p className="lede">
                Three clinician-guided program tracks — each a complete course of care
                with a clear protocol, timeline and measurable outcome. Not single
                sessions; a committed plan.
              </p>
            </div>
            <div className="page-head-side">
              <div className="page-meta">
                <div className="row"><span className="label">Tracks</span><span>{programs.length} program tracks</span></div>
                <div className="row"><span className="label">Duration</span><span>12 weeks — 12 months</span></div>
                <div className="row"><span className="label">Oversight</span><span>Clinician-guided throughout</span></div>
                <div className="row"><span className="label">Pricing</span><span>Contact us for pricing</span></div>
              </div>
              <button className="btn btn-primary" onClick={() => onConsult && onConsult()}>
                Request a consultation <Arrow />
              </button>
            </div>
          </div>
        </div>
      </section>

      <section className="page-body programs-band programs-page" aria-label="Program tracks">
        <div className="shell">
          {programs.map((prog, pi) => (
            <div key={prog.id} className="prog-track">
              <div className="prog-track-header">
                <div>
                  <span className="pt-index">{String(pi + 1).padStart(2, "0")} / {String(programs.length).padStart(2, "0")}</span>
                  <h2 className="prog-track-name">{prog.name}</h2>
                  <div className="prog-track-meta">{prog.tagline} · {prog.duration}</div>
                </div>
                <p className="prog-track-desc">{prog.description}</p>
              </div>

              <div className="tier-grid">
                {prog.tiers.map((t) => (
                  <div key={t.id} className={`tier ${t.featured ? "featured" : ""}`}>
                    {t.featured && <span className="badge">Recommended</span>}
                    {t.label && <div className="tier-label">{t.label}</div>}
                    <div className="tier-name">{t.name}</div>
                    <div className="tier-tag">{t.tag}</div>
                    <ul className="tier-feat">
                      {t.features.map((f) => <li key={f}>{f}</li>)}
                    </ul>
                  </div>
                ))}
              </div>

              <div className="prog-track-cta">
                <button className="btn btn-light" onClick={() => onConsult && onConsult()}>
                  Request a consultation <Arrow />
                </button>
                <span className="ptc-note">Contact us for pricing — every program is tailored to you.</span>
              </div>
            </div>
          ))}
        </div>
      </section>
    </>
  );
};

// Kept as an alias so the "memberships" page id keeps working.
const MembershipsPage: React.FC<PageProps> = (props) => <ProgramsPage {...props} />;

const ContactPage: React.FC<PageProps> = ({ onBook }) => {
  const { brand, hours } = window.RIO3_DATA;
  return (
    <>
      <section className="page-head" aria-label="Visit Rio3">
        <div className="shell">
          <div className="page-head-grid">
            <div>
              <span className="eyebrow">Visit Rio3</span>
              <h1>Find us on <em>Federal Highway.</em></h1>
              <p className="lede">
                A short drive from Boca Raton, Pompano Beach and Fort Lauderdale.
                Free parking in the building lot — Suite 205, second floor.
              </p>
              <div className="page-head-cta">
                <a className="btn btn-primary" href={`tel:${brand.phone}`}>Call {brand.phone}</a>
              </div>
            </div>
            <div className="page-head-side">
              <div className="page-meta">
                <div className="row"><span className="label">Address</span><span>{brand.address}, {brand.city}</span></div>
                <div className="row"><span className="label">Phone</span><span>{brand.phone}</span></div>
                <div className="row"><span className="label">Email</span><span>{brand.email}</span></div>
                <div className="row"><span className="label">Parking</span><span>Free, on-site</span></div>
                <div className="row"><span className="label">Languages</span><span>EN · PT · ES</span></div>
              </div>
            </div>
          </div>
        </div>
      </section>

      <section className="page-body" aria-label="Map and hours">
        <div className="shell">
          <div className="location-grid">
            <div>
              <div className="map-frame">
                <MapPlaceholder />
              </div>
              <AppleMapsLink />
            </div>
            <div className="location-info">
              <div className="loc-address">
                <div className="loc-street">{brand.address}</div>
                <div className="loc-city">{brand.city}</div>
                <div className="loc-contacts">
                  <a href={`tel:${brand.phone}`}>{brand.phone}</a>
                  <a href={`mailto:${brand.email}`}>{brand.email}</a>
                </div>
              </div>
              <h2 className="hours-title">Hours</h2>
              <ul className="hours">
                {hours.map((h) => (
                  <li key={h.day} className={h.closed ? "closed" : ""}>
                    <span className="day">{h.day}</span>
                    <span>{h.open}</span>
                  </li>
                ))}
              </ul>
            </div>
          </div>
        </div>
      </section>
    </>
  );
};

Object.assign(window, { TreatmentsPage, ProgramsPage, MembershipsPage, ContactPage });
