'use client' import { useState, useEffect } from 'react' import { useTranslations, useLocale } from 'next-intl' import { useAuth } from '@/hooks/use-auth' import { ProtectedRoute } from '@/components/auth/protected-route' import { Container, Paper, Box, Typography, Switch, FormControlLabel, Select, MenuItem, FormControl, InputLabel, Card, CardContent, Divider, Alert, Button, CircularProgress } from '@mui/material' import { Settings as SettingsIcon, Palette, TextFields, Language, Notifications, Security, Save, MenuBook } from '@mui/icons-material' export default function SettingsPage() { const { user } = useAuth() const locale = useLocale() const t = useTranslations('settings') const [settings, setSettings] = useState({ theme: user?.theme || 'light', fontSize: user?.fontSize || 'medium', notifications: true, emailUpdates: false, language: locale }) const [message, setMessage] = useState('') const [bibleVersions, setBibleVersions] = useState([]) const [favoriteBibleVersion, setFavoriteBibleVersion] = useState(null) const [loadingVersions, setLoadingVersions] = useState(true) useEffect(() => { const loadBiblePreferences = async () => { try { // Load available versions (no limit to ensure we get all versions including the favorite) const versionsRes = await fetch('/api/bible/versions?all=true') const versionsData = await versionsRes.json() if (versionsData.success) { console.log('[Settings] Loaded versions:', versionsData.versions.length) setBibleVersions(versionsData.versions) } // Load user's favorite version const token = localStorage.getItem('authToken') if (token) { const favoriteRes = await fetch('/api/user/favorite-version', { headers: { 'Authorization': `Bearer ${token}` } }) const favoriteData = await favoriteRes.json() console.log('[Settings] Favorite version data:', favoriteData) if (favoriteData.success && favoriteData.favoriteBibleVersion) { console.log('[Settings] Setting favorite version:', favoriteData.favoriteBibleVersion) console.log('[Settings] Type of favorite version:', typeof favoriteData.favoriteBibleVersion) setFavoriteBibleVersion(favoriteData.favoriteBibleVersion) } } } catch (error) { console.error('Error loading Bible preferences:', error) } finally { setLoadingVersions(false) } } if (user) { loadBiblePreferences() } else { setLoadingVersions(false) } }, [user]) const handleSettingChange = (setting: string, value: any) => { setSettings(prev => ({ ...prev, [setting]: value })) } const handleFavoriteVersionChange = async (versionId: string) => { const token = localStorage.getItem('authToken') if (!token) return try { const response = await fetch('/api/user/favorite-version', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, body: JSON.stringify({ versionId }) }) const data = await response.json() if (data.success) { setFavoriteBibleVersion(versionId) setMessage('Favorite Bible version updated successfully') } else { setMessage('Failed to update favorite version') } } catch (error) { console.error('Error updating favorite version:', error) setMessage('Failed to update favorite version') } } const handleSave = async () => { try { // TODO: Implement settings update API await new Promise(resolve => setTimeout(resolve, 1000)) // Placeholder setMessage(t('settingsSaved')) } catch (error) { setMessage(t('settingsError')) } } return ( {/* Header */} {t('title')} {t('subtitle')} {/* Appearance Settings */} {t('appearance')} {t('theme')} {t('fontSize')} {/* Language & Notifications */} {t('languageAndNotifications')} {t('language')} handleSettingChange('notifications', e.target.checked)} /> } label={t('notifications')} /> handleSettingChange('emailUpdates', e.target.checked)} /> } label={t('emailUpdates')} /> {/* Bible Preferences */} Bible Preferences Select your preferred Bible version. This will be loaded automatically when you open the Bible reader. {loadingVersions ? ( ) : ( Favorite Bible Version {favoriteBibleVersion && ( ✓ This version will be loaded when you open the Bible reader )} )} {/* Security Settings */} {t('security')} {t('passwordSecurity')} {/* Save Button */} {/* Success/Error Message */} {message && ( setMessage('')} > {message} )} ) }