import React from 'react';
import { Badge, BadgeProps } from '@mantine/core';

export type StatusVariant = 'status' | 'role' | 'runtime' | 'type' | 'severity' | 'execution';

export interface StatusBadgeProps extends Omit<BadgeProps, 'color' | 'children'> {
  value: string;
  variant?: StatusVariant;
  customColorMap?: Record<string, string>;
}

// Standardized color mappings across all Skybridge apps
const COLOR_MAPS: Record<StatusVariant, Record<string, string>> = {
  // Common status colors (used in User, KMS, etc.)
  status: {
    active: 'green',
    inactive: 'gray',
    pending: 'yellow',
    suspended: 'red',
    enabled: 'green',
    disabled: 'gray',
    online: 'green',
    offline: 'gray',
    running: 'green',
    stopped: 'gray',
    paused: 'yellow',
    failed: 'red',
    success: 'green',
    completed: 'green',
    error: 'red',
    warning: 'yellow',
    info: 'blue',
  },
  
  // User roles (User Management)
  role: {
    admin: 'red',
    moderator: 'orange',
    user: 'blue',
    viewer: 'gray',
    owner: 'purple',
    editor: 'cyan',
    contributor: 'teal',
    guest: 'gray',
  },
  
  // Application types (KMS)
  type: {
    static: 'blue',
    user: 'cyan',
    service: 'green',
    application: 'purple',
    api: 'orange',
    web: 'teal',
    mobile: 'pink',
    desktop: 'indigo',
  },
  
  // Runtime environments (FaaS)
  runtime: {
    'nodejs18': 'green',
    'nodejs20': 'lime',
    'python3.9': 'blue',
    'python3.11': 'indigo',
    'go1.20': 'cyan',
    'go1.21': 'teal',
    'java11': 'orange',
    'java17': 'red',
    'dotnet6': 'purple',
    'dotnet7': 'violet',
    'rust': 'dark',
    'php8': 'grape',
  },
  
  // Audit severity levels (KMS Audit)
  severity: {
    critical: 'red',
    high: 'orange',
    medium: 'yellow',
    low: 'blue',
    info: 'gray',
    debug: 'dark',
  },
  
  // Function execution states (FaaS)
  execution: {
    queued: 'gray',
    running: 'blue',
    succeeded: 'green',
    failed: 'red',
    timeout: 'orange',
    cancelled: 'yellow',
    retrying: 'cyan',
  },
};

// Default fallback colors for unknown values
const DEFAULT_COLORS: Record<StatusVariant, string> = {
  status: 'gray',
  role: 'blue',
  runtime: 'blue',
  type: 'blue', 
  severity: 'gray',
  execution: 'gray',
};

const StatusBadge: React.FC<StatusBadgeProps> = ({
  value,
  variant = 'status',
  customColorMap,
  size = 'sm',
  ...badgeProps
}) => {
  if (!value) {
    return <Badge color="gray" size={size} {...badgeProps}>-</Badge>;
  }

  // Determine color from appropriate mapping
  const colorMap = customColorMap || COLOR_MAPS[variant] || COLOR_MAPS.status;
  const color = colorMap[value.toLowerCase()] || DEFAULT_COLORS[variant];
  
  // Format display value (capitalize first letter, handle special cases)
  const displayValue = formatDisplayValue(value, variant);

  return (
    <Badge 
      color={color} 
      size={size}
      variant="filled"
      {...badgeProps}
    >
      {displayValue}
    </Badge>
  );
};

// Helper function to format display values consistently
const formatDisplayValue = (value: string, variant: StatusVariant): string => {
  // Special formatting for specific variants
  switch (variant) {
    case 'runtime':
      // Format runtime names nicely (e.g., "nodejs18" -> "Node.js 18")
      if (value.startsWith('nodejs')) return `Node.js ${value.replace('nodejs', '')}`;
      if (value.startsWith('python')) return `Python ${value.replace('python', '')}`;
      if (value.startsWith('go')) return `Go ${value.replace('go', '')}`;
      if (value.startsWith('java')) return `Java ${value.replace('java', '')}`;
      if (value.startsWith('dotnet')) return `.NET ${value.replace('dotnet', '')}`;
      break;
      
    case 'type':
      // Capitalize application types
      if (value === 'api') return 'API';
      if (value === 'web') return 'Web App';
      if (value === 'mobile') return 'Mobile App';
      break;
  }
  
  // Default: capitalize first letter
  return value.charAt(0).toUpperCase() + value.slice(1).toLowerCase();
};

export default StatusBadge;

// Export additional utilities for advanced use cases
export { COLOR_MAPS, DEFAULT_COLORS };

// Convenience components for common use cases
export const UserRoleBadge: React.FC<Omit<StatusBadgeProps, 'variant'>> = (props) => (
  <StatusBadge {...props} variant="role" />
);

export const ApplicationTypeBadge: React.FC<Omit<StatusBadgeProps, 'variant'>> = (props) => (
  <StatusBadge {...props} variant="type" />
);

export const RuntimeBadge: React.FC<Omit<StatusBadgeProps, 'variant'>> = (props) => (
  <StatusBadge {...props} variant="runtime" />
);

export const ExecutionStatusBadge: React.FC<Omit<StatusBadgeProps, 'variant'>> = (props) => (
  <StatusBadge {...props} variant="execution" />
);

export const SeverityBadge: React.FC<Omit<StatusBadgeProps, 'variant'>> = (props) => (
  <StatusBadge {...props} variant="severity" />
);