import {Refine, Authenticated} from '@refinedev/core'
import {DevtoolsPanel, DevtoolsProvider} from '@refinedev/devtools'
import {RefineKbar, RefineKbarProvider} from '@refinedev/kbar'

import {ErrorComponent, useNotificationProvider, RefineSnackbarProvider, ThemedLayoutV2} from '@refinedev/mui'

import dataProvider from '@refinedev/simple-rest'
import CssBaseline from '@mui/material/CssBaseline'
import GlobalStyles from '@mui/material/GlobalStyles'
import {BrowserRouter, Route, Routes, Outlet} from 'react-router'
import routerBindings, {NavigateToResource, CatchAllNavigate, UnsavedChangesNotifier, DocumentTitleHandler} from '@refinedev/react-router'
import {ColorModeContextProvider} from './contexts/color-mode'
import {Header} from './components/header'
import {Login} from './pages/login'
import {Register} from './pages/register'
import {ForgotPassword} from './pages/forgotPassword'
import {authProvider} from './authProvider'
import {i18nProvider} from './i18nProvider'

import {CountryList, CountryCreate, CountryEdit, CountryShow} from './pages/country'
import {CityList, CityCreate, CityEdit, CityShow} from './pages/city'
import {CarrierList, CarrierCreate, CarrierEdit, CarrierShow} from './pages/carrier'
import {MediaList, MediaCreate, MediaEdit, MediaShow} from './pages/media'
import {ArticleList, ArticleCreate, ArticleEdit, ArticleShow} from './pages/article'
import {SightList, SightCreate, SightEdit, SightShow} from './pages/sight'
import {StationList, StationCreate, StationEdit, StationShow} from './pages/station'
import {VehicleList, VehicleCreate, VehicleEdit, VehicleShow} from './pages/vehicle'
import {RouteList, RouteCreate, RouteEdit, RouteShow} from './pages/route'

import {CountryIcon, CityIcon, CarrierIcon, MediaIcon, ArticleIcon, SightIcon, StationIcon, VehicleIcon, RouteIcon} from './components/ui/Icons'
import {BACKEND_URL} from './lib/constants'

