import React, { useState, useEffect } from 'react'; import { View, Text, StyleSheet, ScrollView, SafeAreaView, StatusBar, TextInput, TouchableOpacity, Alert, Linking, Platform, Dimensions, Switch, } from 'react-native'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { useNavigation } from '@react-navigation/native'; import { MaterialIcons } from '@expo/vector-icons'; import { useTheme } from '../contexts/ThemeContext'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { useSettings } from '../hooks/useSettings'; import { SvgXml } from 'react-native-svg'; const { width } = Dimensions.get('window'); const isTablet = width >= 768; const AISettingsScreen: React.FC = () => { const navigation = useNavigation(); const { currentTheme } = useTheme(); const insets = useSafeAreaInsets(); const { settings, updateSetting } = useSettings(); const OPENROUTER_SVG = ` OpenRouter `; const [apiKey, setApiKey] = useState(''); const [loading, setLoading] = useState(false); const [isKeySet, setIsKeySet] = useState(false); useEffect(() => { loadApiKey(); }, []); const loadApiKey = async () => { try { const savedKey = await AsyncStorage.getItem('openrouter_api_key'); if (savedKey) { setApiKey(savedKey); setIsKeySet(true); } } catch (error) { if (__DEV__) console.error('Error loading OpenRouter API key:', error); } }; const handleSaveApiKey = async () => { if (!apiKey.trim()) { Alert.alert('Error', 'Please enter a valid API key'); return; } if (!apiKey.startsWith('sk-or-')) { Alert.alert('Error', 'OpenRouter API keys should start with "sk-or-"'); return; } setLoading(true); try { await AsyncStorage.setItem('openrouter_api_key', apiKey.trim()); setIsKeySet(true); Alert.alert('Success', 'OpenRouter API key saved successfully!'); } catch (error) { Alert.alert('Error', 'Failed to save API key'); if (__DEV__) console.error('Error saving OpenRouter API key:', error); } finally { setLoading(false); } }; const handleRemoveApiKey = () => { Alert.alert( 'Remove API Key', 'Are you sure you want to remove your OpenRouter API key? This will disable AI chat features.', [ { text: 'Cancel', style: 'cancel' }, { text: 'Remove', style: 'destructive', onPress: async () => { try { await AsyncStorage.removeItem('openrouter_api_key'); setApiKey(''); setIsKeySet(false); Alert.alert('Success', 'API key removed successfully'); } catch (error) { Alert.alert('Error', 'Failed to remove API key'); } } } ] ); }; const handleGetApiKey = () => { Linking.openURL('https://openrouter.ai/keys'); }; const headerBaseHeight = Platform.OS === 'android' ? 80 : 60; const topSpacing = Platform.OS === 'android' ? (StatusBar.currentHeight || 0) : insets.top; const headerHeight = headerBaseHeight + topSpacing; return ( {/* Header */} navigation.goBack()} style={styles.backButton} > AI Assistant {/* Info Card */} AI-Powered Chat Ask questions about any movie or TV show episode using advanced AI. Get insights about plot, characters, themes, trivia, and more - all powered by comprehensive TMDB data. Episode-specific context and analysis Plot explanations and character insights Behind-the-scenes trivia and facts Your own free OpenRouter API key {/* API Key Configuration */} OPENROUTER API KEY API Key Enter your OpenRouter API key to enable AI chat features {!isKeySet ? ( {loading ? 'Saving...' : 'Save API Key'} ) : ( Update Remove )} Get Free API Key from OpenRouter {/* Enable Toggle (top) */} Enable AI Chat updateSetting('aiChatEnabled', v)} trackColor={{ false: currentTheme.colors.elevation2, true: currentTheme.colors.primary }} thumbColor={settings.aiChatEnabled ? currentTheme.colors.white : currentTheme.colors.mediumEmphasis} ios_backgroundColor={currentTheme.colors.elevation2} /> When enabled, the Ask AI button will appear on content pages. {/* Status Card */} {isKeySet && ( AI Chat Enabled You can now ask questions about movies and TV shows. Look for the "Ask AI" button on content pages! )} {/* Usage Info */} How it works • OpenRouter provides access to multiple AI models{'\n'} • Your API key stays private and secure{'\n'} • Free tier includes generous usage limits{'\n'} • Chat with context about specific episodes/movies{'\n'} • Get detailed analysis and explanations {/* OpenRouter branding */} ); }; const styles = StyleSheet.create({ container: { flex: 1, }, header: { paddingHorizontal: Math.max(16, width * 0.05), justifyContent: 'flex-end', paddingBottom: 8, }, headerContent: { flexDirection: 'row', alignItems: 'center', }, backButton: { marginRight: 16, padding: 8, }, headerTitle: { fontSize: Math.min(28, width * 0.07), fontWeight: '800', letterSpacing: 0.3, }, scrollView: { flex: 1, }, scrollContent: { padding: Math.max(16, width * 0.05), paddingBottom: 40, }, infoCard: { borderRadius: 16, padding: 20, marginBottom: 20, }, infoHeader: { flexDirection: 'row', alignItems: 'center', marginBottom: 12, }, infoTitle: { fontSize: 20, fontWeight: '700', marginLeft: 12, }, infoDescription: { fontSize: 16, lineHeight: 24, marginBottom: 16, }, featureList: { gap: 8, }, featureItem: { flexDirection: 'row', alignItems: 'center', }, featureText: { fontSize: 15, marginLeft: 8, flex: 1, }, card: { borderRadius: 16, padding: 20, marginBottom: 20, }, cardTitle: { fontSize: 13, fontWeight: '600', letterSpacing: 0.8, marginBottom: 16, }, apiKeySection: { gap: 12, }, label: { fontSize: 16, fontWeight: '600', }, description: { fontSize: 14, lineHeight: 20, }, input: { borderRadius: 12, padding: 16, fontSize: 16, fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace', borderWidth: 1, }, buttonContainer: { marginTop: 8, }, buttonRow: { flexDirection: 'row', gap: 12, }, saveButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', paddingVertical: 14, paddingHorizontal: 20, borderRadius: 12, }, saveButtonText: { color: 'white', fontSize: 16, fontWeight: '600', }, updateButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', paddingVertical: 14, paddingHorizontal: 20, borderRadius: 12, flex: 1, }, updateButtonText: { color: 'white', fontSize: 16, fontWeight: '600', }, removeButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', paddingVertical: 14, paddingHorizontal: 20, borderRadius: 12, borderWidth: 2, flex: 1, }, removeButtonText: { fontSize: 16, fontWeight: '600', }, getKeyButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', paddingVertical: 12, paddingHorizontal: 16, borderRadius: 12, marginTop: 8, }, getKeyButtonText: { fontSize: 15, fontWeight: '500', }, statusCard: { borderRadius: 16, padding: 20, marginBottom: 20, }, statusHeader: { flexDirection: 'row', alignItems: 'center', marginBottom: 8, }, statusTitle: { fontSize: 18, fontWeight: '600', marginLeft: 12, }, statusDescription: { fontSize: 15, lineHeight: 22, }, usageCard: { borderRadius: 16, padding: 20, }, usageTitle: { fontSize: 18, fontWeight: '600', marginBottom: 12, }, usageText: { fontSize: 15, lineHeight: 24, }, }); export default AISettingsScreen;