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

type Props = {
  category: string;
  onChange: (value: string) => void;
};

// Champ + picker "moment de la journée" (Petit déjeuner/Collation/Déjeuner/Dîner),
// extrait de `PhotoCaptureModal.tsx` (dupliqué là-bas entre `ProductResultView`/
// `AiResultView`) pour être réutilisé aussi par `EntryDetailModal.tsx` — gère son propre
// état d'ouverture, pas besoin de le remonter chez l'appelant.
//
// Rendu en pleine largeur avec une icône horloge + libellé explicite ("Moment de la
// journée", pas juste "Catégorie" qui prêtait à confusion avec un type d'aliment) suite
// à un retour utilisateur : le champ existait déjà mais passait inaperçu (bouton étroit,
// libellé ambigu, placé après une liste d'ingrédients potentiellement longue).
export function CategoryPickerField({ category, onChange }: Props) {
  const [open, setOpen] = useState(false);
  const label = CATEGORY_OPTIONS.find((c) => c.value === category)?.label ?? category;

  return (
    <View style={styles.fieldBlock}>
      <Pressable style={styles.categoryButton} onPress={() => setOpen(true)}>
        <View style={styles.categoryButtonLeft}>
          <Ionicons name="time-outline" size={17} color={colors.primary} />
          <Text style={styles.label}>Moment de la journée</Text>
        </View>
        <View style={styles.categoryButtonRight}>
          <Text style={styles.categoryButtonText}>{label}</Text>
          <Ionicons name="chevron-down" size={16} color={colors.textMuted} />
        </View>
      </Pressable>

      <Modal visible={open} transparent animationType="fade" onRequestClose={() => setOpen(false)}>
        <Pressable style={styles.modalScrim} onPress={() => setOpen(false)}>
          <View style={styles.modalCard}>
            {CATEGORY_OPTIONS.map((opt) => (
              <Pressable
                key={opt.value}
                style={styles.modalOption}
                onPress={() => {
                  onChange(opt.value);
                  setOpen(false);
                }}
              >
                <Text style={[styles.modalOptionText, opt.value === category && styles.modalOptionTextActive]}>{opt.label}</Text>
                {opt.value === category && <Ionicons name="checkmark" size={16} color={colors.primary} />}
              </Pressable>
            ))}
          </View>
        </Pressable>
      </Modal>
    </View>
  );
}

const styles = StyleSheet.create({
  fieldBlock: { marginTop: spacing.xs, marginBottom: spacing.xs },
  label: { ...typography.caption, color: colors.textMuted, fontWeight: '700', letterSpacing: 0.3 },
  categoryButton: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    borderWidth: 1,
    borderColor: colors.primary,
    backgroundColor: colors.primary + '0f',
    borderRadius: radii.md,
    paddingHorizontal: spacing.md,
    paddingVertical: spacing.sm + 2,
  },
  categoryButtonLeft: { flexDirection: 'row', alignItems: 'center', gap: 6 },
  categoryButtonRight: { flexDirection: 'row', alignItems: 'center', gap: 4 },
  categoryButtonText: { ...typography.body, fontWeight: '700', color: colors.text },
  modalScrim: { flex: 1, backgroundColor: 'rgba(20,22,45,0.4)', justifyContent: 'center', padding: spacing.lg },
  modalCard: { backgroundColor: colors.card, borderRadius: radii.lg, padding: spacing.xs },
  modalOption: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingHorizontal: spacing.md,
    paddingVertical: spacing.sm + 4,
  },
  modalOptionText: { ...typography.body, color: colors.text },
  modalOptionTextActive: { color: colors.primary, fontWeight: '700' },
});
