import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import { useAuthStore } from './stores/authStore';
import AdminLayout from './components/layout/AdminLayout';
import LoginPage from './pages/LoginPage';
import DashboardPage from './pages/DashboardPage';
import UsersPage from './pages/UsersPage';
import UserDetailPage from './pages/UserDetailPage';
import DoctorsPage from './pages/DoctorsPage';
import DoctorDetailPage from './pages/DoctorDetailPage';
import ClinicsPage from './pages/ClinicsPage';
import ClinicDetailPage from './pages/ClinicDetailPage';
import AppointmentsPage from './pages/AppointmentsPage';
import AppointmentDetailPage from './pages/AppointmentDetailPage';
import PaymentsPage from './pages/PaymentsPage';
import PaymentDetailPage from './pages/PaymentDetailPage';
import SettlementsPage from './pages/SettlementsPage';
import RepresentationsPage from './pages/RepresentationsPage';
import CommentsPage from './pages/CommentsPage';
import RatingsPage from './pages/RatingsPage';
import SmsPage from './pages/SmsPage';
import CategoriesPage from './pages/CategoriesPage';
import BlogsPage from './pages/BlogsPage';
import BlogFormPage from './pages/BlogFormPage';
import SecretariesPage from './pages/SecretariesPage';

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

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

export default function App() {
  return (
    <Routes>
      <Route
        path="/admin/login"
        element={
          <PublicRoute>
            <LoginPage />
          </PublicRoute>
        }
      />
      <Route
        path="/admin/*"
        element={
          <PrivateRoute>
            <AdminLayout />
          </PrivateRoute>
        }
      >
        <Route index element={<Navigate to="/admin/dashboard" replace />} />
        <Route path="dashboard" element={<DashboardPage />} />

        {/* Users */}
        <Route path="users" element={<UsersPage />} />
        <Route path="users/:uuid" element={<UserDetailPage />} />

        {/* Doctors */}
        <Route path="doctors" element={<DoctorsPage />} />
        <Route path="doctors/:uuid" element={<DoctorDetailPage />} />

        {/* Clinics */}
        <Route path="clinics" element={<ClinicsPage />} />
        <Route path="clinics/:uuid" element={<ClinicDetailPage />} />

        {/* Appointments */}
        <Route path="appointments" element={<AppointmentsPage />} />
        <Route path="appointments/:uuid" element={<AppointmentDetailPage />} />

        {/* Payments */}
        <Route path="payments" element={<PaymentsPage />} />
        <Route path="payments/:uuid" element={<PaymentDetailPage />} />

        {/* Settlements */}
        <Route path="settlements" element={<SettlementsPage />} />

        {/* Representations */}
        <Route path="representations" element={<RepresentationsPage />} />

        {/* Comments & Ratings */}
        <Route path="comments" element={<CommentsPage />} />
        <Route path="ratings" element={<RatingsPage />} />

        {/* SMS */}
        <Route path="sms" element={<SmsPage />} />

        {/* Categories */}
        <Route path="categories" element={<CategoriesPage />} />

        {/* Blogs */}
        <Route path="blogs" element={<BlogsPage />} />
        <Route path="blogs/new" element={<BlogFormPage />} />
        <Route path="blogs/:uuid/edit" element={<BlogFormPage />} />

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