import { View, Text, Image, StyleSheet } from 'react-native';
import { ProductCardData } from '../api/productCard';
import { colors, spacing, radii, typography } from '../theme';

// Fiche produit (photo, Nutri-Score, synthèse diététique) partagée entre
// food-lookup (recherche/scan sur "Produits/Aliments") et tracking (résultat
// d'un scan code-barres dans "Mon suivi") — même affichage des deux côtés,
// seul ce qui suit (portion + ajout au suivi vs. rien de plus) diffère par
// appelant. Voir `mobile/src/api/productCard.ts`.

const GRADE_COLORS: Record<string, string> = {
  A: '#2e7d32',
  B: '#8bc34a',
  C: '#fbc02d',
  D: '#fb8c00',
  E: '#e53935',
};

const LEVEL_COLORS: Record<string, string> = { high: colors.danger, mid: '#f59e0b', ok: colors.success };

export function ProductCard({ product }: { product: ProductCardData }) {
  const gradeColor = product.nutriscore ? GRADE_COLORS[product.nutriscore.grade.toUpperCase()] ?? colors.textMuted : null;

  return (
    <View style={styles.card}>
      <View style={styles.headRow}>
        {product.imageUrl ? <Image source={{ uri: product.imageUrl }} style={styles.image} /> : null}
        <View style={styles.headInfo}>
          <Text style={styles.name}>{product.name}</Text>
          {product.brands ? <Text style={styles.brands}>{product.brands}</Text> : null}
        </View>
        {product.nutriscore ? (
          <View style={[styles.gradeBadge, { backgroundColor: gradeColor ?? colors.textMuted }]}>
            <Text style={styles.gradeText}>{product.nutriscore.grade.toUpperCase()}</Text>
          </View>
        ) : null}
      </View>

      {product.synthesis.verdict ? (
        <View style={styles.verdictBox}>
          <Text style={styles.verdict}>{product.synthesis.verdict}</Text>
          {product.synthesis.hint ? <Text style={styles.hint}>{product.synthesis.hint}</Text> : null}
        </View>
      ) : null}

      <View style={styles.macros}>
        <View style={styles.macro}>
          <Text style={styles.macroValue}>{product.macros.caloriesPer100 ?? '—'} kcal</Text>
          <Text style={styles.macroLabel}>/ 100 g</Text>
        </View>
        <View style={styles.macro}>
          <Text style={styles.macroValue}>{product.macros.proteinPer100 ?? '—'} g</Text>
          <Text style={styles.macroLabel}>Protéines</Text>
        </View>
        <View style={styles.macro}>
          <Text style={styles.macroValue}>{product.macros.carbsPer100 ?? '—'} g</Text>
          <Text style={styles.macroLabel}>Glucides</Text>
        </View>
        <View style={styles.macro}>
          <Text style={styles.macroValue}>{product.macros.fatPer100 ?? '—'} g</Text>
          <Text style={styles.macroLabel}>Lipides</Text>
        </View>
      </View>

      {product.synthesis.flags.length > 0 ? (
        <View style={styles.flags}>
          {product.synthesis.flags.map((flag, index) => (
            <View key={`${flag.label}-${index}`} style={styles.flagRow}>
              <View style={[styles.flagDot, { backgroundColor: LEVEL_COLORS[flag.level] ?? colors.textMuted }]} />
              <Text style={styles.flagLabel}>{flag.label}</Text>
              <Text style={styles.flagValue}>{flag.value}</Text>
            </View>
          ))}
        </View>
      ) : null}

      {product.categories.length > 0 ? (
        <Text style={styles.categories}>{product.categories.slice(0, 4).join(' · ')}</Text>
      ) : null}
    </View>
  );
}

const styles = StyleSheet.create({
  card: { backgroundColor: colors.card, borderRadius: radii.lg, padding: spacing.md, gap: spacing.sm },
  headRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm },
  image: { width: 56, height: 56, borderRadius: radii.md, backgroundColor: colors.background },
  headInfo: { flex: 1, gap: 2 },
  name: { ...typography.body, fontSize: 16, fontWeight: '700', color: colors.text },
  brands: { ...typography.caption, color: colors.textMuted },
  gradeBadge: { width: 32, height: 32, borderRadius: 16, alignItems: 'center', justifyContent: 'center' },
  gradeText: { color: '#fff', fontWeight: '700', fontSize: 15 },
  verdictBox: { gap: 2 },
  verdict: { ...typography.body, fontSize: 14, fontWeight: '700', color: colors.text },
  hint: { ...typography.caption, color: colors.textMuted },
  macros: { flexDirection: 'row', justifyContent: 'space-between' },
  macro: { alignItems: 'center', gap: 1 },
  macroValue: { ...typography.body, fontSize: 14, fontWeight: '700', color: colors.text },
  macroLabel: { ...typography.caption, fontSize: 11, color: colors.textMuted },
  flags: { gap: 4 },
  flagRow: { flexDirection: 'row', alignItems: 'center', gap: spacing.xs },
  flagDot: { width: 8, height: 8, borderRadius: 4 },
  flagLabel: { ...typography.caption, fontSize: 12, fontWeight: '600', color: colors.text, flex: 1 },
  flagValue: { ...typography.caption, fontSize: 12, color: colors.textMuted },
  categories: { ...typography.caption, fontSize: 11, color: colors.textMuted },
});
