import { View, Text, Pressable, StyleSheet } from 'react-native';
import { colors, spacing, radii, typography } from '../../../theme';

type Option = { value: string; label: string; description?: string; color?: string };

type Props = {
  options: Option[];
  value: string | null;
  onChange: (value: string) => void;
  disabled?: boolean;
};

export function SegmentedButtons({ options, value, onChange, disabled }: Props) {
  return (
    <View style={styles.row}>
      {options.map((option) => {
        const selected = option.value === value;
        const selectedColor = option.color ?? colors.primary;
        return (
          <Pressable
            key={option.value}
            style={[styles.segment, selected && { borderColor: selectedColor, backgroundColor: `${selectedColor}1a` }]}
            onPress={() => onChange(option.value)}
            disabled={disabled}
          >
            <Text style={[styles.label, selected && { color: selectedColor }]}>{option.label}</Text>
            {option.description ? <Text style={styles.description}>{option.description}</Text> : null}
          </Pressable>
        );
      })}
    </View>
  );
}

const styles = StyleSheet.create({
  row: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.xs },
  segment: {
    paddingHorizontal: spacing.sm,
    paddingVertical: 6,
    borderRadius: radii.sm,
    borderWidth: 1,
    borderColor: colors.border,
    backgroundColor: colors.card,
    alignItems: 'center',
  },
  label: { ...typography.caption, fontSize: 13, fontWeight: '700', color: colors.text },
  description: { ...typography.caption, fontSize: 11, color: colors.textMuted },
});
