import React, { useState, useEffect } from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import Login from './pages/Login';
import Beranda from './pages/Beranda';
import Prospek from './pages/Prospek';
import InputProspek from './pages/InputProspek';
import Penawaran from './pages/Penawaran';
import Activities from './pages/Activities';
import Transaksi from './pages/Transaksi';
import DetailAktivitas from './pages/DetailAktivitas';
import DetailSPK from './pages/DetailSPK';
import FollowUp from './pages/FollowUp';
import AskDora from './pages/AskDora';
import KonfirmasiDeal from './pages/KonfirmasiDeal';
import BuatPenawaran from './pages/BuatPenawaran';
import Tracking from './pages/Tracking';
import DetailMonitoring from './pages/DetailMonitoring';
import Profil from './pages/Profil';

function App() {
  const [isLoggedIn, setIsLoggedIn] = useState(false);

  useEffect(() => {
    const loggedIn = localStorage.getItem('isLoggedIn') === 'true';
    setIsLoggedIn(loggedIn);
  }, []);

  const ProtectedRoute = ({ children }) => {
    return isLoggedIn ? children : <Navigate to="/login" />;
  };

  return (
    <Router>
      <div className="min-h-screen bg-gray-50">
        <Routes>
          <Route path="/login" element={isLoggedIn ? <Navigate to="/" /> : <Login />} />
          <Route path="/" element={<ProtectedRoute><Beranda /></ProtectedRoute>} />
          <Route path="/prospek" element={<ProtectedRoute><Prospek /></ProtectedRoute>} />
          <Route path="/prospek/input" element={<ProtectedRoute><InputProspek /></ProtectedRoute>} />
          <Route path="/penawaran" element={<ProtectedRoute><Penawaran /></ProtectedRoute>} />
          <Route path="/activities" element={<ProtectedRoute><Activities /></ProtectedRoute>} />
          <Route path="/transaksi" element={<ProtectedRoute><Transaksi /></ProtectedRoute>} />
          <Route path="/detail-aktivitas" element={<ProtectedRoute><DetailAktivitas /></ProtectedRoute>} />
          <Route path="/detail-spk" element={<ProtectedRoute><DetailSPK /></ProtectedRoute>} />
          <Route path="/follow-up" element={<ProtectedRoute><FollowUp /></ProtectedRoute>} />
          <Route path="/ask-dora" element={<ProtectedRoute><AskDora /></ProtectedRoute>} />
          <Route path="/konfirmasi-deal" element={<ProtectedRoute><KonfirmasiDeal /></ProtectedRoute>} />
          <Route path="/buat-penawaran" element={<ProtectedRoute><BuatPenawaran /></ProtectedRoute>} />
          <Route path="/tracking" element={<ProtectedRoute><Tracking /></ProtectedRoute>} />
          <Route path="/detail-monitoring" element={<ProtectedRoute><DetailMonitoring /></ProtectedRoute>} />
          <Route path="/profil" element={<ProtectedRoute><Profil /></ProtectedRoute>} />
        </Routes>
      </div>
    </Router>
  );
}

export default App;
