/* ============================================================
   APP · router + shell
   ============================================================ */
function App() {
  const pageFromLocation = () => {
    if (window.location.pathname.replace(/\/$/, '') === '/docs') return 'docs';
    return (window.location.hash || '#home').replace('#', '') || 'home';
  };

  const [page, setPage] = useState(pageFromLocation);
  const [showMentorFloat, setShowMentorFloat] = useState(false);
  const [showMentorModal, setShowMentorModal] = useState(false);

  useEffect(() => {
    const timer = window.setTimeout(() => setShowMentorFloat(true), 5000);
    return () => window.clearTimeout(timer);
  }, []);

  const go = (p) => {
    setPage(p);
    if (p === 'docs') {
      window.history.pushState(null, '', '/docs');
    } else {
      window.history.pushState(null, '', `/#${p}`);
    }
    window.scrollTo({ top: 0, behavior: 'auto' });
  };

  useEffect(() => {
    const onLocation = () => setPage(pageFromLocation());
    window.addEventListener('hashchange', onLocation);
    window.addEventListener('popstate', onLocation);
    return () => {
      window.removeEventListener('hashchange', onLocation);
      window.removeEventListener('popstate', onLocation);
    };
  }, []);

  const PAGES = {
    home: Home,
    about: AboutPage,
    mentorship: MentorshipPage,
    business: BusinessPage,
    team: TeamPage,
    contacts: ContactsPage,
    donate: DonatePage,
    privacy: PrivacyPage,
    politika: PolicyPage,
    reports: ReportsPage,
    docs: DocsPage,
  };
  const Current = PAGES[page] || NotFoundPage;

  return (
    <NavCtx.Provider value={{ page, go }}>
      <Header />
      <main key={page}><Current /></main>
      <Footer />
      <CookieBanner />
      {showMentorFloat && !showMentorModal && (
        <button className="mentor-float-cta" type="button" onClick={() => setShowMentorModal(true)}>
          <Icon name="users" size={22} />
          <span>Стать наставником</span>
        </button>
      )}
      {showMentorModal && (
        <div className="modal-overlay" onClick={() => setShowMentorModal(false)}>
          <div className="modal" onClick={e => e.stopPropagation()}>
            <button className="modal-close" onClick={() => setShowMentorModal(false)} aria-label="Закрыть">
              <Icon name="close" size={22} />
            </button>
            <ApplicationForm onClose={() => setShowMentorModal(false)} />
          </div>
        </div>
      )}
    </NavCtx.Provider>
  );
}

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