import React from 'react';
import { ErrorBoundary } from '@/components/ErrorBoundary';
import { ProtectedRoute } from '@/components/auth/ProtectedRoute';
import {
  LazyLogin,
  LazyRegister,
  LazyForgotPassword,
  LazyVerifyEmail,
  LazyResetPassword,
  LazyDashboard,
  LazyChat,
  LazyChatJoin,
  LazyLibrary,
  LazyProfile,
  LazySettings,
  LazySessions,
  LazyNotFound,
  LazyServerError,
  LazyUserProfile,
  LazyRoles,
  LazyTrackDetail,
  LazyPlaylistRoutes,
  LazyMarketplace,
  LazySearch,
  LazyNotifications,
  LazyAnalytics,
  LazyWebhooks,
  LazyAdminDashboard,
  LazyAdminTransfers,
  LazyDesignSystemDemo,
  LazySocial,
  LazyFeed,
  LazyDiscover,
  LazySellerDashboard,
  LazyWishlist,
  LazyPurchases,
  LazyProductDetail,
  LazyCheckoutComplete,
  LazyQueue,
  LazyDeveloper,
  LazyGear,
  LazyLive,
  LazyGoLive,
  LazyCloud,
} from '@/components/ui/LazyComponent';
import { PublicRoute } from './PublicRoute';
import { ProtectedLayoutRoute } from './ProtectedLayoutRoute';
import type { RouteEntry } from './types';

function wrapPublic(element: React.ReactNode): React.ReactNode {
  return (
    <PublicRoute>
      <ErrorBoundary>{element}</ErrorBoundary>
    </PublicRoute>
  );
}

function wrapProtected(element: React.ReactNode): React.ReactNode {
  return (
    <ProtectedRoute>
      <ProtectedLayoutRoute>
        <ErrorBoundary>{element}</ErrorBoundary>
      </ProtectedLayoutRoute>
    </ProtectedRoute>
  );
}

export function getPublicRoutes(): RouteEntry[] {
  return [
    { path: '/login', element: wrapPublic(<LazyLogin />) },
    { path: '/register', element: wrapPublic(<LazyRegister />) },
    { path: '/forgot-password', element: wrapPublic(<LazyForgotPassword />) },
    { path: '/verify-email', element: wrapPublic(<LazyVerifyEmail />) },
    { path: '/reset-password', element: wrapPublic(<LazyResetPassword />) },
  ];
}

export function getPublicStandaloneRoutes(): RouteEntry[] {
  return [
    { path: '/design-system', element: <ErrorBoundary><LazyDesignSystemDemo /></ErrorBoundary> },
    { path: '/u/:username', element: <ErrorBoundary><LazyUserProfile /></ErrorBoundary> },
  ];
}

export function getProtectedRoutes(): RouteEntry[] {
  return [
    { path: '/dashboard', element: wrapProtected(<LazyDashboard />) },
    { path: '/marketplace', element: wrapProtected(<LazyMarketplace />) },
    { path: '/marketplace/products/:id', element: wrapProtected(<LazyProductDetail />) },
    { path: '/sell', element: wrapProtected(<LazySellerDashboard onCreateProduct={() => {}} />) },
    { path: '/wishlist', element: wrapProtected(<LazyWishlist />) },
    { path: '/purchases', element: wrapProtected(<LazyPurchases />) },
    { path: '/checkout/complete', element: wrapProtected(<LazyCheckoutComplete />) },
    { path: '/chat/join/:token', element: wrapProtected(<LazyChatJoin />) },
    { path: '/chat', element: wrapProtected(<LazyChat />) },
    { path: '/library', element: wrapProtected(<LazyLibrary />) },
    { path: '/profile', element: wrapProtected(<LazyProfile />) },
    { path: '/settings', element: wrapProtected(<LazySettings />) },
    { path: '/settings/sessions', element: wrapProtected(<LazySessions />) },
    { path: '/admin/roles', element: wrapProtected(<LazyRoles />) },
    { path: '/tracks/:id', element: wrapProtected(<LazyTrackDetail />) },
    { path: '/playlists/*', element: wrapProtected(<LazyPlaylistRoutes />) },
    { path: '/search', element: wrapProtected(<LazySearch />) },
    { path: '/notifications', element: wrapProtected(<LazyNotifications />) },
    { path: '/analytics', element: wrapProtected(<LazyAnalytics />) },
    { path: '/webhooks', element: wrapProtected(<LazyWebhooks />) },
    { path: '/admin', element: wrapProtected(<LazyAdminDashboard />) },
    { path: '/admin/transfers', element: wrapProtected(<LazyAdminTransfers />) },
    { path: '/social', element: wrapProtected(<LazySocial />) },
    { path: '/feed', element: wrapProtected(<LazyFeed />) },
    { path: '/discover', element: wrapProtected(<LazyDiscover />) },
    { path: '/queue', element: wrapProtected(<LazyQueue />) },
    { path: '/developer', element: wrapProtected(<LazyDeveloper />) },
    // Gear: connected to backend inventory API
    { path: '/gear', element: wrapProtected(<LazyGear />) },
    // Live: connected to backend live streams API
    { path: '/live/go-live', element: wrapProtected(<LazyGoLive />) },
    { path: '/live', element: wrapProtected(<LazyLive />) },
    // Cloud: connected to backend cloud storage API
    { path: '/cloud', element: wrapProtected(<LazyCloud />) },
  ];
}

export function getErrorRoutes(): RouteEntry[] {
  return [
    { path: '/404', element: <ErrorBoundary><LazyNotFound /></ErrorBoundary> },
    { path: '/500', element: <ErrorBoundary><LazyServerError /></ErrorBoundary> },
  ];
}
