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

const MOODS = [
  { value: 'light', label: 'Diet / Léger' },
  { value: 'normal', label: 'Normal' },
  { value: 'comfort', label: 'Réconfortant' },
];

export type InfoData = {
  name: string;
  categories: string[];
  mood: string;
  prepTimeMinutes: string;
  servings: string;
};

type Props = {
  data: InfoData;
  onChange: (patch: Partial<InfoData>) => void;
  onNext: () => void;
  onBack: () => void;
};

// Calque `templates/recipe_import/index.html.twig` (panneau 1 "Informations").
export function InfoStep({ data, onChange, onNext, onBack }: Props) {
  function toggleCategory(value: string) {
    const on = data.categories.includes(value);
    if (on && data.categories.length <= 1) return; // au moins une catégorie requise
    onChange({ categories: on ? data.categories.filter((c) => c !== value) : [...data.categories, value] });
  }

  return (
    <View style={styles.wrap}>
      <Text style={styles.kick}>Étape 2 / 6</Text>
      <Text style={styles.h2}>Informations générales</Text>
      <Text style={styles.help}>Vérifiez et corrigez les informations extraites par l'IA.</Text>

      <View style={styles.field}>
        <Text style={styles.lbl}>Nom de la recette</Text>
        <TextInput
          style={styles.input}
          value={data.name}
          onChangeText={(name) => onChange({ name })}
          placeholder="Titre de la recette"
          placeholderTextColor={colors.textMuted}
        />
      </View>

      <View style={styles.field}>
        <Text style={styles.lbl}>Catégories</Text>
        <Text style={styles.hint}>Une recette peut convenir à plusieurs repas.</Text>
        <View style={styles.chipRow}>
          {RECIPE_CATEGORY_OPTIONS.map((opt) => {
            const on = data.categories.includes(opt.value);
            return (
              <Pressable key={opt.value} style={[styles.chip, on && styles.chipOn]} onPress={() => toggleCategory(opt.value)}>
                <Text style={[styles.chipText, on && styles.chipTextOn]}>{opt.label}</Text>
              </Pressable>
            );
          })}
        </View>
      </View>

      <View style={styles.field}>
        <Text style={styles.lbl}>Ambiance</Text>
        <View style={styles.chipRow}>
          {MOODS.map((m) => {
            const on = data.mood === m.value;
            return (
              <Pressable key={m.value} style={[styles.chip, on && styles.chipOn]} onPress={() => onChange({ mood: m.value })}>
                <Text style={[styles.chipText, on && styles.chipTextOn]}>{m.label}</Text>
              </Pressable>
            );
          })}
        </View>
      </View>

      <View style={styles.row2}>
        <View style={[styles.field, { flex: 1 }]}>
          <Text style={styles.lbl}>Préparation (min)</Text>
          <TextInput
            style={styles.input}
            value={data.prepTimeMinutes}
            onChangeText={(v) => onChange({ prepTimeMinutes: v.replace(/[^0-9]/g, '') })}
            placeholder="30"
            placeholderTextColor={colors.textMuted}
            keyboardType="number-pad"
          />
        </View>
        <View style={[styles.field, { flex: 1 }]}>
          <Text style={styles.lbl}>Nombre de parts</Text>
          <TextInput
            style={styles.input}
            value={data.servings}
            onChangeText={(v) => onChange({ servings: v.replace(/[^0-9]/g, '') })}
            placeholder="4"
            placeholderTextColor={colors.textMuted}
            keyboardType="number-pad"
          />
        </View>
      </View>

      <View style={styles.actions}>
        <Pressable style={styles.ghostBtn} onPress={onBack}>
          <Text style={styles.ghostBtnText}>Retour</Text>
        </Pressable>
        <Pressable style={styles.primaryBtn} onPress={onNext}>
          <Text style={styles.primaryBtnText}>Étapes de préparation →</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 },
  field: { gap: 4, marginBottom: spacing.xs },
  lbl: { fontSize: 13, fontWeight: '700', color: colors.text },
  hint: { ...typography.caption, fontSize: 12, color: colors.textMuted, marginBottom: 2 },
  input: { borderWidth: 1, borderColor: colors.border, borderRadius: radii.sm, paddingHorizontal: spacing.sm, paddingVertical: spacing.sm + 2, color: colors.text, backgroundColor: colors.card },
  chipRow: { flexDirection: 'row', flexWrap: 'wrap', gap: 8 },
  chip: { paddingVertical: 8, paddingHorizontal: spacing.sm + 2, borderRadius: radii.pill, borderWidth: 1, borderColor: colors.border, backgroundColor: colors.card },
  chipOn: { backgroundColor: '#eef0fe', borderColor: colors.primary },
  chipText: { fontSize: 12.5, fontWeight: '700', color: colors.textMuted },
  chipTextOn: { color: colors.primary },
  row2: { flexDirection: 'row', gap: spacing.sm },
  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' },
  primaryBtnText: { color: '#fff', fontWeight: '700', fontSize: 14 },
});