function App() {
  return (
    <BrowserRouter>
      <RefineKbarProvider>
        <ColorModeContextProvider>
          <CssBaseline />
          <GlobalStyles styles={{html: {WebkitFontSmoothing: 'auto'}}} />
          <RefineSnackbarProvider>
            <DevtoolsProvider>
              <Refine
                dataProvider={dataProvider(BACKEND_URL)}
                notificationProvider={useNotificationProvider}
                routerProvider={routerBindings}
                authProvider={authProvider}
                i18nProvider={i18nProvider}
                resources={[
                  {
                    name: 'country',
                    list: '/country',
                    create: '/country/create',
                    edit: '/country/edit/:id',
                    show: '/country/show/:id',
                    meta: {
                      canDelete: true,
                      label: 'Страны',
                      icon: <CountryIcon />,
                    },
                  },
                  {
                    name: 'city',
                    list: '/city',
                    create: '/city/create',
                    edit: '/city/edit/:id',
                    show: '/city/show/:id',
                    meta: {
                      canDelete: true,
                      label: 'Города',
                      icon: <CityIcon />,
                    },
                  },
                  {
                    name: 'carrier',
                    list: '/carrier',
                    create: '/carrier/create',
                    edit: '/carrier/edit/:id',
                    show: '/carrier/show/:id',
                    meta: {
                      canDelete: true,
                      label: 'Перевозчики',
                      icon: <CarrierIcon />,
                    },
                  },
                  {
                    name: 'media',
                    list: '/media',
                    create: '/media/create',
                    edit: '/media/edit/:id',
                    show: '/media/show/:id',
                    meta: {
                      canDelete: true,
                      label: 'Медиа',
                      icon: <MediaIcon />,
                    },
                  },
                  {
                    name: 'article',
                    list: '/article',
                    create: '/article/create',
                    edit: '/article/edit/:id',
                    show: '/article/show/:id',
                    meta: {
                      canDelete: true,
                      label: 'Статьи',
                      icon: <ArticleIcon />,
                    },
                  },
                  {
                    name: 'sight',
                    list: '/sight',
                    create: '/sight/create',
                    edit: '/sight/edit/:id',
                    show: '/sight/show/:id',
                    meta: {
                      canDelete: true,
                      label: 'Виды',
                      icon: <SightIcon />,
                    },
                  },
                  {
                    name: 'station',
                    list: '/station',
                    create: '/station/create',
                    edit: '/station/edit/:id',
                    show: '/station/show/:id',
                    meta: {
                      canDelete: true,
                      label: 'Остановки',
                      icon: <StationIcon />,
                    },
                  },
                  {
                    name: 'vehicle',
                    list: '/vehicle',
                    create: '/vehicle/create',
                    edit: '/vehicle/edit/:id',
                    show: '/vehicle/show/:id',
                    // добавить SHOW для vehicle->routes (https://wn.krbl.ru/vehicle/routes?id=1)
                    meta: {
                      canDelete: true,
                      label: 'Транспорт',
                      icon: <VehicleIcon />,
                    },
                  },
                  {
                    name: 'route',
                    list: '/route',
                    create: '/route/create',
                    edit: '/route/edit/:id',
                    show: '/route/show/:id',
                    // добавить SHOW для route->station (https://wn.krbl.ru/route/station)
                    // добавить SHOW для route->vehicle (https://wn.krbl.ru/route/vehicle)
                    meta: {
                      canDelete: true,
                      label: 'Маршруты',
                      icon: <RouteIcon />,
                    },
                  },
                ]}
                options={{
                  syncWithLocation: true,
                  warnWhenUnsavedChanges: true,
                  useNewQueryKeys: true,
                  projectId: 'Wv044J-t53S3s-PcbJGe',
                }}
              >
                <Routes>
                  <Route
                    element={
                      <Authenticated key="authenticated-inner" fallback={<CatchAllNavigate to="/login" />}>
                        <ThemedLayoutV2 Header={Header}>
                          <Outlet />
                        </ThemedLayoutV2>
                      </Authenticated>
                    }
                  >
                    <Route index element={<NavigateToResource resource="country" />} />

                    <Route path="/country">
                      <Route index element={<CountryList />} />
                      <Route path="create" element={<CountryCreate />} />
                      <Route path="edit/:name" element={<CountryEdit />} />
                      <Route path="show/:id" element={<CountryShow />} />
                    </Route>

                    <Route path="/city">
                      <Route index element={<CityList />} />
                      <Route path="create" element={<CityCreate />} />
                      <Route path="edit/:id" element={<CityEdit />} />
                      <Route path="show/:id" element={<CityShow />} />
                    </Route>

                    <Route path="/carrier">
                      <Route index element={<CarrierList />} />
                      <Route path="create" element={<CarrierCreate />} />
                      <Route path="edit/:id" element={<CarrierEdit />} />
                      <Route path="show/:id" element={<CarrierShow />} />
                    </Route>

                    <Route path="/media">
                      <Route index element={<MediaList />} />
                      <Route path="create" element={<MediaCreate />} />
                      <Route path="edit/:id" element={<MediaEdit />} />
                      <Route path="show/:id" element={<MediaShow />} />
                    </Route>

                    <Route path="/article">
                      <Route index element={<ArticleList />} />
                      <Route path="create" element={<ArticleCreate />} />
                      <Route path="edit/:id" element={<ArticleEdit />} />
                      <Route path="show/:id" element={<ArticleShow />} />
                    </Route>

                    <Route path="/sight">
                      <Route index element={<SightList />} />
                      <Route path="create" element={<SightCreate />} />
                      <Route path="edit/:id" element={<SightEdit />} />
                      <Route path="show/:id" element={<SightShow />} />
                    </Route>

                    <Route path="/station">
                      <Route index element={<StationList />} />
                      <Route path="create" element={<StationCreate />} />
                      <Route path="edit/:id" element={<StationEdit />} />
                      <Route path="show/:id" element={<StationShow />} />
                    </Route>

                    <Route path="/vehicle">
                      <Route index element={<VehicleList />} />
                      <Route path="create" element={<VehicleCreate />} />
                      <Route path="edit/:id" element={<VehicleEdit />} />
                      <Route path="show/:id" element={<VehicleShow />} />
                    </Route>

                    <Route path="/route">
                      <Route index element={<RouteList />} />
                      <Route path="create" element={<RouteCreate />} />
                      <Route path="edit/:id" element={<RouteEdit />} />
                      <Route path="show/:id" element={<RouteShow />} />
                    </Route>

                    <Route path="*" element={<ErrorComponent />} />
                  </Route>
                  <Route
                    element={
                      <Authenticated key="authenticated-outer" fallback={<Outlet />}>
                        <NavigateToResource />
                      </Authenticated>
                    }
                  >
                    <Route path="/login" element={<Login />} />
                    <Route path="/register" element={<Register />} />
                    <Route path="/forgot-password" element={<ForgotPassword />} />
                  </Route>
                </Routes>

                <RefineKbar />
                <UnsavedChangesNotifier />
                <DocumentTitleHandler />
              </Refine>
              <DevtoolsPanel />
            </DevtoolsProvider>
          </RefineSnackbarProvider>
        </ColorModeContextProvider>
      </RefineKbarProvider>
    </BrowserRouter>
  )
}

export default App
