import { Modal, View, Text, Pressable, StyleSheet, ActivityIndicator } from 'react-native';
import { colors, spacing, radii, typography } from '../theme';

// Boîte de confirmation React Native (bottom sheet), pas `Alert.alert`/`confirmAlert` —
// ces derniers sont réservés aux erreurs/confirmations destructives ponctuelles ; celle-ci
// sert quand on veut une vraie UI cohérente avec le reste de l'app (voir AddMealModal.tsx,
// MoveMealModal.tsx pour le même gabarit backdrop/sheet).
type Props = {
  visible: boolean;
  title: string;
  message?: string;
  confirmLabel: string;
  cancelLabel?: string;
  loading?: boolean;
  onConfirm: () => void;
  onCancel: () => void;
};

export function ConfirmModal({ visible, title, message, confirmLabel, cancelLabel = 'Annuler', loading, onConfirm, onCancel }: Props) {
  return (
    <Modal visible={visible} transparent animationType="slide" onRequestClose={onCancel}>
      <Pressable style={styles.backdrop} onPress={onCancel}>
        <Pressable style={styles.sheet} onPress={(e) => e.stopPropagation()}>
          <Text style={styles.title}>{title}</Text>
          {message ? <Text style={styles.message}>{message}</Text> : null}
          <View style={styles.actions}>
            <Pressable style={styles.ghostBtn} onPress={onCancel} disabled={loading}>
              <Text style={styles.ghostBtnText}>{cancelLabel}</Text>
            </Pressable>
            <Pressable style={[styles.primaryBtn, loading && styles.primaryBtnDisabled]} disabled={loading} onPress={onConfirm}>
              {loading ? <ActivityIndicator color="#fff" /> : <Text style={styles.primaryBtnText}>{confirmLabel}</Text>}
            </Pressable>
          </View>
        </Pressable>
      </Pressable>
    </Modal>
  );
}

const styles = StyleSheet.create({
  backdrop: { flex: 1, backgroundColor: 'rgba(20, 20, 26, 0.4)', justifyContent: 'flex-end' },
  sheet: { backgroundColor: colors.card, borderTopLeftRadius: radii.xl, borderTopRightRadius: radii.xl, padding: spacing.lg },
  title: { ...typography.title, fontSize: 18, color: colors.text, marginBottom: spacing.sm },
  message: { ...typography.body, fontSize: 14, color: colors.textMuted, lineHeight: 20, marginBottom: spacing.lg },
  actions: { flexDirection: 'row', gap: spacing.sm },
  ghostBtn: { paddingVertical: spacing.sm + 2, paddingHorizontal: spacing.md, borderRadius: radii.sm, borderWidth: 1, borderColor: colors.border, alignItems: 'center', justifyContent: 'center' },
  ghostBtnText: { ...typography.body, 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.5 },
  primaryBtnText: { color: '#fff', fontWeight: '700', fontSize: 14 },
});
