/* Stanley Bankruptcy Law — app bootstrap (shared by every route's index.html) */

const { useState, useEffect } = React;

const pageMetadata = {
  home: {
    title: 'Stanley Bankruptcy Law, Eastern Kentucky Chapter 7 & 13',
    description: 'File Chapter 7 bankruptcy for $0 down with Stanley Bankruptcy Law, serving Eastern Kentucky families from Pikeville to Ashland. Free, no-obligation consultation.',
    canonical: 'https://www.stanleybankruptcylaw.com/',
  },
  types: {
    title: 'Chapter 7 vs Chapter 13 Bankruptcy, Stanley Bankruptcy Law',
    description: 'Understand Chapter 7 (liquidation) and Chapter 13 (reorganization) bankruptcy. Which option is right for your situation in Eastern Kentucky?',
    canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-types',
  },
  'chapter-7': {
    title: 'Chapter 7 Bankruptcy Attorney, Eastern Kentucky — $0 Down to File',
    description: 'Chapter 7 bankruptcy wipes out credit cards, medical bills, and personal loans, often in a few months. File for $0 down with a former U.S. Bankruptcy Court clerk. Free consultation.',
    canonical: 'https://www.stanleybankruptcylaw.com/chapter-7',
  },
  'chapter-13': {
    title: 'Chapter 13 Bankruptcy Attorney, Eastern Kentucky — Stop Foreclosure',
    description: 'Chapter 13 bankruptcy stops foreclosure and repossession and reorganizes your debts into one affordable monthly payment over 3 to 5 years. Free consultation across Eastern Kentucky.',
    canonical: 'https://www.stanleybankruptcylaw.com/chapter-13',
  },
  faq: {
    title: 'Bankruptcy FAQ, Common Questions Answered',
    description: 'Honest answers to frequently asked questions about Chapter 7, Chapter 13, credit, foreclosure, and debt relief in Eastern Kentucky.',
    canonical: 'https://www.stanleybankruptcylaw.com/faq',
  },
  blog: {
    title: 'Bankruptcy Blog, Legal Guidance for Eastern Kentucky',
    description: 'Articles on Chapter 7, Chapter 13, foreclosure, credit recovery, and debt relief, written in plain language for Eastern Kentucky residents.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog',
  },
  about: {
    title: 'About Dalton Stanley, Bankruptcy Attorney in Eastern Kentucky',
    description: 'Meet Dalton Stanley: a Pikeville-native bankruptcy attorney with JD + MBA from UK and clerkship experience at the U.S. Bankruptcy Court.',
    canonical: 'https://www.stanleybankruptcylaw.com/about',
  },
  contact: {
    title: 'Contact Stanley Bankruptcy Law, Free Consultation',
    description: 'Schedule your free, no-obligation bankruptcy consultation with Dalton Stanley in Eastern Kentucky. Serving Pikeville, Ashland, and beyond.',
    canonical: 'https://www.stanleybankruptcylaw.com/contact',
  },
  'article-foreclosure': {
    title: 'Can Bankruptcy Stop a Foreclosure in Kentucky?, Stanley Bankruptcy Law',
    description: 'How the automatic stay halts a foreclosure sale and how a Chapter 13 plan helps Eastern Kentucky homeowners keep their home. Plain-language, fully cited to the Bankruptcy Code.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog-stop-foreclosure-kentucky',
    type: 'article',
  },
  'article-discharge': {
    title: 'Which Debts Does Bankruptcy Actually Erase?, Stanley Bankruptcy Law',
    description: 'A statute-by-statute guide to what a Chapter 7 discharge erases, what survives, and the means test, written in plain language and cited to primary law.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog-which-debts-bankruptcy-erases',
    type: 'article',
  },
  'article-garnishment': {
    title: 'Can Bankruptcy Stop a Wage Garnishment in Kentucky?, Stanley Bankruptcy Law',
    description: 'How the automatic stay turns off a wage garnishment, the federal 25% cap under the Consumer Credit Protection Act, and your protection from being fired. Cited to primary law.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog-stop-wage-garnishment-kentucky',
    type: 'article',
  },
  'article-stay': {
    title: "The Automatic Stay Explained, Stanley Bankruptcy Law",
    description: 'What the bankruptcy automatic stay freezes the day you file, how long it lasts, its limits, and the damages for willful violations. Plain language, cited to 11 U.S.C. § 362.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog-automatic-stay-explained',
    type: 'article',
  },
  'article-consult': {
    title: 'What to Bring to Your Free Bankruptcy Consultation, Stanley Bankruptcy Law',
    description: 'A simple checklist of documents and details to gather before your free bankruptcy consultation in Eastern Kentucky. Come as you are.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog-what-to-bring-consultation',
    type: 'article',
  },
  'article-credit': {
    title: 'Rebuilding Your Credit After Bankruptcy, Stanley Bankruptcy Law',
    description: 'A practical, realistic roadmap for rebuilding your credit score in the months after a bankruptcy discharge in Eastern Kentucky.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog-rebuilding-credit-after-bankruptcy',
    type: 'article',
  },
  'article-exemptions': {
    title: 'Will I Lose My Property? Federal Exemptions Explained, Stanley Bankruptcy Law',
    description: 'How bankruptcy exemptions protect your home, car, and belongings, with the current federal amounts and Kentucky’s homestead exemption. Cited to primary law.',
    canonical: 'https://www.stanleybankruptcylaw.com/blog-will-i-lose-my-property-exemptions',
    type: 'article',
  },
  'city-pikeville':    { title: 'Bankruptcy Attorney in Pikeville, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Pikeville and Pike County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-pikeville-ky' },
  'city-hazard':       { title: 'Bankruptcy Attorney in Hazard, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Hazard and Perry County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-hazard-ky' },
  'city-whitesburg':   { title: 'Bankruptcy Attorney in Whitesburg, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Whitesburg and Letcher County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-whitesburg-ky' },
  'city-grayson':      { title: 'Bankruptcy Attorney in Grayson, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Grayson and Carter County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-grayson-ky' },
  'city-salyersville': { title: 'Bankruptcy Attorney in Salyersville, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Salyersville and Magoffin County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-salyersville-ky' },
  'city-prestonsburg': { title: 'Bankruptcy Attorney in Prestonsburg, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Prestonsburg and Floyd County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-prestonsburg-ky' },
  'city-paintsville':  { title: 'Bankruptcy Attorney in Paintsville, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Paintsville and Johnson County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-paintsville-ky' },
  'city-ashland':      { title: 'Bankruptcy Attorney in Ashland, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving Ashland and Boyd County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-ashland-ky' },
  'city-london':       { title: 'Bankruptcy Attorney in London, KY', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving London and Laurel County, KY. Free consultation, $0 down to file.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-london-ky' },
  'city-eastern-kentucky': { title: 'Bankruptcy Attorney in Eastern Kentucky', description: 'Chapter 7 and Chapter 13 bankruptcy attorney serving all of Eastern Kentucky. Free consultation, $0 down to file. Serving Pike, Floyd, Perry, Letcher, Johnson, Boyd, Carter, Magoffin, Laurel and more.', canonical: 'https://www.stanleybankruptcylaw.com/bankruptcy-attorney-eastern-kentucky' },
  'privacy-policy': { title: 'Privacy Policy — Stanley Bankruptcy Law', description: 'Privacy policy for Stanley Bankruptcy Law PLLC, including how we collect, use, and protect your personal information and SMS opt-in data.', canonical: 'https://www.stanleybankruptcylaw.com/privacy-policy' },
  'terms': { title: 'Terms of Service — Stanley Bankruptcy Law', description: 'Terms of service for Stanley Bankruptcy Law PLLC, including text messaging terms, SMS opt-out instructions, and disclaimers.', canonical: 'https://www.stanleybankruptcylaw.com/terms-of-service' },
};

const SITE_ORIGIN = 'https://www.stanleybankruptcylaw.com';
const PAGE_TO_PATH = {};
const PATH_TO_PAGE = {};
Object.keys(pageMetadata).forEach((key) => {
  let path = pageMetadata[key].canonical.replace(SITE_ORIGIN, '');
  if (!path) path = '/';
  PAGE_TO_PATH[key] = path;
  const norm = path.replace(/\/+$/, '') || '/';
  PATH_TO_PAGE[norm] = key;
});
function pageFromPath(pathname) {
  // Strip a trailing /index.html (many hosts show it explicitly) then trailing slashes.
  let p = pathname.replace(/\/index\.html$/i, '/');
  const norm = p.replace(/\/+$/, '') || '/';
  if (PATH_TO_PAGE[norm]) return PATH_TO_PAGE[norm];
  // Fallback: suffix match so directory-prefixed hosts (staging, preview) still resolve.
  const keys = Object.keys(PATH_TO_PAGE).sort((a, b) => b.length - a.length);
  for (const key of keys) {
    if (key !== '/' && norm.endsWith(key)) return PATH_TO_PAGE[key];
  }
  return 'home';
}
window.hrefFor = (key) => PAGE_TO_PATH[key] || '/';

function updatePageMeta(page) {
  const meta = pageMetadata[page] || pageMetadata.home;
  if (document.getElementById('meta-title')) {
    document.getElementById('meta-title').textContent = meta.title;
    document.getElementById('meta-description').content = meta.description;
    document.getElementById('meta-canonical').href = meta.canonical;
    document.getElementById('og-title').content = meta.title;
    document.getElementById('og-description').content = meta.description;
    document.getElementById('og-url').content = meta.canonical;
    document.getElementById('og-type').content = meta.type || 'website';
    document.getElementById('tw-title').content = meta.title;
    document.getElementById('tw-description').content = meta.description;
  }
}

function App() {
  const [page, setPage] = useState(() => pageFromPath(window.location.pathname));
  const [scrolled, setScrolled] = useState(false);

  const navigate = (p) => {
    const path = PAGE_TO_PATH[p] || '/';
    if (window.location.pathname !== path) {
      window.history.pushState({ page: p }, '', path);
    }
    setPage(p);
    window.scrollTo({ top: 0, behavior: 'auto' });
  };

  useEffect(() => {
    const onPop = () => setPage(pageFromPath(window.location.pathname));
    window.addEventListener('popstate', onPop);
    return () => window.removeEventListener('popstate', onPop);
  }, []);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  useEffect(() => {
    updatePageMeta(page);
  }, [page]);

  const pages = {
    home:    window.HomePage,
    types:   window.TypesPage,
    'chapter-7':  window.ChapterSevenPage,
    'chapter-13': window.ChapterThirteenPage,
    faq:     window.FaqPage,
    blog:    window.BlogPage,
    about:   window.AboutPage,
    contact: window.ContactPage,
    'article-foreclosure': window.ArticleForeclosure,
    'article-discharge':   window.ArticleDischarge,
    'article-garnishment': window.ArticleGarnishment,
    'article-stay':        window.ArticleAutomaticStay,
    'article-consult':     window.ArticleConsultPrep,
    'article-credit':      window.ArticleRebuildCredit,
    'article-exemptions':  window.ArticleExemptions,
    'privacy-policy': window.PrivacyPolicyPage,
    'terms':          window.TermsPage,
    ...(window.CityPages || {}),
  };
  const Page = pages[page] || window.HomePage;

  return (
    <React.Fragment>
      <window.SiteHeader page={page} navigate={navigate} scrolled={scrolled} />
      <div className="fade-page" key={page}>
        <Page navigate={navigate} />
      </div>
      <window.SiteFooter navigate={navigate} />
      <window.ChatWidget />
    </React.Fragment>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);
