import { Routes, Route, Navigate } from 'react-router-dom';
import { useAuth } from './context/AuthContext';
import DashboardLayout from './components/Layout/DashboardLayout';
import LoginPage from './pages/auth/LoginPage';
import VerifyOtpPage from './pages/auth/VerifyOtpPage';
import RegisterPage from './pages/auth/RegisterPage';
import CooperationPage from './pages/auth/CooperationPage';
import DashboardPage from './pages/DashboardPage';
import ActiveSignalsPage from './pages/ActiveSignalsPage';
import RecentlyClosedPage from './pages/RecentlyClosedPage';
import HistoryPage from './pages/HistoryPage';
import StatsPage from './pages/StatsPage';
import SignalDetailPage from './pages/SignalDetailPage';
import AccountPage from './pages/AccountPage';
import PaymentsPage from './pages/PaymentsPage';
import SupportPage from './pages/SupportPage';

function PrivateRoute({ children }) {
  const { user, loading } = useAuth();
  if (loading) return null;
  return user ? children : <Navigate to="/login" replace />;
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<LoginPage />} />
      <Route path="/verify-otp" element={<VerifyOtpPage />} />
      <Route path="/register" element={<RegisterPage />} />
      <Route path="/cooperation" element={<CooperationPage />} />

      <Route
        path="/"
        element={
          <PrivateRoute>
            <DashboardLayout />
          </PrivateRoute>
        }
      >
        <Route index element={<Navigate to="/dashboard" replace />} />
        <Route path="dashboard" element={<DashboardPage />} />
        <Route path="signals/active" element={<ActiveSignalsPage />} />
        <Route path="signals/recently-closed" element={<RecentlyClosedPage />} />
        <Route path="signals/history" element={<HistoryPage />} />
        <Route path="signals/:id" element={<SignalDetailPage />} />
        <Route path="stats" element={<StatsPage />} />
        <Route path="account" element={<AccountPage />} />
        <Route path="payments" element={<PaymentsPage />} />
        <Route path="support" element={<SupportPage />} />
      </Route>

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