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

type Props = {
  dayLabel: string;
  categories: CategoryOption[];
  checkedCategories: Record<string, boolean>;
  currentProfile: ColoredOption;
  onCycleProfile: () => void;
  onToggleCategory: (category: string) => void;
};

export function DayRhythmRow({ dayLabel, categories, checkedCategories, currentProfile, onCycleProfile, onToggleCategory }: Props) {
  return (
    <View style={styles.row}>
      <View style={styles.header}>
        <Text style={styles.dayName}>{dayLabel}</Text>
        <Pressable style={[styles.profileBadge, { borderColor: currentProfile.color }]} onPress={onCycleProfile}>
          <View style={[styles.dot, { backgroundColor: currentProfile.color }]} />
          <Text style={[styles.profileLabel, { color: currentProfile.color }]}>{currentProfile.label}</Text>
        </Pressable>
      </View>
      <View style={styles.pills}>
        {categories.map((category) => {
          const on = checkedCategories[category.value] ?? false;
          const color = category.gradient[1];
          return (
            <Pressable
              key={category.value}
              style={[styles.pill, on && { backgroundColor: color, borderColor: color }]}
              onPress={() => onToggleCategory(category.value)}
            >
              <Text style={[styles.pillText, on && styles.pillTextOn]}>{category.label}</Text>
            </Pressable>
          );
        })}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  row: { gap: spacing.xs },
  header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' },
  dayName: { ...typography.body, fontWeight: '700', color: colors.text },
  profileBadge: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: 6,
    borderWidth: 1,
    borderRadius: radii.pill,
    paddingHorizontal: spacing.sm,
    paddingVertical: 4,
  },
  dot: { width: 8, height: 8, borderRadius: 4 },
  profileLabel: { fontSize: 12, fontWeight: '700' },
  pills: { flexDirection: 'row', flexWrap: 'wrap', gap: spacing.xs },
  pill: {
    paddingHorizontal: spacing.sm,
    paddingVertical: 6,
    borderRadius: radii.pill,
    borderWidth: 1,
    borderColor: colors.border,
    backgroundColor: colors.card,
  },
  pillText: { fontSize: 13, fontWeight: '600', color: colors.text },
  pillTextOn: { color: '#fff' },
});
