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

type Props = {
  meals: PlannedMeal[];
  onToggle: (entryId: number) => void;
  disabled: boolean;
};

// Calque `.tpc-sec-t`/`.tpc-up`/`.tpc-upcard` (assets/styles/tracking.css,
// templates/tracking/_timeline.html.twig) : titre de section nu (pas de carte
// englobante) suivi d'une carte par repas, vignette dégradée par catégorie.
export function PlannedMealsCard({ meals, onToggle, disabled }: Props) {
  if (meals.length === 0) return null;

  return (
    <View style={styles.section}>
      <View style={styles.sectionTitle}>
        <Ionicons name="calendar-outline" size={14} color={trackingColors.textMuted} />
        <Text style={styles.sectionTitleText}>À VENIR</Text>
      </View>

      <View style={styles.list}>
        {meals.map((meal) => (
          <View key={meal.entryId} style={styles.card}>
            <View style={styles.thumb}>
              <Ionicons name="restaurant" size={18} color={trackingColors.primary} />
            </View>

            <View style={styles.body}>
              <Text style={styles.tag}>
                {meal.repas} · {meal.heure}
                {meal.quantite ? ` · ${meal.quantite}` : ''}
              </Text>
              <Text style={styles.name}>{meal.nom}</Text>
              <Text style={styles.kcal}>{Math.round(meal.kcal)} kcal</Text>
            </View>

            <Pressable style={styles.eatBtn} onPress={() => onToggle(meal.entryId)} disabled={disabled}>
              <Text style={styles.eatBtnText}>J'ai mangé</Text>
            </Pressable>
          </View>
        ))}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  section: { gap: spacing.sm },
  sectionTitle: { flexDirection: 'row', alignItems: 'center', gap: 6 },
  sectionTitleText: { fontSize: 11, fontWeight: '700', letterSpacing: 1, color: trackingColors.textMuted },
  list: { gap: spacing.sm },
  card: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.sm,
    backgroundColor: trackingColors.cardWhite,
    borderWidth: 1,
    borderColor: trackingColors.border,
    borderRadius: trackingRadii.cardSm - 2,
    padding: spacing.sm + 4,
  },
  thumb: {
    width: 44,
    height: 44,
    borderRadius: trackingRadii.pill,
    backgroundColor: trackingColors.primarySoft,
    alignItems: 'center',
    justifyContent: 'center',
  },
  body: { flex: 1, minWidth: 0 },
  tag: { fontSize: 10, fontWeight: '700', textTransform: 'uppercase', letterSpacing: 0.3, color: trackingColors.textMuted },
  name: { fontSize: 15, fontWeight: '700', color: trackingColors.text, marginTop: 2 },
  kcal: { fontSize: 12, fontWeight: '600', color: trackingColors.textMuted, marginTop: 2 },
  eatBtn: {
    height: 38,
    paddingHorizontal: 14,
    borderRadius: trackingRadii.pill,
    borderWidth: 1,
    borderColor: '#D6CEFF',
    alignItems: 'center',
    justifyContent: 'center',
  },
  eatBtnText: { fontSize: 12, fontWeight: '700', color: trackingColors.primary },
});
