import { Routes, Route, Navigate } from 'react-router-dom';
import useAuth from '../hooks/useAuth';
import { AdminLayout, Layout } from '../layouts';
import {
  CountyListPage,
  Dashboard,
  JobCreationPage,
  JobListPage,
  NotFound,
  JobDetailPage,
  UserListPage,
  UserCreationPage,
  UserUpdationPage,
  Profile,
  UpdateAndViewCounty,
  JobRecodListPage,
  ScrapingDataListPage,
} from '../scenes';
import { ROUTES } from '../constants/paths';

const AdminRoutes = () => {
  let auth = useAuth();
  if (auth?.user?.permission && Array.isArray(auth.user.permission)) {
    const lowercaseSearchString = "user";

    // Check if any of the permission strings contain "user" (case-insensitive)
    const hasUserPermission = auth.user.permission.some(permission =>
        permission.toLowerCase().includes(lowercaseSearchString)
    );

    if (hasUserPermission) {
      console.log("dung la user roif");
      <Navigate to={ROUTES.DASHBOARD.NOT_FOUND.path} />
    }
}
 
  

  return (
    <Routes>
      <Route element={<Layout />}>
        <Route path={ROUTES.DASHBOARD.RECORDS.VIEW_SCRATCH_DATA.path} element={<ScrapingDataListPage />} />
        <Route path={ROUTES.DASHBOARD.USERS.CREATE.path} element={<UserCreationPage />} />
        <Route path={ROUTES.DASHBOARD.USERS.VIEW.path} element={<UserUpdationPage />} />
        <Route path={ROUTES.DASHBOARD.JOBS.CREATE.path} element={<JobCreationPage />} />
        <Route path={ROUTES.DASHBOARD.JOBS.VIEW.path} element={<JobDetailPage />} />
        <Route path={ROUTES.DASHBOARD.COUNTIES.VIEW.path} element={<UpdateAndViewCounty />} />
      </Route>
      <Route element={<AdminLayout />}>
        <Route path={ROUTES.DASHBOARD.ROOT.path} index element={<Dashboard />} />
        <Route path={ROUTES.DASHBOARD.COUNTIES.ROOT.path} element={<CountyListPage />} />
        <Route path={ROUTES.DASHBOARD.USERS.ROOT.path} element={<UserListPage />} />
        <Route path={ROUTES.DASHBOARD.JOBS.ROOT.path} element={<JobListPage />} />
        <Route path={ROUTES.DASHBOARD.PROFILE.ROOT.path} element={<Profile />} />
        <Route path={ROUTES.DASHBOARD.RECORDS.ROOT.path} element={<JobRecodListPage />} />
        <Route path={ROUTES.DASHBOARD.NOT_FOUND.path} element={<NotFound />} />
        <Route path="*" element={<Navigate to={ROUTES.DASHBOARD.NOT_FOUND.path} />} />
      </Route>
    </Routes>
  );
};

export default AdminRoutes;
