import React, { useState } from 'react';
import {
  Modal,
  View,
  Text,
  TouchableOpacity,
  StyleSheet,
  Image,
  ActivityIndicator,
} from 'react-native';

const TrainingPromptModal = ({ visible, onDoIt, onNotNow, dueDate, item }) => {
  const [loading, setLoading] = useState(false);

  return (
    <Modal visible={visible} transparent animationType="fade">
      <View style={styles.overlay}>
        <View style={styles.container}>
          <View style={styles.imageWrapper}>
            {loading && (
              <ActivityIndicator
                size="small"
                color="#0000ff"
                style={styles.loader}
              />
            )}

            <Image
              source={{ uri: item?.ImageIcon }}
              style={styles.bear}
              onLoadStart={() => setLoading(true)}
              onLoadEnd={() => setLoading(false)}
              onError={() => setLoading(false)} // in case image fails
            />
          </View>

          <Text style={styles.title}>{item?.Subject}</Text>
          <Text style={styles.desc}>{item?.Message}</Text>

          <TouchableOpacity
            style={styles.doItBtn}
            onPress={() => onDoIt(item)}
          >
            <Text style={styles.doItText}>Let's do it</Text>
          </TouchableOpacity>

          {item?.PopupId != 1 && (
            <TouchableOpacity
              style={styles.notNowBtn}
              onPress={() => onNotNow(item)}
            >
              <Text style={styles.notNowText}>Not now</Text>
            </TouchableOpacity>
          )}
        </View>
      </View>
    </Modal>
  );
};

const styles = StyleSheet.create({
  overlay: {
    flex: 1,
    backgroundColor: 'rgba(0,0,0,0.2)',
    justifyContent: 'center',
    alignItems: 'center',
  },
  container: {
    width: 320,
    backgroundColor: '#fff',
    borderRadius: 20,
    alignItems: 'center',
    padding: 24,
    elevation: 8,
  },
  bear: {
    width: 140,
    height: 140,
    marginBottom: 10,
    resizeMode: 'contain',
  },
  imageWrapper: {
    justifyContent: 'center',
    alignItems: 'center',
  },
  loader: {
    position: 'absolute', // overlay on top of image
  },
  title: {
    fontSize: 18,
    fontWeight: 'bold',
    marginBottom: 8,
    color: '#222',
  },
  desc: {
    fontSize: 14,
    color: '#444',
    textAlign: 'center',
    marginBottom: 24,
  },
  date: {
    fontWeight: 'bold',
    color: '#1793d1',
  },
  doItBtn: {
    backgroundColor: '#1793d1',
    borderRadius: 24,
    width: '100%',
    alignItems: 'center',
    paddingVertical: 12,
    marginBottom: 10,
  },
  doItText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: 'bold',
  },
  notNowBtn: {
    borderColor: '#1793d1',
    borderWidth: 1,
    borderRadius: 24,
    width: '100%',
    alignItems: 'center',
    paddingVertical: 12,
  },
  notNowText: {
    color: '#1793d1',
    fontSize: 16,
    fontWeight: 'bold',
  },
});

export default TrainingPromptModal;
