/* =============================================================
 * App router — hash-based, code-split-ready
 * ============================================================= */

// SEO: per-route page title & meta description.
// Browser tab + Google search snippet update as users navigate.
const PAGE_SEO = {
  home: { title: "Fathers Arise — Restoring Fatherhood. Building Nations.", description: "Every generation needs fathers who stay. Join the movement restoring men to their God-given role across Uganda." },
  about: { title: "About — Fathers Arise", description: "Who we are. Pastor Isaac Mukisa and the Fathers Arise team — building men the world is waiting for, from boyhood to fatherhood to legacy." },
  "be-a-man": { title: "Be A Man — Fathers Arise", description: "Raising a generation of men who know who they are before the world tells them who to be. Schools, universities, and prisons across Uganda." },
  "nurturing-bonds": { title: "Nurturing Bonds — Fathers Arise", description: "Seven programs equipping fathers to be emotionally present, spiritually grounded, and intentionally connected to their families." },
  "mobile-kanaabe": { title: "Mobile Kanaabe — Fathers Arise", description: "Economic empowerment for young men who need more than words — they need work. Skills training and micro-enterprise." },
  forge: { title: "The Forge — Fathers Arise", description: "Forge groups across Uganda — communities of fathers committed to staying present, honest, and in their children's lives." },
  "champion-virtues": { title: "Champion Virtues — Fathers Arise", description: "The 6-pack code. Six commitments. One man. The moral code of every man in the Fathers Arise movement." },
  stories: { title: "Stories — Fathers Arise", description: "Every man in Fathers Arise carries a story. Stories of transformation, healing, and becoming." },
  events: { title: "Events — Fathers Arise", description: "Upcoming Forge gatherings, Be A Man sessions, and community events. Join us in person." },
  donate: { title: "Donate — Fathers Arise", description: "Your gift puts one father in a Forge group. Mobile Money for Uganda, international cards worldwide. Secure payments." },
  "get-involved": { title: "Get Involved — Fathers Arise", description: "Register, volunteer, partner, or give. There is a place for you in the movement." },
  contact: { title: "Contact — Fathers Arise", description: "Get in touch. A real person will reply. WhatsApp is fastest." },
  "team-login": { title: "Team Login — Fathers Arise", description: "Internal OS for Fathers Arise leadership." }
};

function _applyPageSEO(routeKey) {
  const seo = PAGE_SEO[routeKey] || PAGE_SEO.home;
  document.title = seo.title;
  let m = document.querySelector('meta[name="description"]');
  if (m) m.setAttribute("content", seo.description);
  // Also update OG title/desc so freshly-shared links show the right preview
  const ogTitle = document.querySelector('meta[property="og:title"]');
  if (ogTitle) ogTitle.setAttribute("content", seo.title);
  const ogDesc = document.querySelector('meta[property="og:description"]');
  if (ogDesc) ogDesc.setAttribute("content", seo.description);
  // Canonical URL
  const fullPath = routeKey === "home" ? "https://fathersarize.org/" : "https://fathersarize.org/#/" + routeKey;
  let canonical = document.querySelector('link[rel="canonical"]');
  if (canonical) canonical.setAttribute("href", fullPath);

  // Phase 4.2: Fire GA4 page_view on every route change (hash routes don't
  // trigger Google's auto page_view, so we send it explicitly).
  if (typeof window.gtag === "function") {
    window.gtag("event", "page_view", {
      page_title: seo.title,
      page_location: fullPath,
      page_path: routeKey === "home" ? "/" : "/" + routeKey
    });
  }
}

function App() {
  const [route] = useHashRoute();

  // Update title + meta on every route change for SEO + browser tab clarity
  useEffect(() => { _applyPageSEO(route); }, [route]);

  let page;
  switch (route) {
    case "home": page = <HomePage />; break;
    case "about": page = <AboutPage />; break;
    case "be-a-man": page = <BeAManPage />; break;
    case "nurturing-bonds": page = <NurturingBondsPage />; break;
    case "mobile-kanaabe": page = <MobileKanaabePage />; break;
    case "forge": page = <ForgeDashboard />; break;
    case "champion-virtues": page = <ChampionVirtuesPage />; break;
    case "stories": page = <StoriesPage />; break;
    case "events": page = <EventsPage />; break;
    case "donate": page = <DonatePage />; break;
    case "get-involved": page = <GetInvolvedPage />; break;
    case "contact": page = <ContactPage />; break;
    case "team-login": page = <TeamLoginPage />; break;
    default: page = <HomePage />;
  }

  return (
    <div className="min-h-screen flex flex-col">
      <Navbar route={route} />
      <main className="flex-1">{page}</main>
      <Footer />
      <WhatsAppFloat />
    </div>
  );
}

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<App />);
