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 ArticlePage from "./pages/ArticlePage";
import MusicPage from "./pages/MusicPage";
import VideoPage from "./pages/VideoPage";
import AdminLogin from "./admin/AdminLogin";
import AdminGuard from "./admin/AdminGuard";
import AdminLayout from "./admin/AdminLayout";
import Dashboard from "./admin/Dashboard";
import StampList from "./admin/StampList";
import ArticleList from "./admin/ArticleList";
import MusicList from "./admin/MusicList";
import UsersList from "./admin/UsersList";
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 />} />
        <Route path="/article/:id" element={<ArticlePage />} />
        <Route path="/music/:id" element={<MusicPage />} />
        <Route path="/video/:id" element={<VideoPage />} />

        {/* Admin panel */}
        <Route path="/admin" element={<AdminLogin />} />
        <Route element={<AdminGuard />}>
          <Route element={<AdminLayout />}>
            <Route path="/admin/dashboard" element={<Dashboard />} />
            <Route path="/admin/stamps" element={<StampList />} />
            <Route path="/admin/articles" element={<ArticleList />} />
            <Route path="/admin/music" element={<MusicList />} />
            <Route path="/admin/users" element={<UsersList />} />
            <Route path="/admin/redemptions" element={<RedemptionLog />} />
          </Route>
        </Route>

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