import React from 'react';
import { createBrowserRouter, Outlet, Navigate } from 'react-router-dom';
import LoginPage from './pages/LoginPage';
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';

function RequireAuth() {
  const token = localStorage.getItem('ta_token');
  if (!token) return <Navigate to="/admin/login" replace />;
  return <Outlet />;
}

export const router = createBrowserRouter([
  { path: '/admin/login', element: <LoginPage /> },
  {
    path: '/admin',
    element: <RequireAuth />,
    children: [
      { index: true, element: <EventsPage /> },
      { path: 'events', element: <EventsPage /> },
      { path: 'events/new', element: <EventFormPage /> },
      { path: 'events/edit', element: <EventFormPage /> },
      { path: 'events/view', element: <EventDetailPage /> },
      { path: 'events/photos', element: <EventPhotosPage /> },
      { path: 'settings', element: <SettingsPage /> },
    ],
  },
  { path: '*', element: <Navigate to="/admin" replace /> },
]);
