import React from 'react';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import { AppShell } from '@/components/layout/AppShell';
import { ProtectedRoute } from './ProtectedRoute';
import { LoginPage } from '@/pages/Login';
import { PasswordResetRequestPage } from '@/pages/PasswordResetRequest';
import { PasswordResetConfirmPage } from '@/pages/PasswordResetConfirm';
import { DashboardPage } from '@/pages/Dashboard';
import { CompaniesListPage } from '@/pages/CompaniesList';
import { CompanyDetailPage } from '@/pages/CompanyDetail';
import { CompanyFormPage } from '@/pages/CompanyForm';
import { ContactsListPage } from '@/pages/ContactsList';
import { ContactDetailPage } from '@/pages/ContactDetail';
import { ContactFormPage } from '@/pages/ContactForm';
import { AuditLogPage } from '@/pages/AuditLog';
import { GlobalSearchResultsPage } from '@/pages/GlobalSearchResults';
import { SettingsPage } from '@/pages/Settings';
import { SettingsProfilePage } from '@/pages/SettingsProfile';
import { SettingsRolesPage } from '@/pages/SettingsRoles';
import { SettingsUsersPage } from '@/pages/SettingsUsers';
import { CalendarPage } from '@/pages/Calendar';
import { CalendarKanbanPage } from '@/pages/CalendarKanban';
import { DmsPage } from '@/pages/Dms';
import { DmsTrashPage } from '@/pages/DmsTrash';
import { MailPage } from '@/pages/Mail';
import { MailSettingsPage } from '@/pages/MailSettings';

const router = createBrowserRouter([
  {
    path: '/login',
    element: <LoginPage />,
  },
  {
    path: '/password-reset',
    element: <PasswordResetRequestPage />,
  },
  {
    path: '/password-reset/confirm',
    element: <PasswordResetConfirmPage />,
  },
  {
    element: (
      <ProtectedRoute>
        <AppShell />
      </ProtectedRoute>
    ),
    children: [
      { path: '/', element: <DashboardPage /> },
      { path: '/dashboard', element: <DashboardPage /> },
      { path: '/companies', element: <CompaniesListPage /> },
      { path: '/companies/new', element: <CompanyFormPage /> },
      { path: '/companies/:id', element: <CompanyDetailPage /> },
      { path: '/companies/:id/edit', element: <CompanyFormPage /> },
      { path: '/contacts', element: <ContactsListPage /> },
      { path: '/contacts/new', element: <ContactFormPage /> },
      { path: '/contacts/:id', element: <ContactDetailPage /> },
      { path: '/contacts/:id/edit', element: <ContactFormPage /> },
      { path: '/audit-log', element: <AuditLogPage /> },
      { path: '/search', element: <GlobalSearchResultsPage /> },
      { path: '/calendar', element: <CalendarPage /> },
      { path: '/calendar/kanban', element: <CalendarKanbanPage /> },
      { path: '/dms', element: <DmsPage /> },
      { path: '/dms/trash', element: <DmsTrashPage /> },
      { path: '/mail', element: <MailPage /> },
      { path: '/mail/settings', element: <MailSettingsPage /> },
      {
        path: '/settings',
        element: <SettingsPage />,
        children: [
          { path: 'profile', element: <SettingsProfilePage /> },
          { path: 'roles', element: <SettingsRolesPage /> },
          { path: 'users', element: <SettingsUsersPage /> },
        ],
      },
    ],
  },
]);

export function AppRouter() {
  return <RouterProvider router={router} />;
}
