import React from 'react'
import {
  HashRouter,
  BrowserRouter,
  Routes,
  Route,
  Navigate
} from 'react-router-dom'
import { App, ConfigProvider } from 'antd'

import ProductionOverview from './components/Dashboard/Production/ProductionOverview'

import Printers from './components/Dashboard/Production/Printers'
import ControlPrinter from './components/Dashboard/Production/Printers/ControlPrinter.jsx'
import PrinterInfo from './components/Dashboard/Production/Printers/PrinterInfo.jsx'

import Jobs from './components/Dashboard/Production/Jobs.jsx'
import JobInfo from './components/Dashboard/Production/Jobs/JobInfo.jsx'

import Filaments from './components/Dashboard/Management/Filaments'
import FilamentInfo from './components/Dashboard/Management/Filaments/FilamentInfo.jsx'

import GCodeFiles from './components/Dashboard/Production/GCodeFiles'
import GCodeFileInfo from './components/Dashboard/Production/GCodeFiles/GCodeFileInfo.jsx'

import Parts from './components/Dashboard/Management/Parts.jsx'
import PartInfo from './components/Dashboard/Management/Parts/PartInfo.jsx'

import Products from './components/Dashboard/Management/Products.jsx'
import ProductInfo from './components/Dashboard/Management/Products/ProductInfo.jsx'

import Vendors from './components/Dashboard/Management/Vendors'
import VendorInfo from './components/Dashboard/Management/Vendors/VendorInfo'

import Materials from './components/Dashboard/Management/Materials'

import FilamentStocks from './components/Dashboard/Inventory/FilamentStocks.jsx'
import FilamentStockInfo from './components/Dashboard/Inventory/FilamentStocks/FilamentStockInfo.jsx'

import PartStocks from './components/Dashboard/Inventory/PartStocks.jsx'

import StockAudits from './components/Dashboard/Inventory/StockAudits.jsx'
import StockAuditInfo from './components/Dashboard/Inventory/StockAudits/StockAuditInfo.jsx'

import Dashboard from './components/Dashboard/Dashboard.jsx'
import PrivateRoute from './components/PrivateRoute'
import './App.css'
import { PrintServerProvider } from './components/Dashboard/context/PrintServerContext.js'
import { AuthProvider } from './components/Dashboard/context/AuthContext.js'
import { SpotlightProvider } from './components/Dashboard/context/SpotlightContext.js'
import StockEvents from './components/Dashboard/Inventory/StockEvents.jsx'

import Settings from './components/Dashboard/Management/Settings'
import AuditLogs from './components/Dashboard/Management/AuditLogs.jsx'

import {
  ThemeProvider,
  useThemeContext
} from './components/Dashboard/context/ThemeContext'
import AppError from './components/App/AppError'
import NoteTypes from './components/Dashboard/Management/NoteTypes.jsx'
import NoteTypeInfo from './components/Dashboard/Management/NoteTypes/NoteTypeInfo.jsx'
import SessionStorage from './components/Dashboard/Developer/SessionStorage.jsx'
import AuthContextDebug from './components/Dashboard/Developer/AuthContextDebug.jsx'
import PrintServerContextDebug from './components/Dashboard/Developer/PrintServerContextDebug.jsx'
import { ApiServerProvider } from './components/Dashboard/context/ApiServerContext.js'
import Users from './components/Dashboard/Management/Users.jsx'
import UserInfo from './components/Dashboard/Management/Users/UserInfo.jsx'
import SubJobs from './components/Dashboard/Production/SubJobs.jsx'
import Hosts from './components/Dashboard/Management/Hosts.jsx'
import { ElectronProvider } from './components/Dashboard/context/ElectronContext.js'
import AuthCallback from './components/App/AuthCallback.jsx'

const getRouter = () => {
  if (
    typeof window !== 'undefined' &&
    window.location.href.includes('index.html')
  ) {
    return HashRouter
  }
  return BrowserRouter
}

