import { AppSidebar } from "@/components/app-sidebar"
import {
  Breadcrumb,
  BreadcrumbItem,
  BreadcrumbLink,
  BreadcrumbList,
  BreadcrumbPage,
  BreadcrumbSeparator,
} from "@/components/ui/breadcrumb"
import {
  SidebarInset,
  SidebarProvider,
} from "@/components/ui/sidebar"
import { Toaster } from "@/components/ui/sonner"
import { TooltipProvider } from "@/components/ui/tooltip"
import { Dashboard } from "@/components/dashboard"
import { NavigationProvider, useNavigation, PageId } from "@/contexts/navigation-context"
import { ProProvider } from "@/contexts/pro-context"
import { LicenseActivationBanner } from "@/components/license-activation-banner"
import { RecaptchaSettings } from "@/features/spam/recaptcha/components/recaptcha-settings"
import { EmailVerificationSettings } from "@/features/spam/email-verification"
import { AddedToCartSettings } from "@/features/popups/added-to-cart"
import { SystemInfoPage } from "@/features/system/system-info"
import { CurrencySwitcherSettings } from "@/features/currencies/currency-switcher"
import { ProductBundlesSettings } from "@/features/products/product-bundles"
import { ProductAddonsSettings } from "@/features/products/product-addons"
import { SubscriptionsSettings } from "@/features/products/subscriptions"
import { QuantityLimitsSettings } from "@/features/products/quantity-limits"
import { FieldEditorSettings } from "@/features/checkout/field-editor"
import { CookieConsentSettings } from "@/features/compliance/cookie-consent"
import { BackInStockSettings } from "@/features/notifications/back-in-stock"
import { AbandonedCartSettings } from "@/features/notifications/abandoned-cart"
import { ShipmentTrackingSettings } from "@/features/shipping/shipment-tracking"
import { RegistrationFormSettings } from "@/features/customization/registration-form"
import { WishlistSettings } from "@/features/products/wishlist"
import { AjaxSearchSettings } from "@/features/products/ajax-search"
import { GlobalsSettings, GlobalsProvider } from "@/features/globals"

// Page title mapping
const pageTitles: Record<PageId, { breadcrumbs: string[] }> = {
  'dashboard': { breadcrumbs: ['Dashboard', 'Overview'] },
  'dashboard/overview': { breadcrumbs: ['Dashboard', 'Overview'] },
  'dashboard/analytics': { breadcrumbs: ['Dashboard', 'Analytics'] },
  'dashboard/reports': { breadcrumbs: ['Dashboard', 'Reports'] },
  'globals': { breadcrumbs: ['Globals'] },
  'spam/recaptcha': { breadcrumbs: ['Spam', 'reCAPTCHA'] },
  'spam/email-verification': { breadcrumbs: ['Spam', 'Email Verification'] },
  'popups/added-to-cart': { breadcrumbs: ['Popups', 'Add to Cart Popup'] },
  'notifications/back-in-stock': { breadcrumbs: ['Notifications', 'Back In Stock'] },
  'notifications/abandoned-cart': { breadcrumbs: ['Notifications', 'Cart Recovery'] },
  'shipping/tracking': { breadcrumbs: ['Shipping', 'Tracking'] },
  'system/info': { breadcrumbs: ['System', 'Info'] },
  'currencies/switcher': { breadcrumbs: ['Currencies', 'Currency Switcher'] },
  'orders': { breadcrumbs: ['Orders', 'All Orders'] },
  'products': { breadcrumbs: ['Products', 'All Products'] },
  'products/bundles': { breadcrumbs: ['Products', 'Product Bundles'] },
  'products/addons': { breadcrumbs: ['Products', 'Product Addons'] },
  'products/subscriptions': { breadcrumbs: ['Products', 'Subscriptions'] },
  'products/quantity-limits': { breadcrumbs: ['Products', 'Quantity Limits'] },
  'products/wishlist': { breadcrumbs: ['Products', 'Wishlist'] },
  'products/ajax-search': { breadcrumbs: ['Products', 'Ajax Search'] },
  'checkout/field-editor': { breadcrumbs: ['Checkout', 'Field Editor'] },
  'customization/registration-form': { breadcrumbs: ['Customization', 'Registration Form'] },
  'compliance/cookie-consent': { breadcrumbs: ['Compliance', 'Cookie Consent'] },
}

