import { View, Text, StyleSheet } from 'react-native';
import { spacing } from '../../../theme';
import { trackingColors, trackingRadii } from '../theme';

type Props = {
  consumed: number;
  target: number | null;
  remaining: number | null;
  pct: number | null;
};

// Calque le bloc "IL TE RESTE" du mockup 2a : nombre plat + barre de progression
// rectiligne, remplace l'ancien anneau SVG dégradé (voir plan crystalline-weaving-hammock).
// "Équilibre de l'assiette" est désormais une carte séparée (`PlateBalanceCard.tsx`).
export function CalorieHero({ consumed, target, remaining, pct }: Props) {
  const hasTarget = target !== null;
  const over = hasTarget && remaining !== null && remaining < 0;
  const ratio = hasTarget && target! > 0 ? consumed / target! : 0;
  const clampedRatio = Math.max(0, Math.min(1, ratio));
  const barColor = over ? '#ef4444' : trackingColors.primary;

  return (
    <View style={styles.card}>
      <Text style={styles.label}>{hasTarget ? 'IL TE RESTE' : 'CALORIES CONSOMMÉES'}</Text>

      <View style={styles.valueRow}>
        <Text style={[styles.value, over && styles.valueOver]}>
          {hasTarget ? Math.abs(Math.round(remaining ?? 0)) : Math.round(consumed)}
        </Text>
        <Text style={styles.unit}>kcal</Text>
      </View>

      {hasTarget && (
        <>
          <View style={styles.track}>
            <View style={[styles.fill, { width: `${clampedRatio * 100}%`, backgroundColor: barColor }]} />
          </View>
          <View style={styles.footRow}>
            <Text style={styles.footText}>
              {Math.round(consumed)} consommé — {pct ?? 0} %
            </Text>
            <Text style={styles.footText}>objectif {target}</Text>
          </View>
        </>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  card: { gap: spacing.sm },
  label: { fontSize: 12, fontWeight: '700', letterSpacing: 1, color: trackingColors.textMuted },
  valueRow: { flexDirection: 'row', alignItems: 'flex-end', gap: 8 },
  value: { fontSize: 64, fontWeight: '800', letterSpacing: -1, lineHeight: 64, color: trackingColors.text },
  valueOver: { color: '#ef4444' },
  unit: { fontSize: 15, fontWeight: '700', color: trackingColors.textMuted, paddingBottom: 8 },
  track: { height: 10, borderRadius: trackingRadii.pill, backgroundColor: trackingColors.border, overflow: 'hidden' },
  fill: { height: '100%', borderRadius: trackingRadii.pill },
  footRow: { flexDirection: 'row', justifyContent: 'space-between' },
  footText: { fontSize: 13, fontWeight: '600', color: trackingColors.textMuted2 },
});
