import React from 'react';
import { createBrowserRouter, Outlet, Navigate, useLocation } from 'react-router-dom';
import { useAuth } from './auth/context';
import {
  ADMIN_BASE_PATH,
  ADMIN_DEFAULT_AFTER_LOGIN_PATH,
  ADMIN_EVENTS_PATH,
  ADMIN_LOGIN_START_PATH,
  ADMIN_PUBLIC_LANDING_PATH,
} from './constants';
const LoginPage = React.lazy(() => import('./pages/LoginPage'));
const DashboardPage = React.lazy(() => import('./pages/DashboardPage'));
const EventsPage = React.lazy(() => import('./pages/EventsPage'));
const SettingsPage = React.lazy(() => import('./pages/SettingsPage'));
const EventFormPage = React.lazy(() => import('./pages/EventFormPage'));
const EventPhotosPage = React.lazy(() => import('./pages/EventPhotosPage'));
const EventDetailPage = React.lazy(() => import('./pages/EventDetailPage'));
const EventMembersPage = React.lazy(() => import('./pages/EventMembersPage'));
const EventTasksPage = React.lazy(() => import('./pages/EventTasksPage'));
const EventToolkitPage = React.lazy(() => import('./pages/EventToolkitPage'));
const EventInvitesPage = React.lazy(() => import('./pages/EventInvitesPage'));
const EventPhotoboothPage = React.lazy(() => import('./pages/EventPhotoboothPage'));
const EngagementPage = React.lazy(() => import('./pages/EngagementPage'));
const BillingPage = React.lazy(() => import('./pages/BillingPage'));
const TasksPage = React.lazy(() => import('./pages/TasksPage'));
const TaskCollectionsPage = React.lazy(() => import('./pages/TaskCollectionsPage'));
const EmotionsPage = React.lazy(() => import('./pages/EmotionsPage'));
const FaqPage = React.lazy(() => import('./pages/FaqPage'));
const AuthCallbackPage = React.lazy(() => import('./pages/AuthCallbackPage'));
const WelcomeTeaserPage = React.lazy(() => import('./pages/WelcomeTeaserPage'));
const LoginStartPage = React.lazy(() => import('./pages/LoginStartPage'));
const ProfilePage = React.lazy(() => import('./pages/ProfilePage'));
const LogoutPage = React.lazy(() => import('./pages/LogoutPage'));
const WelcomeLandingPage = React.lazy(() => import('./onboarding/pages/WelcomeLandingPage'));
const WelcomePackagesPage = React.lazy(() => import('./onboarding/pages/WelcomePackagesPage'));
const WelcomeEventSetupPage = React.lazy(() => import('./onboarding/pages/WelcomeEventSetupPage'));
const WelcomeOrderSummaryPage = React.lazy(() => import('./onboarding/pages/WelcomeOrderSummaryPage'));

function RequireAuth() {
  const { status } = useAuth();
  const location = useLocation();

  if (status === 'loading') {
    return (
      <div className="flex min-h-screen items-center justify-center text-sm text-muted-foreground">
        Bitte warten ...
      </div>
    );
  }

  if (status === 'unauthenticated') {
    return <Navigate to={ADMIN_LOGIN_START_PATH} state={{ from: location }} replace />;
  }

  return <Outlet />;
}

function LandingGate() {
  const { status, user } = useAuth();

  if (status === 'loading') {
    return (
      <div className="flex min-h-screen items-center justify-center text-sm text-muted-foreground">
        Bitte warten ...
      </div>
    );
  }

  if (status === 'authenticated') {
    const target = user?.role === 'member' ? ADMIN_EVENTS_PATH : ADMIN_DEFAULT_AFTER_LOGIN_PATH;
    return <Navigate to={target} replace />;
  }

  return <WelcomeTeaserPage />;
}

function RequireAdminAccess({ children }: { children: React.ReactNode }) {
  const { user } = useAuth();

  if (user?.role === 'member') {
    return <Navigate to={ADMIN_EVENTS_PATH} replace />;
  }

  return <>{children}</>;
}

export const router = createBrowserRouter([
  {
    path: ADMIN_BASE_PATH,
    element: <Outlet />,
    children: [
      { index: true, element: <LandingGate /> },
      { path: 'login', element: <LoginPage /> },
      { path: 'start', element: <LoginStartPage /> },
      { path: 'logout', element: <LogoutPage /> },
      { path: 'auth/callback', element: <AuthCallbackPage /> },
      {
        element: <RequireAuth />,
        children: [
          { path: 'dashboard', element: <RequireAdminAccess><DashboardPage /></RequireAdminAccess> },
          { path: 'events', element: <EventsPage /> },
          { path: 'events/new', element: <RequireAdminAccess><EventFormPage /></RequireAdminAccess> },
          { path: 'events/:slug', element: <EventDetailPage /> },
          { path: 'events/:slug/edit', element: <RequireAdminAccess><EventFormPage /></RequireAdminAccess> },
          { path: 'events/:slug/photos', element: <EventPhotosPage /> },
          { path: 'events/:slug/members', element: <RequireAdminAccess><EventMembersPage /></RequireAdminAccess> },
          { path: 'events/:slug/tasks', element: <EventTasksPage /> },
          { path: 'events/:slug/invites', element: <EventInvitesPage /> },
          { path: 'events/:slug/photobooth', element: <RequireAdminAccess><EventPhotoboothPage /></RequireAdminAccess> },
          { path: 'events/:slug/toolkit', element: <EventToolkitPage /> },
          { path: 'engagement', element: <EngagementPage /> },
          { path: 'tasks', element: <TasksPage /> },
          { path: 'task-collections', element: <TaskCollectionsPage /> },
          { path: 'emotions', element: <EmotionsPage /> },
          { path: 'billing', element: <RequireAdminAccess><BillingPage /></RequireAdminAccess> },
          { path: 'settings', element: <RequireAdminAccess><SettingsPage /></RequireAdminAccess> },
          { path: 'faq', element: <FaqPage /> },
          { path: 'settings/profile', element: <RequireAdminAccess><ProfilePage /></RequireAdminAccess> },
          { path: 'welcome', element: <RequireAdminAccess><WelcomeLandingPage /></RequireAdminAccess> },
          { path: 'welcome/packages', element: <RequireAdminAccess><WelcomePackagesPage /></RequireAdminAccess> },
          { path: 'welcome/summary', element: <RequireAdminAccess><WelcomeOrderSummaryPage /></RequireAdminAccess> },
          { path: 'welcome/event', element: <RequireAdminAccess><WelcomeEventSetupPage /></RequireAdminAccess> },
        ],
      },
    ],
  },
  {
    path: '*',
    element: <Navigate to={ADMIN_PUBLIC_LANDING_PATH} replace />,
  },
]);