function PageContent() {
  const { currentPage } = useNavigation()
  
  switch (currentPage) {
    case 'globals':
      return <GlobalsSettings />
    case 'spam/recaptcha':
      return <RecaptchaSettings />
    case 'spam/email-verification':
      return <EmailVerificationSettings />
    case 'popups/added-to-cart':
      return <AddedToCartSettings />
    case 'system/info':
      return <SystemInfoPage />
    case 'currencies/switcher':
      return <CurrencySwitcherSettings />
    case 'products/bundles':
      return <ProductBundlesSettings />
    case 'products/addons':
      return <ProductAddonsSettings />
    case 'products/subscriptions':
      return <SubscriptionsSettings />
    case 'products/quantity-limits':
      return <QuantityLimitsSettings />
    case 'products/wishlist':
      return <WishlistSettings />
    case 'products/ajax-search':
      return <AjaxSearchSettings />
    case 'checkout/field-editor':
      return <FieldEditorSettings />
    case 'customization/registration-form':
      return <RegistrationFormSettings />
    case 'compliance/cookie-consent':
      return <CookieConsentSettings />
    case 'notifications/back-in-stock':
      return <BackInStockSettings />
    case 'notifications/abandoned-cart':
      return <AbandonedCartSettings />
    case 'shipping/tracking':
      return <ShipmentTrackingSettings />
    case 'dashboard':
    case 'dashboard/analytics':
    case 'dashboard/reports':
    default:
      return <Dashboard />
  }
}

function AppHeader() {
  const { currentPage } = useNavigation()
  const pageInfo = pageTitles[currentPage] || { breadcrumbs: ['Dashboard', 'Overview'] }
  
  return (
    <header className="sticky top-0 z-50 flex h-16 shrink-0 items-center gap-2 transition-[width,height] ease-linear group-has-data-[collapsible=icon]/sidebar-wrapper:h-12 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 border-b">
      <div className="flex items-center gap-2 px-4">
        <Breadcrumb>
          <BreadcrumbList>
            <BreadcrumbItem className="hidden md:block">
              <BreadcrumbLink href="#">
                Swift Commerce
              </BreadcrumbLink>
            </BreadcrumbItem>
            {pageInfo.breadcrumbs.map((crumb, index) => (
              <span key={index} className="contents">
                <BreadcrumbSeparator className="hidden md:block" />
                <BreadcrumbItem>
                  {index === pageInfo.breadcrumbs.length - 1 ? (
                    <BreadcrumbPage>{crumb}</BreadcrumbPage>
                  ) : (
                    <BreadcrumbLink href="#" className="hidden md:block">
                      {crumb}
                    </BreadcrumbLink>
                  )}
                </BreadcrumbItem>
              </span>
            ))}
          </BreadcrumbList>
        </Breadcrumb>
      </div>
    </header>
  )
}

function AppContent() {
  return (
    <GlobalsProvider>
      <SidebarProvider>
        <AppSidebar />
        <SidebarInset>
          <AppHeader />
          <LicenseActivationBanner />
          <PageContent />
        </SidebarInset>
      </SidebarProvider>
    </GlobalsProvider>
  )
}

function App() {
  return (
    <div className="Swift Commerce-app">
      <ProProvider>
        <TooltipProvider delayDuration={200}>
          <NavigationProvider>
            <AppContent />
            <Toaster />
          </NavigationProvider>
        </TooltipProvider>
      </ProProvider>
    </div>
  )
}

export default App
