import { Routes, Route, Navigate } from 'react-router-dom'
import { useAuthStore } from '@/stores/authStore'
import { AppShell } from '@/components/layout/AppShell'
import { LoginPage } from '@/pages/LoginPage'
import { DashboardPage } from '@/pages/DashboardPage'
import { BriefingPage } from '@/pages/BriefingPage'
import { ProfilePage } from '@/pages/ProfilePage'
import { CRMPage } from '@/pages/CRMPage'
import { SalesPage } from '@/pages/SalesPage'
import { FinancePage } from '@/pages/FinancePage'
import { HRPage } from '@/pages/HRPage'
import { LegalPage } from '@/pages/LegalPage'
import { MarketingPage } from '@/pages/MarketingPage'
import { SupportPage } from '@/pages/SupportPage'
import { AutomationPage } from '@/pages/AutomationPage'

function ProtectedRoute({ children }: { children: React.ReactNode }) {
  const { isAuthenticated } = useAuthStore()
  return isAuthenticated ? <>{children}</> : <Navigate to="/login" replace />
}

function PublicRoute({ children }: { children: React.ReactNode }) {
  const { isAuthenticated } = useAuthStore()
  return !isAuthenticated ? <>{children}</> : <Navigate to="/" replace />
}

export default function App() {
  return (
    <Routes>
      <Route path="/login" element={<PublicRoute><LoginPage /></PublicRoute>} />
      <Route element={<ProtectedRoute><AppShell /></ProtectedRoute>}>
        <Route path="/" element={<BriefingPage />} />
        <Route path="/dashboard" element={<DashboardPage />} />
        <Route path="/crm" element={<CRMPage />} />
        <Route path="/sales" element={<SalesPage />} />
        <Route path="/finance" element={<FinancePage />} />
        <Route path="/hr" element={<HRPage />} />
        <Route path="/legal" element={<LegalPage />} />
        <Route path="/marketing" element={<MarketingPage />} />
        <Route path="/support" element={<SupportPage />} />
        <Route path="/automation" element={<AutomationPage />} />
        <Route path="/legal" element={<LegalPage />} />
        <Route path="/profile" element={<ProfilePage />} />
      </Route>
      <Route path="*" element={<Navigate to="/" replace />} />
    </Routes>
  )
}
