 import React from 'react'
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import Navbar from './components/Navbar'
import Register from './pages/Register'
import Login from './pages/Login'
import Dashboard from './pages/Dashboard'
import Customer from './pages/Customer'
import Product from './pages/Product'
import Sale from './pages/Sale'
import Reports from './pages/Reports'


const ProtectedRoute = ({ children }) => {
  if (!localStorage.getItem('token')) {
    return <Navigate to="/login" />
  }
  return children
}

function App() {
  return (
    <BrowserRouter>
      <Navbar />
      <Routes>
        <Route path='/' element={<Login />} />
        <Route path='/login' element={<Login />} />
        <Route path='/register' element={<Register />} />

        {/* ALL PROTECTED PAGES WRAPPED */}
        <Route path='/dashboard' element={<ProtectedRoute><Dashboard /></ProtectedRoute>} />
        <Route path='/customer' element={<ProtectedRoute><Customer /></ProtectedRoute>} />
        <Route path='/product' element={<ProtectedRoute><Product /></ProtectedRoute>} />
        <Route path='/sale' element={<ProtectedRoute><Sale /></ProtectedRoute>} />
        <Route path='/reports' element={<ProtectedRoute><Reports /></ProtectedRoute>} />
      </Routes>
    </BrowserRouter>
  )
}
export default App