/**
 * TabataFit StyledText
 * Unified text component — uses theme for default color
 * Supports typography presets via TYPOGRAPHY tokens
 */

import { Text as RNText, TextStyle, StyleProp } from 'react-native'
import { useThemeColors } from '../theme'
import { TYPOGRAPHY } from '../constants/typography'

type FontWeight = 'regular' | 'medium' | 'semibold' | 'bold'

type TypographyPreset = keyof typeof TYPOGRAPHY

const WEIGHT_MAP: Record<FontWeight, TextStyle['fontWeight']> = {
  regular: '400',
  medium: '500',
  semibold: '600',
  bold: '700',
}

interface StyledTextProps {
  children: React.ReactNode
  /** Typography preset — when provided, overrides size/weight with full TYPOGRAPHY token */
  preset?: TypographyPreset
  /** Font size in px (ignored when preset is set) */
  size?: number
  /** Font weight (ignored when preset is set) */
  weight?: FontWeight
  color?: string
  style?: StyleProp<TextStyle>
  numberOfLines?: number
}

export function StyledText({
  children,
  preset,
  size = 17,
  weight = 'regular',
  color,
  style,
  numberOfLines,
}: StyledTextProps) {
  const colors = useThemeColors()
  const resolvedColor = color ?? colors.text.primary

  const baseStyle: TextStyle = preset
    ? { ...TYPOGRAPHY[preset], color: resolvedColor }
    : { fontSize: size, fontWeight: WEIGHT_MAP[weight], color: resolvedColor }

  return (
    <RNText
      style={[baseStyle, style]}
      numberOfLines={numberOfLines}
    >
      {children}
    </RNText>
  )
}
