import React, { ReactNode, forwardRef, CSSProperties } from 'react';

export interface HeadingProps {
  children: ReactNode;
  level?: 1 | 2 | 3 | 4 | 5 | 6;
  weight?: 'normal' | 'medium' | 'semibold' | 'bold';
  align?: 'left' | 'center' | 'right';
  uppercase?: boolean;
  intent?: 'primary' | 'telemetry' | 'warning' | 'critical' | 'default' | any;
  className?: string;
  style?: CSSProperties;
  mb?: number | any;
  marginBottom?: number | any;
  mt?: number | any;
  marginTop?: number | any;
  color?: string;
  fontSize?: string | { base: string; sm?: string; md: string; lg?: string; xl?: string };
  letterSpacing?: string;
  truncate?: boolean;
  size?: string;
  icon?: ReactNode;
  id?: string;
  lineHeight?: string | any;
  groupHoverColor?: string | any;
  transition?: boolean | any;
}

/**
 * Heading - Redesigned for "Modern Precision" theme.
 * Includes extensive compatibility props to prevent app-wide breakage.
 */
export const Heading = forwardRef<HTMLHeadingElement, HeadingProps>(({ 
  children, 
  level = 1, 
  weight = 'bold',
  align = 'left',
  uppercase = false,
  intent = 'default',
  className,
  style,
  mb,
  marginBottom,
  mt,
  marginTop,
  color,
  fontSize,
  letterSpacing,
  truncate,
  size,
  icon,
}, ref) => {
  const Tag = `h${level}` as const;
  
  const weightClasses = {
    normal: 'font-normal',
    medium: 'font-medium',
    semibold: 'font-semibold',
    bold: 'font-bold'
  };
  
  const sizeClasses = {
    1: 'text-4xl md:text-5xl tracking-tighter leading-none',
    2: 'text-3xl md:text-4xl tracking-tight leading-tight',
    3: 'text-2xl md:text-3xl tracking-tight leading-snug',
    4: 'text-xl md:text-2xl tracking-normal leading-normal',
    5: 'text-lg md:text-xl tracking-normal leading-normal',
    6: 'text-base md:text-lg tracking-wide leading-normal'
  };

  const intentClasses = {
    default: 'text-[var(--ui-color-text-high)]',
    primary: 'text-[var(--ui-color-intent-primary)]',
    telemetry: 'text-[var(--ui-color-intent-telemetry)]',
    warning: 'text-[var(--ui-color-intent-warning)]',
    critical: 'text-[var(--ui-color-intent-critical)]',
  };

  const getResponsiveFontSize = (fs: HeadingProps['fontSize']) => {
    if (!fs) return '';
    if (typeof fs === 'string') return ''; // Handled in style
    const classes = [];
    if (fs.base) classes.push(`text-${fs.base}`);
    if (fs.sm) classes.push(`sm:text-${fs.sm}`);
    if (fs.md) classes.push(`md:text-${fs.md}`);
    if (fs.lg) classes.push(`lg:text-${fs.lg}`);
    if (fs.xl) classes.push(`xl:text-${fs.xl}`);
    return classes.join(' ');
  };

  const classes = [
    intentClasses[intent as keyof typeof intentClasses] || intentClasses.default,
    weightClasses[weight],
    fontSize ? getResponsiveFontSize(fontSize) : sizeClasses[level],
    align === 'center' ? 'text-center' : (align === 'right' ? 'text-right' : 'text-left'),
    uppercase ? 'uppercase tracking-widest' : '',
    truncate ? 'truncate' : '',
    className,
  ].join(' ');

  const combinedStyle: React.CSSProperties = {
    ...style,
    ...(mb !== undefined ? { marginBottom: typeof mb === 'number' ? `${mb * 0.25}rem` : mb } : {}),
    ...(marginBottom !== undefined ? { marginBottom: typeof marginBottom === 'number' ? `${marginBottom * 0.25}rem` : marginBottom } : {}),
    ...(mt !== undefined ? { marginTop: typeof mt === 'number' ? `${mt * 0.25}rem` : mt } : {}),
    ...(marginTop !== undefined ? { marginTop: typeof marginTop === 'number' ? `${marginTop * 0.25}rem` : marginTop } : {}),
    ...(color ? { color } : {}),
    ...(letterSpacing ? { letterSpacing } : {}),
    ...(typeof fontSize === 'string' ? { fontSize } : {}),
  };

  return (
    <Tag ref={ref} className={classes} style={combinedStyle}>
      <div className="flex items-center gap-2">
        {icon}
        {children}
      </div>
    </Tag>
  );
});

Heading.displayName = 'Heading';
