import { View, Text, Pressable, StyleSheet } from 'react-native';
import Ionicons from '@expo/vector-icons/Ionicons';
import { RepeatLastMeal } from '../../../api/tracking';
import { colors, spacing, radii, typography } from '../../../theme';
import { CATEGORY_OPTIONS } from '../utils/category';

type Props = {
  meal: RepeatLastMeal | null;
  onAdd: (meal: RepeatLastMeal) => void;
  disabled: boolean;
};

// Répétition auto "comme d'habitude ?" (TrackingService::getRepeatLastMealSuggestion(),
// priorité 2 : après le partage explicite, avant la suggestion familiale automatique) —
// visible uniquement pendant le créneau petit-déj/collation actif, jamais toute la
// journée. Un seul tap, pas de popin de quantité (contrairement à "Ajouter à nouveau" du
// journal, `RequickModal.tsx`) : conforme à "ajout rapide", l'utilisateur ajuste ensuite
// via le journal une fois l'entrée créée. Pas de bouton refuser — l'entrée disparaît
// d'elle-même hors du créneau ou dès qu'un ajout est fait dans cette catégorie.
export function RepeatLastMealCard({ meal, onAdd, disabled }: Props) {
  if (meal === null) return null;

  const label = CATEGORY_OPTIONS.find((c) => c.value === meal.category)?.label ?? meal.category;

  return (
    <View style={styles.card}>
      <View style={styles.icon}>
        <Ionicons name="refresh" size={18} color={colors.primary} />
      </View>
      <View style={styles.main}>
        <Text style={styles.text}>
          Comme d'habitude pour votre <Text style={styles.bold}>{label.toLowerCase()}</Text> ?
        </Text>
        <Text style={styles.sub}>
          {meal.nom} · {Math.round(meal.kcal)} kcal
        </Text>
      </View>
      <Pressable style={styles.addBtn} onPress={() => onAdd(meal)} disabled={disabled} hitSlop={6}>
        <Ionicons name="add" size={16} color="#fff" />
      </Pressable>
    </View>
  );
}

const styles = StyleSheet.create({
  card: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.sm,
    backgroundColor: colors.card,
    borderRadius: radii.lg,
    padding: spacing.md,
  },
  icon: {
    width: 32,
    height: 32,
    borderRadius: radii.pill,
    backgroundColor: colors.primary + '1a',
    alignItems: 'center',
    justifyContent: 'center',
  },
  main: { flex: 1 },
  text: { ...typography.caption, color: colors.text },
  bold: { fontWeight: '700' },
  sub: { ...typography.caption, color: colors.textMuted, marginTop: 1 },
  addBtn: {
    width: 26,
    height: 26,
    borderRadius: radii.pill,
    backgroundColor: colors.primary,
    alignItems: 'center',
    justifyContent: 'center',
  },
});
