import { View, Text, StyleSheet } from 'react-native';
import Svg, { Circle } from 'react-native-svg';
import { colors, spacing } from '../../../theme';
import { RecipeNutrition } from '../../../api/recipes';

const SIZE = 150;
const STROKE = 16;
const MACRO_COLORS: Record<string, string> = { protein: '#6366f1', carbs: '#f59e0b', fat: '#f43f5e' };
const MACRO_LABELS: Record<string, string> = { protein: 'Protéines', carbs: 'Glucides', fat: 'Lipides' };

type Props = {
  totals: RecipeNutrition['totals'];
  segments: { key: string; offset: number; length: number }[];
};

// Calque `.rc-donut`/`.rc-leg` (assets/styles/recipe-catalog.css,
// templates/recipe_catalog/show.html.twig) — répartition énergétique protéines/
// glucides/lipides, données déjà calculées côté serveur (`RecipeNutritionService`).
export function NutritionDonut({ totals, segments }: Props) {
  const radius = (SIZE - STROKE) / 2;
  const circumference = 2 * Math.PI * radius;
  const center = SIZE / 2;

  return (
    <View style={styles.row}>
      <View style={{ width: SIZE, height: SIZE }}>
        <Svg width={SIZE} height={SIZE}>
          <Circle cx={center} cy={center} r={radius} stroke="#eef0f4" strokeWidth={STROKE} fill="none" />
          {segments.map((segment) => (
            <Circle
              key={segment.key}
              cx={center}
              cy={center}
              r={radius}
              stroke={MACRO_COLORS[segment.key] ?? colors.textMuted}
              strokeWidth={STROKE}
              fill="none"
              strokeDasharray={`${segment.length * circumference} ${circumference - segment.length * circumference}`}
              strokeDashoffset={-(segment.offset * circumference)}
              rotation={-90}
              origin={`${center}, ${center}`}
            />
          ))}
        </Svg>
        <View style={[StyleSheet.absoluteFill, styles.center]}>
          <Text style={styles.kcal}>{Math.round(totals.kcal)}</Text>
          <Text style={styles.kcalLabel}>kcal / part</Text>
        </View>
      </View>

      <View style={styles.legend}>
        {(['protein', 'carbs', 'fat'] as const).map((key) => (
          <View key={key} style={styles.legendRow}>
            <View style={[styles.swatch, { backgroundColor: MACRO_COLORS[key] }]} />
            <Text style={styles.legendLabel}>{MACRO_LABELS[key]}</Text>
            <Text style={styles.legendValue}>{totals[key].toFixed(1)} g</Text>
          </View>
        ))}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  row: { flexDirection: 'row', alignItems: 'center', gap: spacing.lg, flexWrap: 'wrap' },
  center: { alignItems: 'center', justifyContent: 'center' },
  kcal: { fontSize: 24, fontWeight: '800', color: colors.text },
  kcalLabel: { fontSize: 11, fontWeight: '600', color: colors.textMuted },
  legend: { gap: spacing.xs },
  legendRow: { flexDirection: 'row', alignItems: 'center', gap: 8 },
  swatch: { width: 10, height: 10, borderRadius: 3 },
  legendLabel: { fontSize: 13, fontWeight: '600', color: colors.text, flex: 1 },
  legendValue: { fontSize: 13, fontWeight: '700', color: colors.textMuted },
});
