File size: 27,539 Bytes
4f7951a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
import React, { useState, useRef, useEffect } from 'react';
import "./global.css"
import {
  View,
  Text,
  TextInput,
  ScrollView,
  Pressable,
  SafeAreaView,
  Animated,
  Keyboard,
  TouchableWithoutFeedback,
  TouchableOpacity,
  Platform,
  KeyboardAvoidingView,
  Alert,
  Image,
  Dimensions,
  ActivityIndicator
} from 'react-native';
import { StatusBar } from 'expo-status-bar';
import { FontAwesome5, Ionicons, MaterialCommunityIcons } from '@expo/vector-icons';
import Markdown from 'react-native-markdown-display';
import * as Clipboard from 'expo-clipboard';
import { marked } from 'marked';
import highlightjs from 'highlight.js/lib/core';
import javascript from 'highlight.js/lib/languages/javascript';
import python from 'highlight.js/lib/languages/python';
import { Link, router, useRouter, Stack } from 'expo-router';
import Constants from 'expo-constants';
import * as Haptics from 'expo-haptics';

// Register commonly used languages
highlightjs.registerLanguage('javascript', javascript);
highlightjs.registerLanguage('python', python);

// Screen dimensions for responsive layouts
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');

// Suggested questions for users
const SUGGESTED_QUESTIONS = [
  "What are common symptoms of PCOS?",
  "How can I manage menstrual pain naturally?",
  "What should I know before pregnancy planning?",
  "Is my period flow normal?",
  "Why am I experiencing fatigue during periods?"
];

