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

type Props = {
  consumedMl: number;
  targetMl: number | null;
  pct: number | null;
  remainingMl: number | null;
  quickAdds: HydrationQuickAdd[];
  onQuickAdd: (item: HydrationQuickAdd) => void;
  disabled: boolean;
};

// Carte blanche bordée (mockup 2a) — remplace l'ancien dégradé bleu, garde toute
// la logique/props existantes.
export function HydrationCard({ consumedMl, targetMl, pct, remainingMl, quickAdds, onQuickAdd, disabled }: Props) {
  const reached = targetMl !== null && consumedMl >= targetMl;

  return (
    <View style={styles.card}>
      <View style={styles.headerRow}>
        <Text style={styles.headerText}>BOISSONS</Text>
        {pct !== null && <Text style={styles.pctText}>{pct} %</Text>}
      </View>

      <View style={styles.valueRow}>
        <Text style={styles.value}>{formatLiters(consumedMl)}</Text>
        {targetMl !== null && <Text style={styles.valueTarget}> / {formatLiters(targetMl)}</Text>}
      </View>

      {targetMl !== null && (
        <View style={styles.progressTrack}>
          <View style={[styles.progressFill, { width: `${Math.max(0, Math.min(100, pct ?? 0))}%` }]} />
        </View>
      )}

      <Text style={styles.caption}>
        {reached ? 'Objectif atteint, bravo !' : remainingMl !== null ? `${remainingMl} ml restants pour atteindre la recommandation` : ' '}
      </Text>

      <View style={styles.quickAddRow}>
        {quickAdds.map((item) => (
          <Pressable key={item.label} style={styles.quickAddButton} onPress={() => onQuickAdd(item)} disabled={disabled}>
            <Text style={styles.quickAddText}>+ {item.label}</Text>
          </Pressable>
        ))}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  card: {
    backgroundColor: trackingColors.cardWhite,
    borderWidth: 1,
    borderColor: trackingColors.border,
    borderRadius: trackingRadii.cardSm,
    padding: spacing.md + 2,
    gap: 10,
  },
  headerRow: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' },
  headerText: { fontSize: 11, fontWeight: '700', color: trackingColors.textMuted, letterSpacing: 1 },
  pctText: { fontSize: 11, fontWeight: '700', color: trackingColors.hydration },
  valueRow: { flexDirection: 'row', alignItems: 'flex-end', gap: 8 },
  value: { fontSize: 26, fontWeight: '800', color: trackingColors.text, lineHeight: 26 },
  valueTarget: { fontSize: 13, fontWeight: '700', color: trackingColors.textMuted, paddingBottom: 2 },
  progressTrack: { height: 8, borderRadius: trackingRadii.pill, backgroundColor: trackingColors.border, overflow: 'hidden' },
  progressFill: { height: '100%', borderRadius: trackingRadii.pill, backgroundColor: trackingColors.hydration },
  caption: { fontSize: 12, color: trackingColors.textMuted, fontWeight: '600' },
  quickAddRow: { flexDirection: 'row', flexWrap: 'wrap', gap: 6, marginTop: 2 },
  quickAddButton: {
    borderWidth: 1,
    borderColor: trackingColors.borderStrong,
    paddingHorizontal: 10,
    paddingVertical: 7,
    borderRadius: 10,
  },
  quickAddText: { fontSize: 12, fontWeight: '700', color: trackingColors.text },
});
