import { View, Text, StyleSheet } from 'react-native';
import Ionicons from '@expo/vector-icons/Ionicons';
import { AdviceGroup } from '../../../api/tracking';
import { spacing } from '../../../theme';
import { trackingColors, trackingRadii } from '../theme';

type Props = {
  adviceGroups: AdviceGroup[];
};

// Même transformation que `g.label|replace({'des ': '', 'un ': ''})|capitalize`
// (templates/tracking/_summary.html.twig) : "des légumes" → "Légumes".
function formatCriterionLabel(label: string): string {
  const stripped = label.replace(/^(des |un )/, '');
  return stripped.charAt(0).toUpperCase() + stripped.slice(1);
}

// Extrait du mockup 2a : "Équilibre de l'assiette" est une carte blanche à part
// (auparavant fusionnée dans `CalorieHero`) — 3 pastilles rondes, checkmark plein
// si `group.ok`, sinon anneau bordé vide.
export function PlateBalanceCard({ adviceGroups }: Props) {
  const covered = adviceGroups.filter((g) => g.ok).length;

  return (
    <View style={styles.card}>
      <View style={styles.header}>
        <Text style={styles.title}>Équilibre de l'assiette</Text>
        <Text style={styles.count}>
          {covered}/{adviceGroups.length}
        </Text>
      </View>

      <View style={styles.row}>
        {adviceGroups.map((group) => (
          <View key={group.id} style={styles.criterion}>
            <View style={[styles.badge, group.ok && styles.badgeOk]}>
              {group.ok && <Ionicons name="checkmark" size={22} color="#fff" />}
            </View>
            <Text style={styles.label}>{formatCriterionLabel(group.label)}</Text>
            <Text style={[styles.state, group.ok && styles.stateOk]}>
              {group.grams !== null && group.gramsTarget
                ? `${group.gramsTarget} g cible`
                : group.ok
                  ? 'validé'
                  : 'à ajouter'}
            </Text>
          </View>
        ))}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  card: {
    backgroundColor: trackingColors.cardWhite,
    borderWidth: 1,
    borderColor: trackingColors.border,
    borderRadius: trackingRadii.card,
    padding: spacing.lg,
    gap: spacing.md,
  },
  header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' },
  title: { fontSize: 16, fontWeight: '800', color: trackingColors.text },
  count: { fontSize: 12, fontWeight: '700', color: trackingColors.textMuted },
  row: { flexDirection: 'row', justifyContent: 'space-around', paddingTop: 4 },
  criterion: { alignItems: 'center', gap: 10, width: 96 },
  badge: {
    width: 58,
    height: 58,
    borderRadius: 29,
    borderWidth: 2,
    borderColor: trackingColors.borderStrong,
    backgroundColor: trackingColors.card,
    alignItems: 'center',
    justifyContent: 'center',
  },
  badgeOk: { backgroundColor: trackingColors.success, borderColor: trackingColors.success },
  label: { fontSize: 13, fontWeight: '700', color: trackingColors.text },
  state: { fontSize: 11, fontWeight: '600', color: '#A8A8B4', marginTop: -4 },
  stateOk: { color: trackingColors.success, fontWeight: '700' },
});
