'use client'; import React, { useState, useMemo } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { QuitPlan, UsageEntry, UserPreferences } from '@/lib/storage'; import { Target, TrendingDown, ChevronDown, ChevronUp, Cigarette, Leaf } from 'lucide-react'; import { useTheme } from '@/lib/theme-context'; import { getTodayString } from '@/lib/date-utils'; import { cn } from '@/lib/utils'; interface SubstancePlanSectionProps { substance: 'nicotine' | 'weed'; plan: QuitPlan | null; usageData: UsageEntry[]; trackingStartDate: string | null; onGeneratePlan: () => void; isExpanded: boolean; onToggle: () => void; } function SubstancePlanSection({ substance, plan, usageData, trackingStartDate, onGeneratePlan, isExpanded, onToggle }: SubstancePlanSectionProps) { const { theme } = useTheme(); // 1. Data Processing const substanceUsage = useMemo(() => usageData.filter(e => e.substance === substance), [usageData, substance]); const uniqueDaysWithData = useMemo(() => new Set(substanceUsage.map(e => e.date)).size, [substanceUsage]); const daysRemaining = Math.max(0, 7 - uniqueDaysWithData); const totalUsage = substanceUsage.reduce((sum, e) => sum + e.count, 0); const currentAverage = uniqueDaysWithData > 0 ? Math.round(totalUsage / uniqueDaysWithData) : 0; // 2. Unlock Logic const isUnlocked = useMemo(() => { if (!trackingStartDate || uniqueDaysWithData < 7) return false; const [y, m, d] = trackingStartDate.split('-').map(Number); const startObj = new Date(y, m - 1, d); const now = new Date(); const todayObj = new Date(now.getFullYear(), now.getMonth(), now.getDate()); const daysPassed = Math.floor((todayObj.getTime() - startObj.getTime()) / (1000 * 60 * 60 * 24)); return daysPassed >= 7 || uniqueDaysWithData > 7; }, [uniqueDaysWithData, trackingStartDate]); // 3. Plan Validation & Calculations const isValidPlan = plan && plan.startDate && plan.weeklyTargets && Array.isArray(plan.weeklyTargets); const activePlan = isValidPlan ? plan : null; const todayStr = getTodayString(); const todayUsage = substanceUsage .filter(e => e.date === todayStr) .reduce((sum, e) => sum + e.count, 0); let weekNumber = 0; let currentTarget = 0; let totalWeeks = 0; let usagePercent = 0; if (activePlan) { const startDate = new Date(activePlan.startDate); const today = new Date(); weekNumber = Math.floor((today.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24 * 7)) + 1; totalWeeks = activePlan.weeklyTargets.length; currentTarget = weekNumber <= totalWeeks ? activePlan.weeklyTargets[weekNumber - 1] : 0; usagePercent = currentTarget > 0 ? (todayUsage / currentTarget) * 100 : 0; } // 4. Styling const isNicotine = substance === 'nicotine'; const Icon = isNicotine ? Cigarette : Leaf; const label = isNicotine ? 'Nicotine' : 'Weed'; // Base Colors const bgColor = isNicotine ? (theme === 'light' ? 'bg-yellow-500/10' : 'bg-yellow-500/5') : (theme === 'light' ? 'bg-emerald-500/10' : 'bg-emerald-500/5'); const borderColor = isNicotine ? (theme === 'light' ? 'border-yellow-500/20' : 'border-yellow-500/10') : (theme === 'light' ? 'border-emerald-500/20' : 'border-emerald-500/10'); const accentColor = isNicotine ? 'text-yellow-500' : 'text-emerald-500'; const progressFill = isNicotine ? 'bg-yellow-500' : 'bg-emerald-500'; // Specific plan color for the progress bar alert states let progressColor = progressFill; if (activePlan) { if (usagePercent >= 100) progressColor = 'bg-red-500'; else if (usagePercent >= 80) progressColor = isNicotine ? 'bg-orange-400' : 'bg-yellow-400'; } return (
{activePlan ? `Week ${Math.min(weekNumber, totalWeeks)} of ${totalWeeks}` : `Tracking: Day ${uniqueDaysWithData}/7`}
Baseline established: {currentAverage} puffs/day
Keep logging for {daysRemaining} more days to calculate your personalized reduction plan.
)}Current Daily Limit
{currentTarget}
puffs allowed today
Wk {wNum}
{isFuture ? '?' : target}