'use client' import { Container, Card, CardContent, Typography, Box, TextField, Button, Paper, Avatar, Chip, IconButton, Dialog, DialogTitle, DialogContent, DialogActions, List, ListItem, ListItemAvatar, ListItemText, MenuItem, useTheme, CircularProgress, Skeleton, Alert, Tabs, Tab, FormControlLabel, Switch, } from '@mui/material' import { Favorite, Add, Close, Person, AccessTime, FavoriteBorder, Share, MoreVert, AutoAwesome, Edit, Login, } from '@mui/icons-material' import { useState, useEffect } from 'react' import { useTranslations, useLocale, useFormatter } from 'next-intl' import { useAuth } from '@/hooks/use-auth' interface PrayerRequest { id: string title: string description: string category: string author: string timestamp: Date prayerCount: number isPrayedFor: boolean } export default function PrayersPage() { const theme = useTheme() const locale = useLocale() const t = useTranslations('pages.prayers') const tc = useTranslations('common') const f = useFormatter() const { user } = useAuth() const [prayers, setPrayers] = useState([]) const [selectedCategory, setSelectedCategory] = useState('all') const [openDialog, setOpenDialog] = useState(false) const [tabValue, setTabValue] = useState(0) // 0 = Write, 1 = AI Generate const [newPrayer, setNewPrayer] = useState({ title: '', description: '', category: 'personal', }) const [aiPrompt, setAiPrompt] = useState('') const [isGenerating, setIsGenerating] = useState(false) const [loading, setLoading] = useState(true) const categories = [ { value: 'personal', label: t('categories.personal'), color: 'primary' }, { value: 'family', label: t('categories.family'), color: 'secondary' }, { value: 'health', label: t('categories.health'), color: 'error' }, { value: 'work', label: t('categories.work'), color: 'warning' }, { value: 'ministry', label: t('categories.ministry'), color: 'success' }, { value: 'world', label: t('categories.world'), color: 'info' }, ] // Fetch prayers from API const fetchPrayers = async () => { setLoading(true) try { const params = new URLSearchParams() if (selectedCategory !== 'all') { params.append('category', selectedCategory) } params.append('limit', '50') if (user?.id) { params.append('userId', user.id) } const response = await fetch(`/api/prayers?${params.toString()}`) if (response.ok) { const data = await response.json() setPrayers(data.prayers.map((prayer: any) => ({ ...prayer, timestamp: new Date(prayer.timestamp) }))) } else { console.error('Failed to fetch prayers') } } catch (error) { console.error('Error fetching prayers:', error) } finally { setLoading(false) } } useEffect(() => { fetchPrayers() }, [selectedCategory, user]) const handleGenerateAIPrayer = async () => { if (!aiPrompt.trim()) return if (!user) return setIsGenerating(true) try { const response = await fetch('/api/prayers/generate', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('authToken')}` }, body: JSON.stringify({ prompt: aiPrompt, category: newPrayer.category, locale }), }) if (response.ok) { const data = await response.json() setNewPrayer({ title: data.title || '', description: data.prayer || '', category: newPrayer.category }) setTabValue(0) // Switch to write tab to review generated prayer } else { console.error('Failed to generate prayer') } } catch (error) { console.error('Error generating prayer:', error) } finally { setIsGenerating(false) } } const handleSubmitPrayer = async () => { if (!newPrayer.title.trim() || !newPrayer.description.trim()) return if (!user) return const prayer: PrayerRequest = { id: Date.now().toString(), title: newPrayer.title, description: newPrayer.description, category: newPrayer.category, author: user.name || (locale === 'en' ? 'You' : 'Tu'), timestamp: new Date(), prayerCount: 0, isPrayedFor: false, } try { const response = await fetch('/api/prayers', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('authToken')}` }, body: JSON.stringify({ title: newPrayer.title, description: newPrayer.description, category: newPrayer.category, isAnonymous: false }), }) if (response.ok) { const data = await response.json() setPrayers([{ ...data.prayer, timestamp: new Date(data.prayer.timestamp) }, ...prayers]) setNewPrayer({ title: '', description: '', category: 'personal' }) setAiPrompt('') setTabValue(0) setOpenDialog(false) } else { console.error('Failed to submit prayer') } } catch (error) { console.error('Error submitting prayer:', error) } } const handleOpenDialog = () => { if (!user) { // Could redirect to login or show login modal return } setOpenDialog(true) } const handlePrayFor = async (prayerId: string) => { try { const headers: HeadersInit = { 'Content-Type': 'application/json' } const authToken = localStorage.getItem('authToken') if (authToken) { headers['Authorization'] = `Bearer ${authToken}` } const response = await fetch(`/api/prayers/${prayerId}/pray`, { method: 'POST', headers }) if (response.ok) { const data = await response.json() setPrayers(prayers.map(prayer => prayer.id === prayerId ? { ...prayer, prayerCount: data.prayerCount || prayer.prayerCount + 1, isPrayedFor: true } : prayer )) } else { console.error('Failed to update prayer count') } } catch (error) { console.error('Error updating prayer count:', error) } } const getCategoryInfo = (category: string) => { return categories.find(cat => cat.value === category) || categories[0] } const formatTimestamp = (timestamp: Date) => { const currentTime = new Date() const diff = currentTime.getTime() - timestamp.getTime() const minutes = Math.floor(diff / (1000 * 60)) const hours = Math.floor(minutes / 60) const days = Math.floor(hours / 24) try { // Provide the 'now' parameter to avoid the warning if (days > 0) return f.relativeTime(-days, 'day', { now: currentTime }) if (hours > 0) return f.relativeTime(-hours, 'hour', { now: currentTime }) if (minutes > 0) return f.relativeTime(-minutes, 'minute', { now: currentTime }) return f.relativeTime(0, 'second', { now: currentTime }) } catch (e) { // Fallback to simple formatting if relativeTime fails if (days > 0) return locale === 'ro' ? `acum ${days} ${days === 1 ? 'zi' : 'zile'}` : `${days} ${days === 1 ? 'day' : 'days'} ago` if (hours > 0) return locale === 'ro' ? `acum ${hours} ${hours === 1 ? 'oră' : 'ore'}` : `${hours} ${hours === 1 ? 'hour' : 'hours'} ago` if (minutes > 0) return locale === 'ro' ? `acum ${minutes} ${minutes === 1 ? 'minut' : 'minute'}` : `${minutes} ${minutes === 1 ? 'minute' : 'minutes'} ago` return locale === 'ro' ? 'acum' : 'just now' } } return ( {/* Header */} {t('title')} {t('subtitle')} {/* Categories Filter */} {/* Add Prayer Button */} {user ? ( ) : ( )} {t('categories.title')} setSelectedCategory('all')} sx={{ justifyContent: 'flex-start', cursor: 'pointer' }} /> {categories.map((category) => ( setSelectedCategory(category.value)} sx={{ justifyContent: 'flex-start', cursor: 'pointer' }} /> ))} {t('stats.title')} • {t('stats.activeRequests', { count: prayers.length })}
• {t('stats.totalPrayers', { count: prayers.reduce((sum, p) => sum + p.prayerCount, 0) })}
• {t('stats.youPrayed', { count: prayers.filter(p => p.isPrayedFor).length })}
{/* Prayer Requests */} {loading ? ( {Array.from({ length: 3 }).map((_, index) => ( ))} ) : ( {prayers.map((prayer) => { const categoryInfo = getCategoryInfo(prayer.category) return ( {prayer.title} {prayer.author} {formatTimestamp(prayer.timestamp)} {prayer.description} {t('stats.totalPrayers', { count: prayer.prayerCount })} ) })} )}
{/* Add Prayer Dialog */} setOpenDialog(false)} maxWidth="md" fullWidth > {t('dialog.title')} setOpenDialog(false)} size="small"> {/* Tabs for Write vs AI Generate */} setTabValue(newValue)} centered> } label={locale === 'en' ? 'Write Prayer' : 'Scrie rugăciune'} iconPosition="start" /> } label={locale === 'en' ? 'AI Generate' : 'Generează cu AI'} iconPosition="start" /> {/* Write Prayer Tab */} {tabValue === 0 && ( setNewPrayer({ ...newPrayer, title: e.target.value })} sx={{ mb: 2, mt: 1 }} /> setNewPrayer({ ...newPrayer, category: e.target.value })} sx={{ mb: 2 }} > {categories.map((option) => ( {option.label} ))} setNewPrayer({ ...newPrayer, description: e.target.value })} placeholder={t('dialog.placeholder')} /> )} {/* AI Generate Prayer Tab */} {tabValue === 1 && ( {locale === 'en' ? 'Describe what you\'d like to pray about, and AI will help you create a meaningful prayer.' : 'Descrie pentru ce ai vrea să te rogi, iar AI-ul te va ajuta să creezi o rugăciune semnificativă.' } setNewPrayer({ ...newPrayer, category: e.target.value })} sx={{ mb: 2 }} > {categories.map((option) => ( {option.label} ))} setAiPrompt(e.target.value)} placeholder={locale === 'en' ? 'e.g., "Help me find peace during a difficult time at work" or "Guidance for my family\'s health struggles"' : 'ex. "Ajută-mă să găsesc pace într-o perioadă dificilă la muncă" sau "Îndrumarea pentru problemele de sănătate ale familiei mele"' } sx={{ mb: 2 }} /> {newPrayer.title && newPrayer.description && ( {locale === 'en' ? 'Prayer generated! Switch to the "Write Prayer" tab to review and edit before submitting.' : 'Rugăciune generată! Comută la tabul "Scrie rugăciune" pentru a revizui și edita înainte de a trimite.' } )} )}
) }