import { View, Text, Pressable, StyleSheet } from 'react-native';
import Ionicons from '@expo/vector-icons/Ionicons';
import MaterialCommunityIcons from '@expo/vector-icons/MaterialCommunityIcons';
import { FamilySuggestion } from '../../../api/tracking';
import { spacing } from '../../../theme';
import { trackingColors, trackingRadii } from '../theme';
import { foodIconName } from '../utils/foodGlyph';

type Props = {
  suggestions: FamilySuggestion[];
  onAdd: (suggestion: FamilySuggestion) => void;
  onDismiss: (id: number) => void;
  disabled: boolean;
  // Généralisé pour être réutilisé tel quel par les partages explicites
  // (`sharedSuggestions`, priorité 1) en plus des suggestions familiales automatiques
  // (`familySuggestions`, priorité 3) — même forme de donnée et mêmes actions
  // (ajouter/ignorer), seul le texte change.
  title?: string;
  verb?: string;
  icon?: keyof typeof Ionicons.glyphMap;
};

// Équivalent simplifié de `.tpc-fam-sugg` (_timeline.html.twig) : "Ajouter" reprend
// directement nom/kcal/macros de la suggestion (pas de popin d'édition des ingrédients
// comme côté web — jugé disproportionné ici, l'utilisateur garde la main via le journal
// une fois l'entrée ajoutée).
export function FamilySuggestionsCard({
  suggestions,
  onAdd,
  onDismiss,
  disabled,
  title = 'Suggestions de la famille',
  verb = 'a ajouté',
  icon = 'sparkles',
}: Props) {
  if (suggestions.length === 0) return null;

  return (
    <View style={styles.card}>
      <View style={styles.header}>
        <Text style={styles.title}>{title.toUpperCase()}</Text>
      </View>

      {suggestions.map((s) => (
        <View key={s.id} style={styles.row}>
          <View style={styles.icon}>
            <MaterialCommunityIcons name={foodIconName(s.glyph.icon) as any} size={18} color={trackingColors.primary} />
          </View>
          <View style={styles.main}>
            <Text style={styles.text}>
              <Text style={styles.bold}>{s.fromUser}</Text> {verb}
            </Text>
            <Text style={styles.name}>{s.nom}</Text>
            <Text style={styles.kcal}>{Math.round(s.kcal)} kcal</Text>
          </View>
          <Pressable style={styles.addBtn} onPress={() => onAdd(s)} disabled={disabled} hitSlop={6}>
            <Ionicons name="add" size={18} color="#fff" />
          </Pressable>
          <Pressable onPress={() => onDismiss(s.id)} disabled={disabled} hitSlop={6}>
            <Ionicons name="close" size={16} color={trackingColors.textMuted} />
          </Pressable>
        </View>
      ))}
    </View>
  );
}

const styles = StyleSheet.create({
  card: { gap: 10 },
  header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' },
  title: { fontSize: 11, fontWeight: '700', color: trackingColors.textMuted, letterSpacing: 1 },
  row: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.sm,
    backgroundColor: trackingColors.cardWhite,
    borderWidth: 1,
    borderColor: trackingColors.border,
    borderRadius: trackingRadii.cardSm - 2,
    padding: spacing.sm + 6,
  },
  icon: { width: 38, height: 38, borderRadius: trackingRadii.pill, backgroundColor: trackingColors.primarySoft, alignItems: 'center', justifyContent: 'center' },
  main: { flex: 1 },
  text: { fontSize: 12, fontWeight: '600', color: trackingColors.textMuted },
  bold: { fontWeight: '700', color: trackingColors.text },
  name: { fontSize: 14, fontWeight: '700', color: trackingColors.text, lineHeight: 18 },
  kcal: { fontSize: 12, fontWeight: '600', color: trackingColors.textMuted, marginTop: 2 },
  addBtn: {
    width: 34,
    height: 34,
    borderRadius: trackingRadii.pill,
    backgroundColor: trackingColors.primary,
    alignItems: 'center',
    justifyContent: 'center',
  },
});
