import React, { useState, useCallback, useRef, useEffect } from 'react';
import {
  View,
  Text,
  StyleSheet,
  FlatList,
  TextInput,
  TouchableOpacity,
  KeyboardAvoidingView,
  Platform,
  Alert,
  ActivityIndicator,
  Modal,
  ScrollView,
  Animated,
} from 'react-native';
import { Ionicons, Feather } from '@expo/vector-icons';
import { SafeAreaView } from 'react-native-safe-area-context';
import * as SecureStore from 'expo-secure-store';
import * as Speech from 'expo-speech';
// NOTE: expo-speech-recognition does not work in Expo Go, so we disable voice input
// import { ExpoSpeechRecognitionModule, useSpeechRecognitionEvent } from 'expo-speech-recognition';
import { useBeneficiary } from '@/contexts/BeneficiaryContext';
import { api } from '@/services/api';
import { AppColors, BorderRadius, FontSizes, Spacing } from '@/constants/theme';
import type { Message, Beneficiary } from '@/types';

const OLD_API_URL = 'https://eluxnetworks.net/function/well-api/api';

interface ActivityData {
  name: string;
  rooms: Array<{
    name: string;
    data: Array<{
      title: string;
      events: number;
      hours: number;
    }>;
  }>;
}

interface ActivitiesResponse {
  alert_text: string;
  chart_data: ActivityData[];
}

interface VoiceAskResponse {
  ok: boolean;
  response: {
    Command: string;
    body: string;
    name?: string;
    reflected?: string;
    language?: string;
    time?: number;
  };
  status: string;
}

