import Agencies from "@/pages/agencies/ui/Agencies";
import AgencyDetail from "@/pages/agencies/ui/AgencyDetail";
import EditAgecy from "@/pages/agencies/ui/EditAgecy";
import Login from "@/pages/auth/ui/Login";
import Bookings from "@/pages/bookings/ui/Bookings";
import Employees from "@/pages/employees/ui/Employees";
import Faq from "@/pages/faq/ui/Faq";
import FaqCategory from "@/pages/faq/ui/FaqCategory";
import FinancePage from "@/pages/finance/ui/Finance";
import FinanceDetailTour from "@/pages/finance/ui/FinanceDetailTour";
import {
  default as FinanceDetailUsers,
  default as PurchaseDetailPage,
} from "@/pages/finance/ui/FinanceDetailUsers";
import AddNews from "@/pages/news/ui/AddNews";
import News from "@/pages/news/ui/News";
import NewsCategory from "@/pages/news/ui/NewsCategory";
import Seo from "@/pages/seo/ui/Seo";
import PolicyCrud from "@/pages/site-page/ui/PolicyCrud";
import SitePage from "@/pages/site-page/ui/SitePage";
import SupportAgency from "@/pages/support/ui/SupportAgency";
import SupportTours from "@/pages/support/ui/SupportTours";
import TourSettings from "@/pages/tour-settings/ui/TourSettings";
import CreateEditTour from "@/pages/tours/ui/CreateEditTour";
import TourDetail from "@/pages/tours/ui/TourDetail";
import Tours from "@/pages/tours/ui/Tours";
import ToursSetting from "@/pages/tours/ui/ToursSetting";
import CreateUser from "@/pages/users/ui/Create";
import EditUser from "@/pages/users/ui/Edit";
import UserList from "@/pages/users/ui/User";
import UserDetail from "@/pages/users/ui/UserDetail";
import MainProvider from "@/providers/main";
import "@/shared/config/i18n";
import useUserStore from "@/shared/hooks/user";
import { getAuthToken } from "@/shared/lib/authCookies";
import { Sidebar } from "@/widgets/sidebar/ui/Sidebar";
import { useEffect } from "react";
import {
  Navigate,
  Route,
  Routes,
  useLocation,
  useNavigate,
} from "react-router-dom";

const App = () => {
  const { user } = useUserStore();
  const token = getAuthToken();
  const navigate = useNavigate();
  const location = useLocation();

  const hideSidebarPaths = ["/login"];
  const shouldShowSidebar = !hideSidebarPaths.includes(location.pathname);

  useEffect(() => {
    if (token && user) {
      navigate("/user");
    } else if (!token && !user) {
      navigate("/login");
    }
  }, [token, user]);

  return (
    <MainProvider>
      <div className="flex max-lg:flex-col bg-gray-900">
        {shouldShowSidebar && <Sidebar role={user ? user.role : "admin"} />}

        <Routes>
          <Route path="/" element={<Navigate to={"/user"} />} />
          <Route path="/user" element={<UserList />} />
          <Route path="/login" element={<Login />} />
          <Route path="/users/create" element={<CreateUser />} />
          <Route path="/users/:id/edit" element={<EditUser />} />
          <Route path="/users/:id/" element={<UserDetail />} />
          <Route path="/agencies" element={<Agencies />} />
          <Route path="/agencies/:id" element={<AgencyDetail />} />
          <Route path="/agency/:id/edit" element={<EditAgecy />} />
          <Route path="/tours/:id" element={<TourDetail />} />
          <Route path="/employees" element={<Employees />} />
          <Route path="/finance" element={<FinancePage />} />
          <Route path="/purchases/:id/" element={<PurchaseDetailPage />} />
          <Route path="/travel/booking/:id/" element={<FinanceDetailTour />} />
          <Route path="/bookings/:id/" element={<FinanceDetailUsers />} />
          <Route path="/tours" element={<Tours />} />
          <Route path="/tours/setting" element={<ToursSetting />} />
          <Route path="/tours/:id/edit" element={<CreateEditTour />} />
          <Route path="/tours/create" element={<CreateEditTour />} />
          <Route path="/bookings" element={<Bookings />} />
          <Route path="/news" element={<News />} />
          <Route path="/news/add" element={<AddNews />} />
          <Route path="/news/categories" element={<NewsCategory />} />
          <Route path="/faq" element={<Faq />} />
          <Route path="/faq/categories" element={<FaqCategory />} />
          <Route path="/support/tours" element={<SupportAgency />} />
          <Route path="/support/user" element={<SupportTours />} />
          <Route path="/site-seo" element={<Seo />} />
          <Route path="/site-pages/" element={<SitePage />} />
          <Route path="/site-help/" element={<PolicyCrud />} />
          <Route path="/site-settings/" element={<TourSettings />} />
        </Routes>
      </div>
    </MainProvider>
  );
};

export default App;
