import React, { useEffect } from "react";
import { Routes, Route, Navigate } from "react-router-dom";
import { Toaster } from "react-hot-toast";
import useAuthStore from "./store/authStore";

import Login from "./pages/auth/Login";
import Register from "./pages/auth/Register";
import Dashboard from "./pages/Dashboard";
import Companies from "./pages/companies/Companies";
import CompanyDetail from "./pages/companies/CompanyDetail";
import Employees from "./pages/employees/Employees";
import EmployeeDetail from "./pages/employees/EmployeeDetail";
import Departments from "./pages/departments/Departments";
import Profile from "./pages/Profile";
import Layout from "./components/Layout";

// Advanced Enterprise Features
import GroupDashboard from "./pages/GroupDashboard";
import SuperAdminDashboard from "./pages/SuperAdminDashboard";
import SuperAdminBankAccounts from "./pages/SuperAdminBankAccounts";
import CompanyDashboard from "./pages/CompanyDashboard";
import LoanManagement from "./pages/LoanManagement";
import ProfitSweepManagement from "./pages/ProfitSweepManagement";
import RoleManagement from "./pages/RoleManagement";
import Invoices from "./pages/Invoices";
import Treasury from "./pages/Treasury";
import Settings from "./pages/Settings";
import AuditLogs from "./pages/AuditLogs";
import CompanyAdminDashboard from "./pages/CompanyAdminDashboard";
import Expenses from "./pages/Expenses";
import Assets from "./pages/Assets";
import CompanyUsers from "./pages/CompanyUsers";
import InvestorsPartners from "./pages/InvestorsPartners";
import BankAccounts from "./pages/BankAccounts";

function App() {
  const { isAuthenticated, fetchUser, token } = useAuthStore();

  useEffect(() => {
    if (token && !isAuthenticated) {
      fetchUser();
    }
  }, [token]);

  return (
    <>
      <Toaster
        position="top-right"
        toastOptions={{
          duration: 3000,
          style: {
            background: "#fff",
            color: "#1f2937",
            border: "1px solid #e8e8fd",
            borderRadius: "12px",
            boxShadow: "0 4px 24px rgba(93,64,55,0.1)",
          },
          success: {
            iconTheme: { primary: "#5D4037", secondary: "#fff" },
          },
          error: {
            iconTheme: { primary: "#dc2626", secondary: "#fff" },
          },
        }}
      />

      <Routes>
        <Route
          path="/login"
          element={!isAuthenticated ? <Login /> : <Navigate to="/dashboard" />}
        />
        <Route
          path="/register"
          element={
            !isAuthenticated ? <Register /> : <Navigate to="/dashboard" />
          }
        />

        <Route
          path="/"
          element={isAuthenticated ? <Layout /> : <Navigate to="/login" />}
        >
          <Route index element={<Navigate to="/dashboard" />} />
          <Route path="dashboard" element={<Dashboard />} />
          <Route path="companies" element={<Companies />} />
          <Route path="companies/:id" element={<CompanyDetail />} />
          <Route path="employees" element={<Employees />} />
          <Route path="employees/:id" element={<EmployeeDetail />} />
          <Route path="departments" element={<Departments />} />
          <Route path="profile" element={<Profile />} />

          {/* Advanced Enterprise Features */}
          <Route path="group-dashboard" element={<GroupDashboard />} />
          <Route
            path="super-admin-dashboard"
            element={<SuperAdminDashboard />}
          />
          <Route
            path="super-admin-bank-accounts"
            element={<SuperAdminBankAccounts />}
          />
          <Route path="company-dashboard" element={<CompanyDashboard />} />
          <Route path="loans" element={<LoanManagement />} />
          <Route path="profit-sweeps" element={<ProfitSweepManagement />} />
          <Route path="roles" element={<RoleManagement />} />
          <Route path="invoices" element={<Invoices />} />
          <Route path="treasury" element={<Treasury />} />
          <Route path="settings" element={<Settings />} />
          <Route path="audit-logs" element={<AuditLogs />} />
          <Route
            path="company-admin-dashboard"
            element={<CompanyAdminDashboard />}
          />
          <Route path="expenses" element={<Expenses />} />
          <Route path="assets" element={<Assets />} />
          <Route path="company-users" element={<CompanyUsers />} />
          <Route path="investors" element={<InvestorsPartners />} />
          <Route path="bank-accounts" element={<BankAccounts />} />
        </Route>

        <Route path="*" element={<Navigate to="/dashboard" />} />
      </Routes>
    </>
  );
}

export default App;
