import React, { Component, useContext } from 'react';
import './App.css';
import { Routes, Route, Outlet, Navigate } from 'react-router-dom';
import SignIn from './llm-fe/pages/SignIn/SignIn';
import SignUp from './llm-fe/pages/SignUp/SignUp';
import AuthCallback from './llm-fe/pages/AuthCallback/AuthCallback';
import BillingSuccess from './llm-fe/pages/BillingSuccess/BillingSuccess';
import BillingCancel from './llm-fe/pages/BillingCancel/BillingCancel';
import PasswordReset from './llm-fe/pages/PasswordReset/PasswordReset';
import NotFound from './llm-fe/pages/NotFound/NotFound';
import { AuthContext } from './llm-fe/contexts/AuthContext';
import TermsOfService from './llm-fe/pages/TermsOfService/TermsOfService';
import PublicTermsOfServicePage from './llm-fe/pages/TermsOfService/PublicTermsOfServicePage';
import AccountDeletionPage from './llm-fe/pages/AccountDeletion/AccountDeletionPage';
import SetPassword from './llm-fe/components/SetPassword/SetPassword';
import AsyncDashboard2 from './llm-fe/pages/AsyncDashboard2/AsyncDashboard2';
import FeedbackPage2 from './llm-fe/pages/FeedbackPage2/FeedbackPage2';
import DocumentStoragePage from './llm-fe/pages/DocumentStoragePage/DocumentStoragePage';
import Account2 from './llm-fe/pages/Account2/Account2';
import AnalyticsPage from './llm-fe/pages/Analytics/Analytics';
import PasswordResetConfirmation from './llm-fe/pages/PasswordResetConfirmation/PasswordReset';
import GlobalThemeWrapper from './llm-fe/components/GlobalThemeWrapper/GlobalThemeWrapper';
import Tracker from './llm-fe/components/Tracker/Tracker';
import { AnalyticsConsentProvider } from './llm-fe/contexts/AnalyticsConsentContext';
import AnalyticsConsentBanner from './llm-fe/components/AnalyticsConsentBanner/AnalyticsConsentBanner';
import AnalyticsSession from './llm-fe/components/AnalyticsSession/AnalyticsSession';
import AppErrorBoundary from './llm-fe/components/AppErrorBoundary/AppErrorBoundary';
import ToastHost from './llm-fe/components/ToastHost/ToastHost';

const ProtectedRoutes = () => {
  const { authenticated, loading } = useContext(AuthContext);
  if (loading) {
    return (
      <div>Loading</div>
    )
  }
  if (!authenticated) return <Navigate to='/signin/' replace />
  //if(!needsNewPassword) return <Navigate to='/password_reset/' replace/>

  return <Outlet />
}



class App extends Component {

  render() {
    return (
      <GlobalThemeWrapper>
        <AnalyticsConsentProvider>
        <Tracker />
        <AnalyticsSession />
        <ToastHost />
        <AnalyticsConsentBanner />
        <div className='site'>
          <main>
            <div className="main-container">


              <AppErrorBoundary>
              <Routes>
                <Route path='*' element={<NotFound />} />

                <Route path={"/signin/"} Component={SignIn} />
                <Route path={"/signup/"} Component={SignUp} />
                <Route path={"/auth/callback/"} Component={AuthCallback} />
                <Route path={"/billing/success"} Component={BillingSuccess} />
                <Route path={"/billing/cancel"} Component={BillingCancel} />
                <Route path={"/password_reset/"} Component={PasswordReset} />
                <Route path={"/password_reset_confirmation/"} Component={PasswordResetConfirmation} />
                <Route path={'/set_password/'} Component={SetPassword} />
                {/* Public legal pages (also used for post-login ToS acknowledge) */}
                <Route path={"/terms_of_service/"} Component={TermsOfService} />
                <Route path={"/legal/terms-of-service"} Component={PublicTermsOfServicePage} />
                {/* Play Console account-deletion URL (#103) — must stay public */}
                <Route path={"/account-deletion/"} Component={AccountDeletionPage} />

                <Route element={<ProtectedRoutes />}>
                  <Route path={"/"} index={true} Component={AsyncDashboard2} />
                  <Route path={"/account/"} Component={Account2} />
                  <Route path={"/document_storage"} Component={DocumentStoragePage} />
                  <Route path={"/feedback/"} Component={FeedbackPage2} />
                  <Route path={"/analytics/"} Component={AnalyticsPage} />

                </Route>

              </Routes>
              </AppErrorBoundary>

            </div>

          </main>
        </div>
        </AnalyticsConsentProvider>
      </GlobalThemeWrapper>


    );

  }

}

export default App;
