import { View, Text, Pressable, StyleSheet, ActivityIndicator } from 'react-native';
import { colors, spacing, radii, typography } from '../../../../theme';
import { RECIPE_CATEGORY_OPTIONS } from '../../utils/category';
import { FormState, UNITS } from './types';

const MOOD_LABELS: Record<string, string> = { light: 'Diet / Léger', normal: 'Normal', comfort: 'Réconfortant' };

type Props = {
  data: FormState;
  saving: boolean;
  saveLabel?: string;
  onBack: () => void;
  onSave: () => void;
};

// Calque `templates/recipe_import/index.html.twig` (panneau 4 "Vérification finale").
export function ReviewStep({ data, saving, saveLabel = 'Enregistrer la recette', onBack, onSave }: Props) {
  const categoryLabels = data.categories
    .map((c) => RECIPE_CATEGORY_OPTIONS.find((o) => o.value === c)?.label ?? c)
    .join(' · ');

  return (
    <View style={styles.wrap}>
      <Text style={styles.kick}>Étape 5 / 6</Text>
      <Text style={styles.h2}>Vérification finale</Text>
      <Text style={styles.help}>Relisez la recette avant de l'enregistrer.</Text>

      <View style={styles.card}>
        <Text style={styles.name}>{data.name || '—'}</Text>
        <View style={styles.chipRow}>
          {[categoryLabels, MOOD_LABELS[data.mood] ?? data.mood, data.prepTimeMinutes ? `${data.prepTimeMinutes} min` : null,
            data.servings ? `${data.servings} part${Number(data.servings) > 1 ? 's' : ''}` : null]
            .filter(Boolean)
            .map((label, i) => (
              <View key={i} style={styles.chip}><Text style={styles.chipText}>{label}</Text></View>
            ))}
        </View>

        <Text style={styles.sectionLbl}>Étapes de préparation</Text>
        {data.instructions.length === 0 ? (
          <Text style={styles.empty}>Aucune étape</Text>
        ) : (
          data.instructions.map((step, i) => (
            <Text key={i} style={styles.stepText}>{i + 1}. {step}</Text>
          ))
        )}

        <Text style={styles.sectionLbl}>Ingrédients</Text>
        {data.ingredients.length === 0 ? (
          <Text style={styles.empty}>Aucun ingrédient</Text>
        ) : (
          data.ingredients.map((ing, i) => (
            <Text key={i} style={styles.ingText}>
              <Text style={styles.ingQty}>{ing.quantity || 0} {UNITS.find((u) => u.value === ing.unit)?.label ?? ing.unit}</Text> {ing.name}
            </Text>
          ))
        )}

        <Text style={styles.sectionLbl}>Appareils</Text>
        {data.appliances.length === 0 ? (
          <Text style={styles.empty}>Aucun appareil</Text>
        ) : (
          <View style={styles.chipRow}>
            {data.appliances.map((a) => <View key={a} style={styles.chip}><Text style={styles.chipText}>{a}</Text></View>)}
          </View>
        )}
      </View>

      <View style={styles.actions}>
        <Pressable style={styles.ghostBtn} onPress={onBack} disabled={saving}>
          <Text style={styles.ghostBtnText}>Modifier</Text>
        </Pressable>
        <Pressable style={[styles.primaryBtn, saving && styles.primaryBtnDisabled]} onPress={onSave} disabled={saving}>
          {saving ? <ActivityIndicator color="#fff" /> : <Text style={styles.primaryBtnText}>{saveLabel}</Text>}
        </Pressable>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  wrap: { gap: spacing.sm },
  kick: { ...typography.caption, fontSize: 11.5, fontWeight: '800', color: colors.primary, textTransform: 'uppercase', letterSpacing: 0.5 },
  h2: { ...typography.title, fontSize: 20, color: colors.text },
  help: { ...typography.body, fontSize: 13.5, color: colors.textMuted, marginBottom: spacing.xs },
  card: { borderWidth: 1, borderColor: colors.border, borderRadius: radii.md, backgroundColor: colors.card, padding: spacing.md, gap: 6 },
  name: { ...typography.title, fontSize: 18, color: colors.text },
  chipRow: { flexDirection: 'row', flexWrap: 'wrap', gap: 6, marginBottom: spacing.xs },
  chip: { backgroundColor: '#f1f1f3', borderRadius: radii.pill, paddingVertical: 4, paddingHorizontal: 10 },
  chipText: { fontSize: 11.5, fontWeight: '700', color: colors.textMuted },
  sectionLbl: { fontSize: 11.5, fontWeight: '800', color: colors.textMuted, textTransform: 'uppercase', letterSpacing: 0.4, marginTop: spacing.sm },
  empty: { fontSize: 13, color: colors.textMuted, fontStyle: 'italic' },
  stepText: { fontSize: 13.5, color: colors.text, lineHeight: 19 },
  ingText: { fontSize: 13.5, color: colors.text },
  ingQty: { fontWeight: '700', color: colors.primary },
  actions: { flexDirection: 'row', gap: spacing.sm, marginTop: spacing.sm },
  ghostBtn: { paddingVertical: spacing.sm + 2, paddingHorizontal: spacing.md, borderRadius: radii.sm, borderWidth: 1, borderColor: colors.border, alignItems: 'center', justifyContent: 'center' },
  ghostBtnText: { fontSize: 14, fontWeight: '700', color: colors.textMuted },
  primaryBtn: { flex: 1, paddingVertical: spacing.sm + 2, borderRadius: radii.sm, backgroundColor: colors.primary, alignItems: 'center', justifyContent: 'center' },
  primaryBtnDisabled: { opacity: 0.6 },
  primaryBtnText: { color: '#fff', fontWeight: '700', fontSize: 14 },
});
