import { Routes, Route, Navigate, useParams } from "react-router-dom";
import { AuthProvider } from "./lib/auth";
import LandingPage from "./pages/LandingPage";
import AlbumPage from "./pages/AlbumPage";
import AdminLogin from "./admin/AdminLogin";
import AdminGuard from "./admin/AdminGuard";
import AdminLayout from "./admin/AdminLayout";
import StampList from "./admin/StampList";
import StampForm from "./admin/StampForm";
import StampQRCode from "./admin/StampQRCode";
import RuleList from "./admin/RuleList";
import RuleForm from "./admin/RuleForm";
import RedemptionLog from "./admin/RedemptionLog";

function CollectRedirect() {
  const { stampId } = useParams();
  return <Navigate to={`/?stamp=${stampId}`} replace />;
}

export default function App() {
  return (
    <AuthProvider>
      <Routes>
        {/* User-facing mobile H5 */}
        <Route path="/" element={<LandingPage />} />
        <Route path="/album" element={<AlbumPage />} />
        <Route path="/collect/:stampId" element={<CollectRedirect />} />

        {/* Admin panel */}
        <Route path="/admin" element={<AdminLogin />} />
        <Route element={<AdminGuard />}>
          <Route element={<AdminLayout />}>
            <Route path="/admin/stamps" element={<StampList />} />
            <Route path="/admin/stamps/new" element={<StampForm />} />
            <Route path="/admin/stamps/:id/edit" element={<StampForm />} />
            <Route path="/admin/stamps/:id/qrcode" element={<StampQRCode />} />
            <Route path="/admin/rules" element={<RuleList />} />
            <Route path="/admin/rules/new" element={<RuleForm />} />
            <Route path="/admin/rules/:id/edit" element={<RuleForm />} />
            <Route path="/admin/redemptions" element={<RedemptionLog />} />
          </Route>
        </Route>

        <Route path="*" element={<Navigate to="/" replace />} />
      </Routes>
    </AuthProvider>
  );
}
