import type { ReactNode } from 'react';
import {
  AppShell,
  AppShellMain,
  AppShellMobileToggle,
  AppShellNav,
  AppShellNavItem,
  AppShellOverlay,
  AppShellPageHeader,
  AppShellSidebar,
  AppShellSkipLink,
  Badge,
  Button,
  Card,
  CardDescription,
  CardHeader,
  CardTitle,
  Checkbox,
  ConfirmDialog,
  DataList,
  DataListItem,
  DataListMeta,
  DataTable,
  DataTableBody,
  DataTableCell,
  DataTableHead,
  DataTableHeader,
  DataTableRow,
  DiffCard,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuGroup,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuRadioGroup,
  DropdownMenuSeparator,
  DropdownMenuSub,
  DropdownMenuSubTrigger,
  DropdownMenuTrigger,
  EmptyState,
  IconButton,
  Input,
  Label,
  ListItemButton,
  LoadingSpinner,
  Modal,
  Panel,
  PanelBody,
  PanelDescription,
  PanelHeader,
  PanelTitle,
  RadioGroup,
  RadioGroupItem,
  SegmentedControl,
  Select,
  Separator,
  Sidebar,
  SidebarItem,
  StatCard,
  StatusBadge,
  StatusDot,
  Surface,
  SurfaceList,
  SurfaceListItem,
  Switch,
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
  Textarea,
  Timeline,
  Toast,
  ToastProvider,
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
  dismissToast,
  toast,
  useToast,
  type StatusTone,
} from '@bytelyst/ui';

export {
  AppShell,
  AppShellMain,
  AppShellMobileToggle,
  AppShellNav,
  AppShellNavItem,
  AppShellOverlay,
  AppShellPageHeader,
  AppShellSidebar,
  AppShellSkipLink,
  Badge,
  Button,
  Card,
  CardDescription,
  CardHeader,
  CardTitle,
  Checkbox,
  ConfirmDialog,
  DataList,
  DataListItem,
  DataListMeta,
  DataTable,
  DataTableBody,
  DataTableCell,
  DataTableHead,
  DataTableHeader,
  DataTableRow,
  DiffCard,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuGroup,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuRadioGroup,
  DropdownMenuSeparator,
  DropdownMenuSub,
  DropdownMenuSubTrigger,
  DropdownMenuTrigger,
  EmptyState,
  IconButton,
  Input,
  Label,
  ListItemButton,
  LoadingSpinner,
  Modal,
  Panel,
  PanelBody,
  PanelDescription,
  PanelHeader,
  PanelTitle,
  RadioGroup,
  RadioGroupItem,
  SegmentedControl,
  Select,
  Separator,
  Sidebar,
  SidebarItem,
  StatCard,
  StatusBadge,
  StatusDot,
  Surface,
  SurfaceList,
  SurfaceListItem,
  Switch,
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
  Textarea,
  Timeline,
  Toast,
  ToastProvider,
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
  dismissToast,
  toast,
  useToast,
};

export type { StatusTone };

export type ProductStatus =
  | 'active'
  | 'approved'
  | 'blocked'
  | 'buy'
  | 'cancelled'
  | 'connected'
  | 'danger'
  | 'degraded'
  | 'disabled'
  | 'error'
  | 'failed'
  | 'idle'
  | 'info'
  | 'live'
  | 'neutral'
  | 'off'
  | 'ok'
  | 'paper'
  | 'pending'
  | 'rejected'
  | 'sell'
  | 'success'
  | 'synced'
  | 'warning';

const productStatusTone: Record<ProductStatus, StatusTone> = {
  active: 'success',
  approved: 'success',
  blocked: 'danger',
  buy: 'success',
  cancelled: 'neutral',
  connected: 'success',
  danger: 'danger',
  degraded: 'warning',
  disabled: 'neutral',
  error: 'danger',
  failed: 'danger',
  idle: 'neutral',
  info: 'info',
  live: 'warning',
  neutral: 'neutral',
  off: 'neutral',
  ok: 'success',
  paper: 'info',
  pending: 'warning',
  rejected: 'danger',
  sell: 'danger',
  success: 'success',
  synced: 'success',
  warning: 'warning',
};

export function statusToneFor(status: ProductStatus | string | null | undefined): StatusTone {
  if (!status) return 'neutral';

  const normalized = status.trim().toLowerCase().replace(/[\s_]+/g, '-') as ProductStatus;
  return productStatusTone[normalized] ?? 'neutral';
}

export function ProductStatusBadge({
  status,
  children,
}: {
  status: ProductStatus | string | null | undefined;
  children?: ReactNode;
}) {
  return (
    <StatusBadge tone={statusToneFor(status)} dot>
      {children ?? status ?? 'Unknown'}
    </StatusBadge>
  );
}
