import { Navigate, Route, Routes } from 'react-router-dom';
import Layout from './components/Layout';
import { useAuth } from './AuthContext';
import Admin from './pages/Admin';
import CasePage from './pages/CasePage';
import CasesPage from './pages/CasesPage';
import Dashboard from './pages/Dashboard';
import Leaderboard from './pages/Leaderboard';
import Login from './pages/Login';
import Register from './pages/Register';
import BattleHub from './pages/BattleHub';
import BattleRoomPage from './pages/BattleRoomPage';
import ProfilePage from './pages/ProfilePage';
import PlayerProfilePage from './pages/PlayerProfilePage';
import CatalogPage from './pages/CatalogPage';
import PrestigePage from './pages/PrestigePage';
import AchievementsPage from './pages/AchievementsPage';
import ShopPage from './pages/ShopPage';

function HomeRedirect() {
  const { user, loading } = useAuth();
  if (loading) return <p className="muted" style={{ padding: '2rem' }}>Loading…</p>;
  if (!user) return <Navigate to="/login" replace />;
  if (user.role === 'admin') return <Navigate to="/admin" replace />;
  return <Navigate to="/dashboard" replace />;
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<Login />} />
      <Route path="/register" element={<Register />} />
      <Route path="/admin/*" element={<Admin />} />
      <Route element={<Layout />}>
        <Route path="/" element={<HomeRedirect />} />
        <Route path="/dashboard" element={<Dashboard />} />
        <Route path="/cases" element={<CasesPage />} />
        <Route path="/cases/:id" element={<CasePage />} />
        <Route path="/catalog" element={<CatalogPage />} />
        <Route path="/prestige" element={<PrestigePage />} />
        <Route path="/achievements" element={<AchievementsPage />} />
        <Route path="/battle" element={<BattleHub />} />
        <Route path="/battle/create" element={<BattleRoomPage create />} />
        <Route path="/battle/multi" element={<Navigate to="/battle/create" replace />} />
        <Route path="/battle/1vx" element={<Navigate to="/battle" replace />} />
        <Route path="/battle/1vx/:id" element={<Navigate to="/battle" replace />} />
        <Route path="/battle/coinflip" element={<Navigate to="/battle" replace />} />
        <Route path="/battle/:id" element={<BattleRoomPage />} />
        <Route path="/bet" element={<Navigate to="/battle" replace />} />
        <Route path="/bet/multi" element={<Navigate to="/battle/create" replace />} />
        <Route path="/bet/1vx" element={<Navigate to="/battle" replace />} />
        <Route path="/bet/1vx/:id" element={<Navigate to="/battle" replace />} />
        <Route path="/bet/coinflip" element={<Navigate to="/battle" replace />} />
        <Route path="/profile" element={<ProfilePage />} />
        <Route path="/player/:username" element={<PlayerProfilePage />} />
        <Route path="/shop" element={<ShopPage />} />
        <Route path="/leaderboard" element={<Leaderboard />} />
      </Route>
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
  );
}
