import { Switch, Route } from "wouter";
import { Header } from "@/components/Header";
import Footer from "@/components/Footer";
import Home from "@/pages/Home";
import NotFound from "@/pages/NotFound";
import PrivacyPolicy from "@/pages/PrivacyPolicy";
import TermsOfService from "@/pages/TermsOfService";
import RefundPolicy from "@/pages/RefundPolicy";
import ContactUs from "@/pages/ContactUs";
import Track from "@/pages/Track";
import AuthPage from "@/pages/auth-page";
import ForgotPasswordPage from "@/pages/auth/forgot-password";
import ResetPasswordPage from "@/pages/auth/reset-password";
import CreatePage from "@/pages/app/create";
import AppStyleSelection from "@/pages/app/style-selection";
import AppResults from "@/pages/app/results";
import AppProductType from "@/pages/app/product-type";
import AppPreview from "@/pages/app/preview";
import AppCheckout from "@/pages/app/checkout";
import AppOrderConfirmation from "@/pages/app/order-confirmation";
import SettingsPage from "@/pages/app/settings";
import AdminPage from "@/pages/app/admin";
import AppContact from "@/pages/app/contact";
import AppOrderHistory from "@/pages/app/order-history";
import GalleryPage from "@/pages/app/gallery";
// Removed variant verifier page imports as functionality is now in Admin Dashboard
import { ProtectedRoute } from "@/lib/protected-route";
import { AuthProvider } from "@/hooks/use-auth";
import { CartProvider } from "@/hooks/use-cart";
import { Toaster } from "@/components/ui/toaster";
import { NotFoundError } from "@/components/app/NotFoundError";
import { TooltipProvider } from "@/components/ui/tooltip";
import { CookieConsentBanner } from "@/components/CookieConsent";
import { OfflineBanner } from "@/components/OfflineBanner";

function Router() {
  return (
    <div className="min-h-screen flex flex-col">
      <Switch>
        {/* Guest funnel — no registration wall. Identity is a guest cookie until checkout. */}
        <Route path="/app/create" component={CreatePage} />
        <Route path="/app/style/:sessionId" component={AppStyleSelection} />
        <Route path="/app/results/:sessionId" component={AppResults} />
        <Route path="/app/product-type/:sessionId" component={AppProductType} />
        <Route path="/app/preview/:sessionId" component={AppPreview} />
        <Route path="/app/checkout" component={AppCheckout} />
        <Route path="/app/order-confirmation/:orderHash" component={AppOrderConfirmation} />
        <Route path="/app/contact" component={AppContact} />
        {/* Account-only routes still require login */}
        <ProtectedRoute path="/app/order-history" component={AppOrderHistory} />
        <ProtectedRoute path="/app/gallery" component={GalleryPage} />
        <ProtectedRoute path="/app/settings" component={SettingsPage} />
        <ProtectedRoute path="/app/admin" component={AdminPage} adminOnly />
        <ProtectedRoute path="/app/:rest*" component={() => 
          <NotFoundError 
            title="Page Not Found" 
            message="The page you're looking for doesn't exist within the app." 
          />
        } />
        
        {/* Legacy admin redirect */}
        <Route path="/admin">
          {() => {
            window.location.href = '/app/admin';
            return null;
          }}
        </Route>

        {/* Authentication page - no header/footer */}
        {/* Authentication routes */}
        <Route path="/auth">
          <AuthPage />
        </Route>
        <Route path="/auth/forgot-password">
          <ForgotPasswordPage />
        </Route>
        <Route path="/auth/reset-password/:token?">
          <ResetPasswordPage />
        </Route>
        
        {/* Privacy Policy page */}
        <Route path="/privacy">
          <PrivacyPolicy />
        </Route>
        
        {/* Terms of Service page */}
        <Route path="/terms">
          <TermsOfService />
        </Route>
        
        {/* Refund Policy page */}
        <Route path="/refund-policy">
          <RefundPolicy />
        </Route>
        
        {/* Contact Us page */}
        <Route path="/contact">
          <ContactUs />
        </Route>

        {/* Guest order tracking (order number + email) — no login required */}
        <Route path="/track/:orderHash?">
          <div className="min-h-screen flex flex-col">
            <Header />
            <div className="flex-grow"><Track /></div>
            <Footer />
          </div>
        </Route>

        {/* Public website routes with header and footer */}
        <Route path="/">
          <div className="min-h-screen flex flex-col">
            <Header />
            <div className="flex-grow">
              <Switch>
                <Route path="/" component={Home} />
                <Route component={NotFound} />
              </Switch>
            </div>
            <Footer />
          </div>
        </Route>
        
        {/* Catch-all route for any unmatched paths */}
        <Route component={NotFound} />
      </Switch>
      {/* Toaster is hidden to remove processing popup */}
    </div>
  );
}

function App() {
  return (
    <AuthProvider>
      <CartProvider>
        <TooltipProvider>
          <OfflineBanner />
          <Router />
          <CookieConsentBanner />
        </TooltipProvider>
      </CartProvider>
    </AuthProvider>
  );
}

export default App;
