import { View, Text, Pressable, TextInput, StyleSheet } from 'react-native';
import Ionicons from '@expo/vector-icons/Ionicons';
import { colors, spacing, radii, typography } from '../../../../theme';

type Props = {
  steps: string[];
  onChange: (steps: string[]) => void;
  onNext: () => void;
  onBack: () => void;
};

// Calque `templates/recipe_import/index.html.twig` (panneau 2 "Étapes").
export function StepsStep({ steps, onChange, onNext, onBack }: Props) {
  function updateStep(index: number, text: string) {
    const next = [...steps];
    next[index] = text;
    onChange(next);
  }

  function removeStep(index: number) {
    onChange(steps.filter((_, i) => i !== index));
  }

  return (
    <View style={styles.wrap}>
      <Text style={styles.kick}>Étape 3 / 6</Text>
      <Text style={styles.h2}>Étapes de préparation</Text>
      <Text style={styles.help}>Modifiez, supprimez ou ajoutez des étapes.</Text>

      <View style={styles.list}>
        {steps.map((step, i) => (
          <View key={i} style={styles.card}>
            <Text style={styles.n}>{i + 1}</Text>
            <TextInput
              style={styles.textarea}
              value={step}
              onChangeText={(text) => updateStep(i, text)}
              placeholder="Décrivez cette étape…"
              placeholderTextColor={colors.textMuted}
              multiline
            />
            <Pressable onPress={() => removeStep(i)} hitSlop={6}>
              <Ionicons name="trash-outline" size={17} color={colors.textMuted} />
            </Pressable>
          </View>
        ))}
      </View>

      <Pressable style={styles.addBtn} onPress={() => onChange([...steps, ''])}>
        <Ionicons name="add" size={16} color={colors.primary} />
        <Text style={styles.addBtnText}>Ajouter une étape</Text>
      </Pressable>

      <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}>Ingrédients →</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 },
  list: { gap: spacing.sm },
  card: { flexDirection: 'row', alignItems: 'flex-start', gap: spacing.sm, borderWidth: 1, borderColor: colors.border, borderRadius: radii.md, padding: spacing.sm, backgroundColor: colors.card },
  n: { width: 22, height: 22, borderRadius: 11, backgroundColor: '#eef0fe', color: colors.primary, textAlign: 'center', lineHeight: 22, fontSize: 12, fontWeight: '800' },
  textarea: { flex: 1, ...typography.body, fontSize: 14, color: colors.text, minHeight: 40, textAlignVertical: 'top' },
  addBtn: { flexDirection: 'row', alignItems: 'center', gap: 6, alignSelf: 'flex-start', paddingVertical: 6 },
  addBtnText: { fontSize: 13.5, 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' },
  primaryBtnText: { color: '#fff', fontWeight: '700', fontSize: 14 },
});
