import { BrowserRouter, Routes, Route, Navigate, Outlet } from 'react-router-dom';
import { authApi } from './services/api';
import LoginPage from './pages/Login';
import AdminLayout from './components/AdminLayout';
import PublicQueryPage from './pages/PublicQuery';
import DashboardPage from './pages/Dashboard';
import ProfilePage from './pages/Profile';
import EmployeeSerialsPage from './pages/EmployeeSerials';
import ProductTracesPage from './pages/ProductTraces';
import ProductTracePublicPage from './pages/ProductTracePublic';
import AftersalesPage from './pages/Aftersales';
import AftersalesDetailPage from './pages/AftersalesDetail';
import AftersalesConfirmPage from './pages/AftersalesConfirm';
import ProjectOrdersPage from './pages/ProjectOrders';
import ProjectOrderDetailPage from './pages/ProjectOrderDetail';
import ProjectOrderCompletePage from './pages/ProjectOrderComplete';

const PrivateRoute = () => {
  const user = authApi.getCurrentUser();
  if (!user) {
    return <Navigate to="/login" replace />;
  }
  return <Outlet />;
};

const PublicRoute = ({ children }: { children: React.ReactNode }) => {
  const user = authApi.getCurrentUser();
  if (user) {
    return <Navigate to="/admin/dashboard" replace />;
  }
  return <>{children}</>;
};

const AdminRoutes = () => {
  return <AdminLayout />;
};

const AdminIndexRedirect = () => {
  const user = authApi.getCurrentUser();
  return <Navigate to={user?.role === 'technician' ? 'aftersales' : 'dashboard'} replace />;
};

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Navigate to="/login" replace />} />
        <Route path="/login" element={
          <PublicRoute>
            <LoginPage />
          </PublicRoute>
        } />
        <Route path="/query" element={<PublicQueryPage />} />
        <Route path="/product-traces/:serialNumber" element={<ProductTracePublicPage />} />
        <Route path="/aftersales/:serialNumber" element={<AftersalesConfirmPage />} />
        <Route path="/project-orders/:serialNumber" element={<ProjectOrderCompletePage />} />

        <Route element={<PrivateRoute />}>
          <Route element={<AdminRoutes />}>
            <Route path="/admin" element={<AdminIndexRedirect />} />
            <Route path="/admin/dashboard" element={<DashboardPage />} />
            <Route path="/admin/employee-serials" element={<EmployeeSerialsPage />} />
            <Route path="/admin/product-traces" element={<ProductTracesPage />} />
            <Route path="/admin/aftersales" element={<AftersalesPage />} />
            <Route path="/admin/aftersales/:serialNumber" element={<AftersalesDetailPage />} />
            <Route path="/admin/project-orders" element={<ProjectOrdersPage />} />
            <Route path="/admin/project-orders/:serialNumber" element={<ProjectOrderDetailPage />} />
            <Route path="/admin/profile" element={<ProfilePage />} />
          </Route>
        </Route>
      </Routes>
    </BrowserRouter>
  );
}

export default App;
