'use client'; import { useState, useEffect } from 'react'; import Clock from './Clock'; interface Prayer { id: string; userId: string; date: string; text: string; prayFor?: string; answered?: boolean; answerDetails?: string; prayFrequency?: number; // 1-7 times per week lastPrayedFor?: string; // ISO date string } interface HomePageProps { prayers: Prayer[]; todayCount: number; onStartPraying: () => void; onSelectPrayer: (prayer: Prayer) => void; userName?: string; } export default function HomePage({ prayers, todayCount, onStartPraying, onSelectPrayer, userName }: HomePageProps) { const today = new Date().toISOString().split('T')[0]; const todayPrayers = prayers.filter(p => p.date === today); const recentPrayers = prayers.slice(0, 5); const totalPrayers = prayers.length; const uniqueDays = new Set(prayers.map(p => p.date)).size; const answeredPrayers = prayers.filter(p => p.answered).length; // Get greeting based on time of day const getGreeting = () => { const hour = new Date().getHours(); if (hour < 12) return 'Good morning'; if (hour < 18) return 'Good afternoon'; return 'Good evening'; }; // Get first name from display name or email const getFirstName = () => { if (!userName) return 'friend'; // If it's an email, get the part before @ if (userName.includes('@')) { return userName.split('@')[0]; } // If it's a full name, get first name return userName.split(' ')[0]; }; // Count active prayer requests (not answered) const activePrayerCount = prayers.filter(p => !p.answered).length; // Count how many active prayers were prayed for today const prayedTodayCount = prayers.filter(p => !p.answered && p.lastPrayedFor === today).length; // Calculate actual streak - consecutive days with prayers const calculateStreak = () => { if (prayers.length === 0) return 0; // Get all dates where user either created a prayer OR marked something as prayed for const activityDates = new Set(); prayers.forEach(p => { activityDates.add(p.date); // Date prayer was created if (p.lastPrayedFor) { activityDates.add(p.lastPrayedFor); // Date marked as prayed for } }); const sortedDates = Array.from(activityDates).sort().reverse(); let streak = 0; let checkDate = new Date(today); for (let i = 0; i < 365; i++) { const dateStr = checkDate.toISOString().split('T')[0]; if (sortedDates.includes(dateStr)) { streak++; } else if (i === 0) { // If today has no activity, streak is broken break; } else { break; } checkDate.setDate(checkDate.getDate() - 1); } return streak; }; const streak = calculateStreak(); // Calculate more stats const avgPrayerLength = totalPrayers > 0 ? Math.round(prayers.reduce((sum, p) => sum + p.text.length, 0) / totalPrayers) : 0; const answeredPercentage = totalPrayers > 0 ? Math.round((answeredPrayers / totalPrayers) * 100) : 0; // Get most prayed for person/topic based on lastPrayedFor clicks const prayerForCounts: Record = {}; prayers.forEach(p => { if (p.prayFor && p.lastPrayedFor) { prayerForCounts[p.prayFor] = (prayerForCounts[p.prayFor] || 0) + 1; } }); const mostPrayedFor = Object.entries(prayerForCounts).sort((a, b) => b[1] - a[1])[0]; // Recent answered prayers const recentAnswered = prayers.filter(p => p.answered).slice(0, 3); return (
{/* Animated Background Elements */}
{/* Clock */}
{/* Header with Personalized Greeting */}

{getGreeting()}, {getFirstName()}!

{activePrayerCount > 0 ? (

You have {activePrayerCount} {activePrayerCount === 1 ? 'thing' : 'things'} that need prayer today.

{prayedTodayCount > 0 && (

You've prayed for {prayedTodayCount} of them! 🙏

)}
) : (

All caught up! Start a new prayer to keep your journey going.

)}

{new Date().toLocaleDateString('en-US', { weekday: 'long', month: 'long', day: 'numeric' })}

{/* Stats Grid */}
{/* Today's Prayers - Featured */}
Today's Prayers
{todayCount}
📿
{/* Main Stats Grid */}
{/* Total Prayers */}
Total Prayers
{totalPrayers}
All time
{/* Days Prayed */}
Days Prayed
{uniqueDays}
Consistent
{/* Answered Prayers */}
✅ Answered
{answeredPrayers}
{answeredPercentage}% answered
{/* Streak */}
🔥 Streak
{streak}
consecutive days
{/* Secondary Stats */}
{/* Avg Prayer Length */}
📊 Avg Length
{avgPrayerLength}
characters
{/* Most Prayed For */}
💝 Most Prayed For
{mostPrayedFor ? ( <>
{mostPrayedFor[0]}
{mostPrayedFor[1]} times
) : ( <>
Start praying!
Click "Mark Prayed" to build your prayer focus
)}
{/* CTA Button */}
{/* Things to Pray For - Active Requests */} {(() => { const pendingPrayers = prayers.filter(p => !p.answered); if (pendingPrayers.length > 0) { return (

🙏 Things to Pray For

{pendingPrayers.length}
active prayer requests
); } return null; })()} {/* Recent Prayers */} {recentPrayers.length > 0 && (

📖 Recent Prayers

{recentPrayers.map((prayer, idx) => ( ))}
)} {/* Recent Answered Prayers */} {recentAnswered.length > 0 && (

✨ Recently Answered

{recentAnswered.map((prayer) => (
✅
{prayer.prayFor && (
🙏 {prayer.prayFor}
)}

{prayer.text}

{prayer.answerDetails && (

💬 "{prayer.answerDetails.substring(0, 60)}..."

)}
{new Date(prayer.date).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })}
))}
)} {/* Empty State */} {prayers.length === 0 && (
🙏

Start Your Prayer Journey

Create your first prayer entry and begin tracking your spiritual journey.

)}
); }