const AppContent = () => {
  const { themeConfig } = useThemeContext()
  const Router = getRouter()

  return (
    <ConfigProvider theme={themeConfig}>
      <App>
        <Router>
          <ElectronProvider>
            <AuthProvider>
              <PrintServerProvider>
                <ApiServerProvider>
                  <SpotlightProvider>
                    <Routes>
                      <Route
                        path='/'
                        element={
                          <PrivateRoute
                            component={() => (
                              <Navigate
                                to='/dashboard/production/overview'
                                replace
                              />
                            )}
                          />
                        }
                      />
                      <Route path='/auth/callback' element={<AuthCallback />} />
                      <Route
                        path='/dashboard'
                        element={
                          <PrivateRoute component={() => <Dashboard />} />
                        }
                      >
                        {/* Production Routes */}
                        <Route
                          path='production/overview'
                          element={<ProductionOverview />}
                        />
                        <Route
                          path='production/printers'
                          element={<Printers />}
                        />
                        <Route
                          path='production/printers/control'
                          element={<ControlPrinter />}
                        />
                        <Route
                          path='production/printers/info'
                          element={<PrinterInfo />}
                        />
                        <Route path='production/jobs' element={<Jobs />} />
                        <Route
                          path='production/subjobs'
                          element={<SubJobs />}
                        />
                        <Route
                          path='production/jobs/info'
                          element={<JobInfo />}
                        />
                        <Route
                          path='production/gcodefiles'
                          element={<GCodeFiles />}
                        />
                        <Route
                          path='production/gcodefiles/info'
                          element={<GCodeFileInfo />}
                        />

                        {/* Inventory Routes */}
                        <Route
                          path='inventory/filamentstocks'
                          element={<FilamentStocks />}
                        />
                        <Route
                          path='inventory/filamentstocks/info'
                          element={<FilamentStockInfo />}
                        />
                        <Route
                          path='inventory/partstocks'
                          element={<PartStocks />}
                        />
                        <Route
                          path='inventory/stockevents'
                          element={<StockEvents />}
                        />
                        <Route
                          path='inventory/stockaudits'
                          element={<StockAudits />}
                        />
                        <Route
                          path='inventory/stockaudits/info'
                          element={<StockAuditInfo />}
                        />

                        {/* Management Routes */}
                        <Route
                          path='management/filaments'
                          element={<Filaments />}
                        />
                        <Route
                          path='management/filaments/info'
                          element={<FilamentInfo />}
                        />
                        <Route path='management/parts' element={<Parts />} />
                        <Route
                          path='management/parts/info'
                          element={<PartInfo />}
                        />
                        <Route
                          path='management/products'
                          element={<Products />}
                        />
                        <Route
                          path='management/products/info'
                          element={<ProductInfo />}
                        />
                        <Route
                          path='management/vendors'
                          element={<Vendors />}
                        />
                        <Route path='management/hosts' element={<Hosts />} />
                        <Route
                          path='management/users/info'
                          element={<UserInfo />}
                        />
                        <Route
                          path='management/vendors/info'
                          element={<VendorInfo />}
                        />
                        <Route
                          path='management/materials'
                          element={<Materials />}
                        />
                        <Route
                          path='management/notetypes'
                          element={<NoteTypes />}
                        />
                        <Route path='management/users' element={<Users />} />
                        <Route
                          path='management/notetypes/info'
                          element={<NoteTypeInfo />}
                        />
                        <Route
                          path='management/settings'
                          element={<Settings />}
                        />
                        <Route
                          path='management/auditlogs'
                          element={<AuditLogs />}
                        />
                        <Route
                          path='developer/sessionstorage'
                          element={<SessionStorage />}
                        />
                        <Route
                          path='developer/authcontextdebug'
                          element={<AuthContextDebug />}
                        />
                        <Route
                          path='developer/printservercontextdebug'
                          element={<PrintServerContextDebug />}
                        />
                      </Route>
                      <Route
                        path='*'
                        element={
                          <AppError
                            message='Error 404. Page not found.'
                            showRefresh={false}
                          />
                        }
                      />
                    </Routes>
                  </SpotlightProvider>
                </ApiServerProvider>
              </PrintServerProvider>
            </AuthProvider>
          </ElectronProvider>
        </Router>
      </App>
    </ConfigProvider>
  )
}

const FarmControlApp = () => {
  return (
    <ThemeProvider>
      <AppContent />
    </ThemeProvider>
  )
}

export default FarmControlApp