const FilleAI = () => {
  const router = useRouter();
  
  const [isSearchSubmitted, setIsSearchSubmitted] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const [messages, setMessages] = useState<{ type: string; text: string }[]>([]);
  const [inputText, setInputText] = useState('');
  const [inputHeight, setInputHeight] = useState(40);
  const [suggestedQuestions, setSuggestedQuestions] = useState(SUGGESTED_QUESTIONS);
  const scrollViewRef = useRef<ScrollView>(null);
  const inputRef = useRef<TextInput>(null);
  
  // Animation values
  const welcomeOpacity = useRef(new Animated.Value(1)).current;
  const centerContentMargin = useRef(new Animated.Value(150)).current;
  const floatingButtonScale = useRef(new Animated.Value(1)).current;
  const inputContainerAnimation = useRef(new Animated.Value(0)).current;
  
  // Pulse animation for suggestion bubbles
  useEffect(() => {
    const pulsate = Animated.loop(
      Animated.sequence([
        Animated.timing(floatingButtonScale, {
          toValue: 1.05,
          duration: 1000,
          useNativeDriver: true,
        }),
        Animated.timing(floatingButtonScale, {
          toValue: 1,
          duration: 1000,
          useNativeDriver: true,
        }),
      ])
    );
    
    if (!isSearchSubmitted) {
      pulsate.start();
    } else {
      pulsate.stop();
    }
    
    return () => pulsate.stop();
  }, [isSearchSubmitted]);
  
  // Show input container animation
  useEffect(() => {
    Animated.timing(inputContainerAnimation, {
      toValue: 1,
      duration: 500,
      delay: 500,
      useNativeDriver: true,
    }).start();
  }, []);

  // Function to handle input height changes
  const updateInputHeight = (height: number) => {
    const newHeight = Math.min(Math.max(40, height), 100);
    setInputHeight(newHeight);
  };

  // Function to format code blocks with custom renderer
  const formatMessage = (text: string) => {
    marked.setOptions({
      highlight: function(code, language) {
        if (language && highlightjs.getLanguage(language)) {
          return highlightjs.highlight(code, { language: language }).value;
        }
        return highlightjs.highlightAuto(code).value;
      }
    });
    
    return marked.parse(text);
  };

  // User Message Component with enhanced styling
  const UserMessage = ({ text }: { text: string }) => (
    <View style={{ 
      alignSelf: 'flex-end', 
      maxWidth: '80%', 
      marginVertical: 8,
      transform: [{ translateY: 0 }], // For animation preparation
    }}>
      <View style={{ 
        backgroundColor: '#FF7B00', 
        borderRadius: 18, 
        borderTopRightRadius: 4,
        padding: 12,
        shadowColor: "#000",
        shadowOffset: { width: 0, height: 2 },
        shadowOpacity: 0.15,
        shadowRadius: 3,
        elevation: 3,
      }}>
        <Text style={{ color: 'white', fontSize: 16, fontWeight: '500' }}>{text}</Text>
      </View>
      <View style={{ flexDirection: 'row', alignItems: 'center', alignSelf: 'flex-end', marginTop: 4, marginRight: 4 }}>
        <Text style={{ color: '#AAAAAA', fontSize: 12, marginRight: 5 }}>You</Text>
        <View style={{ 
          width: 24, 
          height: 24, 
          borderRadius: 12, 
          backgroundColor: '#555', 
          alignItems: 'center', 
          justifyContent: 'center' 
        }}>
          <FontAwesome5 name="user" size={12} color="#FFF" />
        </View>
      </View>
    </View>
  );

  // Computer Message Component with Markdown support
  const ComputerMessage = ({ text }: { text: string }) => {
    const [copiedIndex, setCopiedIndex] = useState<number | null>(null);

    const handleCopyCode = async (code: string, index: number) => {
      await Clipboard.setStringAsync(code);
      setCopiedIndex(index);
      
      // Add haptic feedback
      if (Platform.OS !== 'web') {
        Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
      }
      
      setTimeout(() => setCopiedIndex(null), 2000);
    };

    const markdownStyles = {
      body: {
        color: 'white',
        fontSize: 16,
        lineHeight: 24,
      },
      heading1: {
        fontWeight: 'bold',
        fontSize: 22,
        marginTop: 8,
        marginBottom: 4,
        color: 'white',
      },
      heading2: {
        fontWeight: 'bold',
        fontSize: 20,
        marginTop: 8,
        marginBottom: 4,
        color: 'white',
      },
      heading3: {
        fontWeight: 'bold',
        fontSize: 18,
        marginTop: 8,
        marginBottom: 4,
        color: 'white',
      },
      link: {
        color: '#FF9D4F',
        textDecorationLine: "underline",
      },
      blockquote: {
        borderLeftWidth: 3,
        borderLeftColor: '#FF7B00',
        paddingLeft: 10,
        fontStyle: 'italic',
      },
      code_block: {
        backgroundColor: 'rgba(0, 0, 0, 0.3)',
        padding: 10,
        borderRadius: 5,
        fontFamily: 'monospace',
        fontSize: 14,
      },
      code_inline: {
        backgroundColor: 'rgba(0, 0, 0, 0.3)',
        padding: 4,
        borderRadius: 3,
        fontFamily: 'monospace',
        fontSize: 14,
      },
      list_item: {
        marginBottom: 6,
      },
    };

    // Custom renderer for code blocks
    const renderCodeBlock = (props: { content: string; language?: string; index: number }) => {
      const { content, language } = props;
      return (
        <View style={{
          backgroundColor: 'rgba(0, 0, 0, 0.4)',
          borderRadius: 6,
          overflow: 'hidden',
          marginVertical: 10,
        }}>
          <View style={{
            flexDirection: 'row',
            justifyContent: 'space-between',
            alignItems: 'center',
            padding: 8,
            backgroundColor: 'rgba(0, 0, 0, 0.2)',
            borderBottomWidth: 1,
            borderBottomColor: 'rgba(255, 255, 255, 0.1)',
          }}>
            <Text style={{
              fontSize: 12,
              fontWeight: 'bold',
              textTransform: 'uppercase',
              color: 'rgba(255, 255, 255, 0.7)',
            }}>
              {language || 'code'}
            </Text>
            <TouchableOpacity 
              onPress={() => handleCopyCode(content, props.index)}
              style={{
                backgroundColor: 'transparent',
                padding: 4,
                borderRadius: 4,
              }}>
              <FontAwesome5 
                name={copiedIndex === props.index ? "check" : "copy"} 
                size={14} 
                color="white" 
              />
            </TouchableOpacity>
          </View>
          <ScrollView horizontal showsHorizontalScrollIndicator={false}>
            <Text style={{
              fontFamily: 'monospace',
              padding: 12,
              color: 'white',
              fontSize: 14,
            }}>
              {content}
            </Text>
          </ScrollView>
        </View>
      );
    };

    return (
      <View style={{ 
        alignSelf: 'flex-start', 
        maxWidth: '80%', 
        marginVertical: 8,
        flexDirection: 'row',
        alignItems: 'flex-end',
      }}>
        <View style={{ 
          width: 32, 
          height: 32, 
          borderRadius: 16, 
          backgroundColor: '#FF7B00', 
          alignItems: 'center', 
          justifyContent: 'center',
          marginRight: 8,
          marginBottom: 4,
        }}>
          <FontAwesome5 name="robot" size={16} color="#FFF" />
        </View>
        <View style={{ flex: 1 }}>
          <View style={{ 
            backgroundColor: '#332940', 
            borderRadius: 18, 
            borderBottomLeftRadius: 4,
            padding: 12,
            shadowColor: "#000",
            shadowOffset: { width: 0, height: 2 },
            shadowOpacity: 0.15,
            shadowRadius: 3,
            elevation: 2,
          }}>
            <Markdown 
              style={markdownStyles as any}
              rules={{
                code_block: (node, children, parent, styles, renderContent) => {
                  return renderCodeBlock({
                    content: node.content,
                    language: (node as any).language,
                    index: parseInt(node.key, 10),
                  });
                }
              }}
            >
              {text}
            </Markdown>
          </View>
          <Text style={{ color: '#AAAAAA', fontSize: 12, marginLeft: 4, marginTop: 4 }}>Fille AI</Text>
        </View>
      </View>
    );
  };

  // Enhanced Loading indicator with animation
  const LoadingIndicator = () => {
    const [dotIndex, setDotIndex] = useState(0);
    
    useEffect(() => {
      const interval = setInterval(() => {
        setDotIndex(prev => (prev + 1) % 4);
      }, 300);
      return () => clearInterval(interval);
    }, []);
    
    return (
      <View style={{ 
        alignSelf: 'flex-start', 
        marginVertical: 8,
        flexDirection: 'row',
        alignItems: 'flex-end',
      }}>
        <View style={{ 
          width: 32, 
          height: 32, 
          borderRadius: 16, 
          backgroundColor: '#FF7B00', 
          alignItems: 'center', 
          justifyContent: 'center',
          marginRight: 8,
          marginBottom: 4,
        }}>
          <FontAwesome5 name="robot" size={16} color="#FFF" />
        </View>
        <View style={{ 
          backgroundColor: '#332940', 
          borderRadius: 18,
          paddingHorizontal: 16,
          paddingVertical: 12,
          flexDirection: 'row',
          alignItems: 'center',
          shadowColor: "#000",
          shadowOffset: { width: 0, height: 2 },
          shadowOpacity: 0.15,
          shadowRadius: 3,
          elevation: 2,
        }}>
          {[0, 1, 2].map((i) => (
            <View 
              key={i}
              style={{
                width: 10,
                height: 10,
                borderRadius: 5,
                backgroundColor: 'white',
                opacity: dotIndex === i ? 0.9 : 0.4,
                marginHorizontal: 4,
                transform: [{ 
                  translateY: dotIndex === i ? -4 : 0 
                }]
              }}
            />
          ))}
        </View>
      </View>
    );
  };

  // Suggestion bubble component
  const SuggestionBubble = ({ text }: { text: string }) => (
    <Animated.View style={{
      transform: [{ scale: floatingButtonScale }]
    }}>
      <TouchableOpacity
        style={{
          backgroundColor: 'rgba(255, 123, 0, 0.15)',
          borderWidth: 1,
          borderColor: 'rgba(255, 123, 0, 0.3)',
          borderRadius: 20,
          paddingHorizontal: 16,
          paddingVertical: 10,
          marginRight: 10,
          marginBottom: 10,
          shadowColor: "#FF7B00",
          shadowOffset: { width: 0, height: 2 },
          shadowOpacity: 0.2,
          shadowRadius: 3,
          elevation: 2,
        }}
        onPress={() => {
          // Add haptic feedback
          if (Platform.OS !== 'web') {
            Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
          }
          setInputText(text);
          handleSubmit(text);
        }}
      >
        <Text style={{ color: '#FF9D4F', fontSize: 14 }}>{text}</Text>
      </TouchableOpacity>
    </Animated.View>
  );

  // Get server URL based on environment
  const getServerUrl = () => {
    if (Platform.OS === 'android') {
      // Special IP that Android emulator uses to access host machine
      return "http://10.0.2.2:8000/chat";
    } else if (Platform.OS === 'ios') {
      return "http://localhost:8000/chat";
    } else {
      // For web or other platforms
      return "http://localhost:8000/chat";
    }
  };

  // Handler for chat submit
  const handleSubmit = async (text = inputText) => {
    const messageToSend = text.trim();
    if (messageToSend === '' || isLoading) return;

    // Add haptic feedback on send
    if (Platform.OS !== 'web') {
      Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
    }

    // Add user message to chat
    const userMessage = messageToSend;
    setMessages(prevMessages => [...prevMessages, { type: 'user', text: userMessage }]);
    setInputText('');
    setInputHeight(40);

    // If this is the first message, transition the UI
    if (!isSearchSubmitted) {
      Animated.parallel([
        Animated.timing(welcomeOpacity, {
          toValue: 0,
          duration: 300,
          useNativeDriver: true,
        }),
        Animated.timing(centerContentMargin, {
          toValue: 20,
          duration: 500,
          useNativeDriver: false,
        }),
      ]).start(() => {
        setIsSearchSubmitted(true);
      });
    }

    // Set loading state
    setIsLoading(true);

    try {
      const serverUrl = getServerUrl();
      console.log(`Sending request to: ${serverUrl}`);
      
      // Send request to server
      const response = await fetch(serverUrl, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "Accept": "application/json",
        },
        body: JSON.stringify({ message: userMessage }),
      });

      if (!response.ok) {
        throw new Error(`Failed to get response: ${response.status}`);
      }

      const data = await response.json();

      // Add AI response to chat
      const aiResponse = typeof data.response === 'string' 
        ? data.response
        : JSON.stringify(data.response);
      
      // Add haptic feedback when response arrives
      if (Platform.OS !== 'web') {
        Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
      }
      
      setMessages(prevMessages => [...prevMessages, { type: 'computer', text: aiResponse }]);
      
      // Update suggested questions based on conversation context
      updateSuggestedQuestions(userMessage, aiResponse);
      
    } catch (error) {
      console.error('Error:', error);
      
      // Add haptic feedback for error
      if (Platform.OS !== 'web') {
        Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);
      }
      
      Alert.alert(
        "Connection Error",
        `Failed to connect to server: ${(error as Error).message}\n\nMake sure your server is running and the device can reach it.`,
        [{ text: "OK" }]
      );
      
      setMessages(prevMessages => [
        ...prevMessages,
        { type: 'computer', text: 'Sorry, there was an error processing your request. Please check your network connection and make sure the server is running.' },
      ]);
    } finally {
      setIsLoading(false);
    }
  };

  // Update suggested questions based on conversation
  const updateSuggestedQuestions = (userMessage: string, aiResponse: string) => {
    // This is a simplified approach. For production, you might want to use the AI to generate these.
    const periodRelated = userMessage.toLowerCase().includes('period') || 
                          aiResponse.toLowerCase().includes('period');
    
    const painRelated = userMessage.toLowerCase().includes('pain') || 
                        aiResponse.toLowerCase().includes('pain');
    
    const pregnancyRelated = userMessage.toLowerCase().includes('pregnan') || 
                             aiResponse.toLowerCase().includes('pregnan');
    
    const hormoneRelated = userMessage.toLowerCase().includes('hormone') || 
                           aiResponse.toLowerCase().includes('hormone');
    
    let newSuggestions = [...SUGGESTED_QUESTIONS];
    
    if (periodRelated) {
      newSuggestions = [
        "What causes irregular periods?",
        "How can I track my menstrual cycle?",
        "When should I be concerned about heavy flow?",
      ];
    } else if (painRelated) {
      newSuggestions = [
        "What are natural remedies for cramps?",
        "Should I see a doctor about period pain?",
        "How can exercise help with menstrual pain?",
      ];
    } else if (pregnancyRelated) {
      newSuggestions = [
        "What prenatal vitamins should I take?",
        "How does ovulation tracking work?",
        "What are early signs of pregnancy?",
      ];
    } else if (hormoneRelated) {
      newSuggestions = [
        "How do hormones affect mood?",
        "What foods help balance hormones?",
        "How does stress impact hormonal health?",
      ];
    }
    
    // Shuffle the array to get different suggestions each time
    newSuggestions.sort(() => Math.random() - 0.5);
    
    // Take only the first 3
    setSuggestedQuestions(newSuggestions.slice(0, 3));
  };

  // Scroll to bottom when messages change
  useEffect(() => {
    if (scrollViewRef.current && messages.length > 0) {
      setTimeout(() => {
        if (scrollViewRef.current) {
          scrollViewRef.current.scrollToEnd({ animated: true });
        }
      }, 100);
    }
  }, [messages]);

  return (
    <>
      {/* Add Stack.Screen options to hide the header */}
      <Stack.Screen options={{ headerShown: false }} />
      
      <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
        <SafeAreaView style={{ flex: 1, backgroundColor: '#1A1C25' }}>
          <StatusBar style="light" />
          
          {/* Navbar */}
          <View style={{ 
            padding: 20, 
            flexDirection: 'row', 
            justifyContent: 'space-between',
            alignItems: 'center',
            borderBottomWidth: 1,
            borderBottomColor: 'rgba(255,255,255,0.1)'
          }}>
            <View style={{ flexDirection: 'row', alignItems: 'center' }}>
              <FontAwesome5 name="heartbeat" size={18} color="#FF7B00" style={{ marginRight: 10 }} />
              <Text style={{ color: 'white', fontWeight: 'bold', fontSize: 18 }}>
                FILLE <Text style={{ color: '#FF7B00' }}>AI</Text>
              </Text>
            </View>
            <TouchableOpacity 
              style={{ 
                backgroundColor: '#332940', 
                paddingHorizontal: 12,
                paddingVertical: 6,
                borderRadius: 16,
                flexDirection: 'row',
                alignItems: 'center'
              }}
              onPress={() => {
                if (Platform.OS !== 'web') {
                  Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
                }
                router.push("/realchat");
              }}
            >
              <MaterialCommunityIcons name="doctor" size={16} color="#FF9D4F" style={{ marginRight: 6 }} />
              <Text style={{ color: '#FF9D4F', fontSize: 13 }}>Talk to Doctor</Text>
            </TouchableOpacity>
          </View>
          
          {/* Main Content */}
          <KeyboardAvoidingView 
            behavior={Platform.OS === 'ios' ? 'padding' : undefined}
            style={{ flex: 1 }}
            keyboardVerticalOffset={Platform.OS === 'ios' ? 80 : 0}
          >
            <View style={{ flex: 1, justifyContent: 'space-between' }}>
              {/* Center Content */}
              <Animated.View style={{ 
                flex: 1, 
                marginTop: centerContentMargin,
              }}>
                {/* Welcome Message */}
                {!isSearchSubmitted && (
                  <Animated.View style={{ 
                    opacity: welcomeOpacity,
                    alignItems: 'center',
                    padding: 20,
                  }}>
                    <View style={{ 
                      width: 100, 
                      height: 100, 
                      borderRadius: 50, 
                      backgroundColor: '#FF7B00',
                      justifyContent: 'center',
                      alignItems: 'center',
                      marginBottom: 20,
                      shadowColor: "#FF7B00",
                      shadowOffset: { width: 0, height: 0 },
                      shadowOpacity: 0.6,
                      shadowRadius: 20,
                    }}>
                      <FontAwesome5 name="female" size={50} color="white" />
                    </View>
                    <Text style={{ 
                      color: 'white', 
                      fontWeight: 'bold', 
                      fontSize: 28,
                      textAlign: 'center'
                    }}>
                      Hello, GIRL!
                    </Text>
                    <Text style={{ 
                      color: '#AAAAAA', 
                      fontSize: 16,
                      marginTop: 12,
                      textAlign: 'center',
                      paddingHorizontal: 30,
                      lineHeight: 22
                    }}>
                      I'm your health companion. Ready to share your problems and feelings today?
                    </Text>
                    
                    {/* Suggested Topics */}
                    <Text style={{ color: 'white', marginTop: 30, marginBottom: 15, fontWeight: '600' }}>
                      Try asking about:
                    </Text>
                    <View style={{ flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'center' }}>
                      {suggestedQuestions.map((question, index) => (
                        <SuggestionBubble key={index} text={question} />
                      ))}
                    </View>
                  </Animated.View>
                )}

                {/* Conversation Container */}
                <ScrollView
                  ref={scrollViewRef}
                  style={{ 
                    display: isSearchSubmitted ? 'flex' : 'none',
                    paddingHorizontal: 16,
                  }}
                  contentContainerStyle={{ paddingBottom: 16 }}
                  showsVerticalScrollIndicator={false}
                >
                  {messages.map((message, index) => (
                    (message as { type: string; text: string }).type === 'user' ? (
                      <UserMessage key={index} text={(message as { type: string; text: string }).text} />
                    ) : (
                      <ComputerMessage key={index} text={(message as { type: string; text: string }).text} />
                    )
                  ))}
                  {isLoading && <LoadingIndicator />}
                </ScrollView>
              </Animated.View>

              {/* Suggestions row (visible only after first message) */}
              {isSearchSubmitted && !isLoading && messages.length > 0 && (
                <View style={{ 
                  paddingHorizontal: 20, 
                  paddingVertical: 10 
                }}>
                  <ScrollView 
                    horizontal 
                    showsHorizontalScrollIndicator={false}
                    style={{ flexDirection: 'row' }}
                    contentContainerStyle={{ paddingRight: 20 }}
                  >
                    {suggestedQuestions.map((question, index) => (
                      <SuggestionBubble key={index} text={question} />
                    ))}
                  </ScrollView>
                </View>
              )}

              {/* Input Container */}
              <Animated.View style={{ 
                flexDirection: 'row', 
                alignItems: 'flex-end',
                marginHorizontal: 20, 
                marginBottom: 20,
                marginTop: 10,
                backgroundColor: '#24273A',
                borderRadius: 25,
                paddingHorizontal: 10,
                paddingVertical: 5,
                shadowColor: "#000",
                shadowOffset: { width: 0, height: 4 },
                shadowOpacity: 0.2,
                shadowRadius: 5,
                elevation: 5,
                opacity: inputContainerAnimation,
                transform: [{ 
                  translateY: inputContainerAnimation.interpolate({
                    inputRange: [0, 1],
                    outputRange: [20, 0]
                  }) 
                }]
              }}>
                <TextInput
                  ref={inputRef}
                  style={{
                    flex: 1,
                    color: 'white',
                    fontSize: 16,
                    paddingVertical: 12,
                    paddingHorizontal: 12,
                    minHeight: inputHeight,
                    maxHeight: 100,
                  }}
                  placeholder="Ask me anything about women's health"
                  placeholderTextColor="rgba(255,255,255,0.4)"
                  value={inputText}
                  onChangeText={setInputText}
                  multiline
                  onContentSizeChange={(e) => 
                    updateInputHeight(e.nativeEvent.contentSize.height)
                  }
                />
                <TouchableOpacity 
                  onPress={() => handleSubmit()}
                  disabled={isLoading || inputText.trim() === ''}
                  style={{ 
                    padding: 10, 
                    backgroundColor: (isLoading || inputText.trim() === '') ? '#444' : '#FF7B00',
                    borderRadius: 20,
                    width: 40,
                    height: 40,
                    justifyContent: 'center',
                    alignItems: 'center',
                    marginBottom: 6,
                  }}
                >
                  <Ionicons name="send" size={18} color="white" />
                </TouchableOpacity>
              </Animated.View>
            </View>
          </KeyboardAvoidingView>

          {/* Footer */}
          <View style={{ 
            paddingBottom: 10, 
            alignItems: 'center', 
            borderTopWidth: 1,
            borderTopColor: 'rgba(255,255,255,0.1)',
            paddingTop: 10
          }}>
            <Text style={{ color: 'grey', fontSize: 12 }}>
              For informational purposes. Consult a healthcare professional.
            </Text>
          </View>
        </SafeAreaView>
      </TouchableWithoutFeedback>
    </>
  );
};

export default FilleAI;