export default function VoiceAIScreen() {
  const { currentBeneficiary, setCurrentBeneficiary } = useBeneficiary();
  const [messages, setMessages] = useState<Message[]>([
    {
      id: '1',
      role: 'assistant',
      content: 'Hello! I\'m Julia, your voice assistant for monitoring your loved ones. Select a beneficiary and type your question to get started.',
      timestamp: new Date(),
    },
  ]);
  const [input, setInput] = useState('');
  const [isSending, setIsSending] = useState(false);
  const [isSpeaking, setIsSpeaking] = useState(false);
  const [showBeneficiaryPicker, setShowBeneficiaryPicker] = useState(false);
  const [beneficiaries, setBeneficiaries] = useState<Beneficiary[]>([]);
  const flatListRef = useRef<FlatList>(null);
  const lastSendTimeRef = useRef<number>(0);
  const pulseAnim = useRef(new Animated.Value(1)).current;
  const SEND_COOLDOWN_MS = 1000;

  // Load beneficiaries on mount
  useEffect(() => {
    loadBeneficiaries();
    return () => {
      Speech.stop();
    };
  }, []);

  const loadBeneficiaries = async () => {
    const response = await api.getAllBeneficiaries();
    if (response.ok && response.data) {
      setBeneficiaries(response.data);
    }
  };

  // Pulse animation for speaking
  useEffect(() => {
    if (isSpeaking) {
      const pulse = Animated.loop(
        Animated.sequence([
          Animated.timing(pulseAnim, {
            toValue: 1.3,
            duration: 500,
            useNativeDriver: true,
          }),
          Animated.timing(pulseAnim, {
            toValue: 1,
            duration: 500,
            useNativeDriver: true,
          }),
        ])
      );
      pulse.start();
      return () => pulse.stop();
    }
  }, [isSpeaking]);

  // Fetch activity data and format it as context
  const getActivityContext = async (token: string, userName: string, deploymentId: string): Promise<string> => {
    try {
      const response = await fetch(OLD_API_URL, {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: new URLSearchParams({
          function: 'activities_report_details',
          user_name: userName,
          token: token,
          deployment_id: deploymentId,
          filter: '0',
        }).toString(),
      });

      const data: ActivitiesResponse = await response.json();

      if (!data.chart_data || data.chart_data.length === 0) {
        return '';
      }

      // Get weekly data (most recent)
      const weeklyData = data.chart_data.find(d => d.name === 'Weekly');
      if (!weeklyData) return '';

      // Build context string
      const lines: string[] = [];
      lines.push(`Alert status: ${data.alert_text || 'No alert'}`);

      // Calculate today's data (last item in each room's data)
      const todayStats: string[] = [];
      for (const room of weeklyData.rooms) {
        const todayData = room.data[room.data.length - 1]; // Today is the last entry
        if (todayData && todayData.hours > 0) {
          todayStats.push(`${room.name}: ${todayData.hours.toFixed(1)} hours (${todayData.events} events)`);
        }
      }

      if (todayStats.length > 0) {
        lines.push(`Today's activity: ${todayStats.join(', ')}`);
      }

      // Calculate weekly totals
      const weeklyStats: string[] = [];
      for (const room of weeklyData.rooms) {
        const totalHours = room.data.reduce((sum, d) => sum + d.hours, 0);
        if (totalHours > 0) {
          weeklyStats.push(`${room.name}: ${totalHours.toFixed(1)} hours total this week`);
        }
      }

      if (weeklyStats.length > 0) {
        lines.push(`Weekly summary: ${weeklyStats.join(', ')}`);
      }

      return lines.join('. ');
    } catch (error) {
      console.log('Failed to fetch activity context:', error);
      return '';
    }
  };

  const sendToVoiceAsk = async (question: string): Promise<string> => {
    const token = await SecureStore.getItemAsync('accessToken');
    const userName = await SecureStore.getItemAsync('userName');

    if (!token || !userName) {
      throw new Error('Please log in to use voice assistant');
    }

    if (!currentBeneficiary?.id) {
      throw new Error('Please select a beneficiary first');
    }

    // Get activity context to include with the question
    const activityContext = await getActivityContext(
      token,
      userName,
      currentBeneficiary.id.toString()
    );

    // Build enhanced question with context
    const beneficiaryName = currentBeneficiary.name || 'the patient';
    let enhancedQuestion = question;

    if (activityContext) {
      enhancedQuestion = `About ${beneficiaryName}: ${activityContext}. User question: ${question}`;
    } else {
      enhancedQuestion = `About ${beneficiaryName}: ${question}`;
    }

    const response = await fetch(OLD_API_URL, {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      body: new URLSearchParams({
        function: 'voice_ask',
        clientId: '001',
        user_name: userName,
        token: token,
        question: enhancedQuestion,
        deployment_id: currentBeneficiary.id.toString(),
      }).toString(),
    });

    const data: VoiceAskResponse = await response.json();

    if (data.ok && data.response?.body) {
      return data.response.body;
    } else if (data.status === '401 Unauthorized') {
      throw new Error('Session expired. Please log in again.');
    } else {
      throw new Error('Could not get response from voice assistant');
    }
  };

  const speakResponse = async (text: string) => {
    setIsSpeaking(true);
    try {
      await Speech.speak(text, {
        language: 'en-US',
        pitch: 1.0,
        rate: 0.9,
        onDone: () => setIsSpeaking(false),
        onError: () => setIsSpeaking(false),
      });
    } catch (error) {
      console.error('TTS error:', error);
      setIsSpeaking(false);
    }
  };

  const handleSend = useCallback(async () => {
    const trimmedInput = input.trim();
    if (!trimmedInput || isSending) return;

    // Require beneficiary selection
    if (!currentBeneficiary?.id) {
      Alert.alert(
        'Select Beneficiary',
        'Please select a beneficiary first to ask questions about their wellbeing.',
        [{ text: 'Select', onPress: () => setShowBeneficiaryPicker(true) }, { text: 'Cancel' }]
      );
      return;
    }

    // Debounce
    const now = Date.now();
    if (now - lastSendTimeRef.current < SEND_COOLDOWN_MS) return;
    lastSendTimeRef.current = now;

    const userMessage: Message = {
      id: Date.now().toString(),
      role: 'user',
      content: trimmedInput,
      timestamp: new Date(),
    };

    setMessages(prev => [...prev, userMessage]);
    setInput('');
    setIsSending(true);

    try {
      const aiResponse = await sendToVoiceAsk(trimmedInput);

      const assistantMessage: Message = {
        id: (Date.now() + 1).toString(),
        role: 'assistant',
        content: aiResponse,
        timestamp: new Date(),
      };
      setMessages(prev => [...prev, assistantMessage]);

      // Speak the response
      await speakResponse(aiResponse);
    } catch (error) {
      const errorMessage: Message = {
        id: (Date.now() + 1).toString(),
        role: 'assistant',
        content: `Sorry, I encountered an error: ${error instanceof Error ? error.message : 'Unknown error'}. Please try again.`,
        timestamp: new Date(),
      };
      setMessages(prev => [...prev, errorMessage]);
    } finally {
      setIsSending(false);
    }
  }, [input, isSending, currentBeneficiary]);

  const selectBeneficiary = (beneficiary: Beneficiary) => {
    setCurrentBeneficiary(beneficiary);
    setShowBeneficiaryPicker(false);

    const welcomeMessage: Message = {
      id: Date.now().toString(),
      role: 'assistant',
      content: `Great! I'm now ready to answer questions about ${beneficiary.name}. ${beneficiary.wellness_descriptor ? `Current status: ${beneficiary.wellness_descriptor}.` : ''} Type your question below!`,
      timestamp: new Date(),
    };
    setMessages(prev => [...prev, welcomeMessage]);

    // Speak the welcome message
    speakResponse(`Ready to answer questions about ${beneficiary.name}`);
  };

  const stopSpeaking = () => {
    Speech.stop();
    setIsSpeaking(false);
  };

  const showMicNotAvailable = () => {
    Alert.alert(
      'Voice Input Not Available',
      'Voice recognition is not available in Expo Go. Please type your question instead.',
      [{ text: 'OK' }]
    );
  };

  const renderMessage = ({ item }: { item: Message }) => {
    const isUser = item.role === 'user';

    return (
      <View style={[styles.messageContainer, isUser ? styles.userMessageContainer : styles.assistantMessageContainer]}>
        {!isUser && (
          <View style={styles.avatarContainer}>
            <Feather name="mic" size={16} color={AppColors.white} />
          </View>
        )}
        <View style={[styles.messageBubble, isUser ? styles.userBubble : styles.assistantBubble]}>
          <Text style={[styles.messageText, isUser ? styles.userMessageText : styles.assistantMessageText]}>
            {item.content}
          </Text>
          <Text style={[styles.timestamp, isUser && styles.userTimestamp]}>
            {item.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
          </Text>
        </View>
      </View>
    );
  };

  return (
    <SafeAreaView style={styles.container} edges={['top']}>
      {/* Header */}
      <View style={styles.header}>
        <View style={styles.headerInfo}>
          <View style={styles.headerAvatar}>
            <Feather name="mic" size={20} color={AppColors.white} />
          </View>
          <View>
            <Text style={styles.headerTitle}>Julia AI</Text>
            <Text style={styles.headerSubtitle}>
              {isSending
                ? 'Thinking...'
                : isSpeaking
                  ? 'Speaking...'
                  : currentBeneficiary
                    ? `Monitoring ${currentBeneficiary.name}`
                    : 'Select a beneficiary'}
            </Text>
          </View>
        </View>
        <TouchableOpacity style={styles.beneficiaryButton} onPress={() => setShowBeneficiaryPicker(true)}>
          <Feather name="users" size={20} color={AppColors.primary} />
          <Text style={styles.beneficiaryButtonText}>
            {currentBeneficiary?.name?.split(' ')[0] || 'Select'}
          </Text>
        </TouchableOpacity>
      </View>

      {/* Messages */}
      <KeyboardAvoidingView
        style={styles.chatContainer}
        behavior={Platform.OS === 'ios' ? 'padding' : undefined}
        keyboardVerticalOffset={Platform.OS === 'ios' ? 90 : 0}
      >
        <FlatList
          ref={flatListRef}
          data={messages}
          keyExtractor={item => item.id}
          renderItem={renderMessage}
          contentContainerStyle={styles.messagesList}
          showsVerticalScrollIndicator={false}
          onContentSizeChange={() => flatListRef.current?.scrollToEnd({ animated: true })}
        />

        {/* Speaking indicator */}
        {isSpeaking && (
          <TouchableOpacity style={styles.speakingIndicator} onPress={stopSpeaking}>
            <Feather name="volume-2" size={20} color="#9B59B6" />
            <Text style={styles.speakingText}>Speaking... tap to stop</Text>
          </TouchableOpacity>
        )}

        {/* Input */}
        <View style={styles.inputContainer}>
          <TouchableOpacity
            style={[styles.micButton, styles.micButtonDisabled]}
            onPress={showMicNotAvailable}
          >
            <Feather name="mic-off" size={24} color={AppColors.textMuted} />
          </TouchableOpacity>

          <TextInput
            style={styles.input}
            placeholder="Type your question..."
            placeholderTextColor={AppColors.textMuted}
            value={input}
            onChangeText={setInput}
            multiline
            maxLength={1000}
            editable={!isSending}
            onSubmitEditing={handleSend}
          />

          <TouchableOpacity
            style={[styles.sendButton, (!input.trim() || isSending) && styles.sendButtonDisabled]}
            onPress={handleSend}
            disabled={!input.trim() || isSending}
          >
            {isSending ? (
              <ActivityIndicator size="small" color={AppColors.white} />
            ) : (
              <Ionicons name="send" size={20} color={input.trim() ? AppColors.white : AppColors.textMuted} />
            )}
          </TouchableOpacity>
        </View>
      </KeyboardAvoidingView>

      {/* Beneficiary Picker Modal */}
      <Modal visible={showBeneficiaryPicker} animationType="slide" transparent>
        <View style={styles.modalOverlay}>
          <View style={styles.modalContent}>
            <View style={styles.modalHeader}>
              <Text style={styles.modalTitle}>Select Beneficiary</Text>
              <TouchableOpacity onPress={() => setShowBeneficiaryPicker(false)}>
                <Ionicons name="close" size={24} color={AppColors.textPrimary} />
              </TouchableOpacity>
            </View>
            <ScrollView style={styles.beneficiaryList}>
              {beneficiaries.length === 0 ? (
                <View style={styles.emptyState}>
                  <ActivityIndicator size="large" color={AppColors.primary} />
                  <Text style={styles.emptyStateText}>Loading beneficiaries...</Text>
                </View>
              ) : (
                beneficiaries.map(beneficiary => (
                  <TouchableOpacity
                    key={beneficiary.id}
                    style={[
                      styles.beneficiaryItem,
                      currentBeneficiary?.id === beneficiary.id && styles.beneficiaryItemSelected,
                    ]}
                    onPress={() => selectBeneficiary(beneficiary)}
                  >
                    <View style={styles.beneficiaryInfo}>
                      <Text style={styles.beneficiaryName}>{beneficiary.name}</Text>
                      <Text style={styles.beneficiaryStatus}>
                        {beneficiary.wellness_descriptor || beneficiary.last_location || 'No data'}
                      </Text>
                    </View>
                    <View style={[styles.statusDot, { backgroundColor: beneficiary.status === 'online' ? AppColors.success : AppColors.offline }]} />
                  </TouchableOpacity>
                ))
              )}
            </ScrollView>
          </View>
        </View>
      </Modal>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: AppColors.surface,
  },
  header: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingHorizontal: Spacing.md,
    paddingVertical: Spacing.sm,
    backgroundColor: AppColors.background,
    borderBottomWidth: 1,
    borderBottomColor: AppColors.border,
  },
  headerInfo: {
    flexDirection: 'row',
    alignItems: 'center',
  },
  headerAvatar: {
    width: 40,
    height: 40,
    borderRadius: BorderRadius.full,
    backgroundColor: '#9B59B6',
    justifyContent: 'center',
    alignItems: 'center',
    marginRight: Spacing.sm,
  },
  headerTitle: {
    fontSize: FontSizes.lg,
    fontWeight: '600',
    color: AppColors.textPrimary,
  },
  headerSubtitle: {
    fontSize: FontSizes.sm,
    color: AppColors.success,
  },
  beneficiaryButton: {
    flexDirection: 'row',
    alignItems: 'center',
    paddingHorizontal: Spacing.sm,
    paddingVertical: Spacing.xs,
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.lg,
    borderWidth: 1,
    borderColor: AppColors.border,
  },
  beneficiaryButtonText: {
    marginLeft: Spacing.xs,
    fontSize: FontSizes.sm,
    color: AppColors.primary,
    fontWeight: '500',
  },
  chatContainer: {
    flex: 1,
  },
  messagesList: {
    padding: Spacing.md,
    paddingBottom: Spacing.lg,
  },
  messageContainer: {
    flexDirection: 'row',
    marginBottom: Spacing.md,
    alignItems: 'flex-end',
  },
  userMessageContainer: {
    justifyContent: 'flex-end',
  },
  assistantMessageContainer: {
    justifyContent: 'flex-start',
  },
  avatarContainer: {
    width: 32,
    height: 32,
    borderRadius: BorderRadius.full,
    backgroundColor: '#9B59B6',
    justifyContent: 'center',
    alignItems: 'center',
    marginRight: Spacing.xs,
  },
  messageBubble: {
    maxWidth: '75%',
    padding: Spacing.sm + 4,
    borderRadius: BorderRadius.lg,
  },
  userBubble: {
    backgroundColor: AppColors.primary,
    borderBottomRightRadius: BorderRadius.sm,
  },
  assistantBubble: {
    backgroundColor: AppColors.background,
    borderBottomLeftRadius: BorderRadius.sm,
  },
  messageText: {
    fontSize: FontSizes.base,
    lineHeight: 22,
  },
  userMessageText: {
    color: AppColors.white,
  },
  assistantMessageText: {
    color: AppColors.textPrimary,
  },
  timestamp: {
    fontSize: FontSizes.xs,
    color: AppColors.textMuted,
    marginTop: Spacing.xs,
    alignSelf: 'flex-end',
  },
  userTimestamp: {
    color: 'rgba(255,255,255,0.7)',
  },
  speakingIndicator: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    paddingVertical: Spacing.sm,
    paddingHorizontal: Spacing.md,
    backgroundColor: 'rgba(155, 89, 182, 0.1)',
    marginHorizontal: Spacing.md,
    borderRadius: BorderRadius.lg,
    marginBottom: Spacing.sm,
  },
  speakingText: {
    fontSize: FontSizes.sm,
    color: '#9B59B6',
    fontWeight: '500',
    marginLeft: Spacing.sm,
  },
  inputContainer: {
    flexDirection: 'row',
    alignItems: 'flex-end',
    padding: Spacing.md,
    backgroundColor: AppColors.background,
    borderTopWidth: 1,
    borderTopColor: AppColors.border,
  },
  micButton: {
    width: 50,
    height: 50,
    borderRadius: BorderRadius.full,
    backgroundColor: AppColors.surface,
    justifyContent: 'center',
    alignItems: 'center',
    marginRight: Spacing.sm,
    borderWidth: 2,
    borderColor: AppColors.primary,
  },
  micButtonDisabled: {
    borderColor: AppColors.textMuted,
  },
  input: {
    flex: 1,
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.xl,
    paddingHorizontal: Spacing.md,
    paddingVertical: Spacing.sm,
    fontSize: FontSizes.base,
    color: AppColors.textPrimary,
    maxHeight: 100,
    marginRight: Spacing.sm,
  },
  sendButton: {
    width: 44,
    height: 44,
    borderRadius: BorderRadius.full,
    backgroundColor: '#9B59B6',
    justifyContent: 'center',
    alignItems: 'center',
  },
  sendButtonDisabled: {
    backgroundColor: AppColors.surface,
  },
  // Modal styles
  modalOverlay: {
    flex: 1,
    backgroundColor: 'rgba(0,0,0,0.5)',
    justifyContent: 'flex-end',
  },
  modalContent: {
    backgroundColor: AppColors.background,
    borderTopLeftRadius: BorderRadius.xl,
    borderTopRightRadius: BorderRadius.xl,
    maxHeight: '70%',
  },
  modalHeader: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    padding: Spacing.md,
    borderBottomWidth: 1,
    borderBottomColor: AppColors.border,
  },
  modalTitle: {
    fontSize: FontSizes.lg,
    fontWeight: '600',
    color: AppColors.textPrimary,
  },
  beneficiaryList: {
    padding: Spacing.md,
  },
  beneficiaryItem: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    padding: Spacing.md,
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.lg,
    marginBottom: Spacing.sm,
  },
  beneficiaryItemSelected: {
    backgroundColor: '#E8F0FE',
    borderWidth: 2,
    borderColor: AppColors.primary,
  },
  beneficiaryInfo: {
    flex: 1,
  },
  beneficiaryName: {
    fontSize: FontSizes.base,
    fontWeight: '600',
    color: AppColors.textPrimary,
  },
  beneficiaryStatus: {
    fontSize: FontSizes.sm,
    color: AppColors.textMuted,
    marginTop: 2,
  },
  statusDot: {
    width: 10,
    height: 10,
    borderRadius: 5,
    marginLeft: Spacing.sm,
  },
  emptyState: {
    alignItems: 'center',
    padding: Spacing.xl,
  },
  emptyStateText: {
    marginTop: Spacing.md,
    fontSize: FontSizes.base,
    color: AppColors.textMuted,
  },
});
