import React from 'react';
import { createBrowserRouter, Outlet, Navigate, useLocation } from 'react-router-dom';
import LoginPage from './pages/LoginPage';
import DashboardPage from './pages/DashboardPage';
import EventsPage from './pages/EventsPage';
import SettingsPage from './pages/SettingsPage';
import EventFormPage from './pages/EventFormPage';
import EventPhotosPage from './pages/EventPhotosPage';
import EventDetailPage from './pages/EventDetailPage';
import EventMembersPage from './pages/EventMembersPage';
import EventTasksPage from './pages/EventTasksPage';
import BillingPage from './pages/BillingPage';
import TasksPage from './pages/TasksPage';
import TaskCollectionsPage from './pages/TaskCollectionsPage';
import EmotionsPage from './pages/EmotionsPage';
import AuthCallbackPage from './pages/AuthCallbackPage';
import WelcomeTeaserPage from './pages/WelcomeTeaserPage';
import LogoutPage from './pages/LogoutPage';
import { useAuth } from './auth/context';
import {
  ADMIN_BASE_PATH,
  ADMIN_HOME_PATH,
  ADMIN_LOGIN_PATH,
  ADMIN_PUBLIC_LANDING_PATH,
} from './constants';
import WelcomeLandingPage from './onboarding/pages/WelcomeLandingPage';
import WelcomePackagesPage from './onboarding/pages/WelcomePackagesPage';
import WelcomeEventSetupPage from './onboarding/pages/WelcomeEventSetupPage';
import WelcomeOrderSummaryPage from './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_PATH} state={{ from: location }} replace />;
  }

  return <Outlet />;
}

function LandingGate() {
  const { status } = 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') {
    return <Navigate to={ADMIN_HOME_PATH} replace />;
  }

  return <WelcomeTeaserPage />;
}

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