import React, { useState, useEffect } from 'react'; import { XCircle, Users, ShieldCheck, CheckCircle, AlertCircle, Eye, EyeOff, RefreshCw, LogOut, Download } from 'lucide-react'; import type { UserProfile, ModuleItem, Currency } from '../../types'; import { COUNTRY_CODES, getCountryRuleByName } from '../../constants/countries'; import { cleanPhoneInput, parsePhoneNumber, formatE164Phone, SPECIAL_CHAR_REGEX } from '../../utils/validation'; import { PhoneInput } from '../common/PhoneInput'; interface ProfileDrawerProps { isOpen: boolean; onClose: () => void; loggedInUser: UserProfile | null; profileDisplayName: string; setProfileDisplayName: (name: string) => void; profilePhone: string; setProfilePhone: (phone: string) => void; profileCountry: string; setProfileCountry: (country: string) => void; setCurrency: (currency: Currency) => void; profileSaving: boolean; profileError: string | null; profileSuccess: boolean; onUpdateProfile: (e: React.FormEvent) => Promise; showPasswordChange: boolean; setShowPasswordChange: (show: boolean) => void; changeOldPassword: string; setChangeOldPassword: (pwd: string) => void; changeNewPassword: string; setChangeNewPassword: (pwd: string) => void; changeConfirmPassword: string; setChangeConfirmPassword: (pwd: string) => void; changePasswordLoading: boolean; changePasswordError: string | null; changePasswordSuccess: boolean; onChangePassword: (e: React.FormEvent) => Promise; onLogout: () => void; onBrowseStorefront: () => void; onViewSubscriptions?: () => void; curriculumModules: ModuleItem[]; onDownloadInvoice?: (paymentId: string, directUrl?: string) => void; } export const ProfileDrawer: React.FC = ({ isOpen, onClose, loggedInUser, profileDisplayName, setProfileDisplayName, profilePhone, setProfilePhone, profileCountry, setProfileCountry, setCurrency, profileSaving, profileError, profileSuccess, onUpdateProfile, showPasswordChange, setShowPasswordChange, changeOldPassword, setChangeOldPassword, changeNewPassword, setChangeNewPassword, changeConfirmPassword, setChangeConfirmPassword, changePasswordLoading, changePasswordError, changePasswordSuccess, onChangePassword, onLogout, onBrowseStorefront, onViewSubscriptions, curriculumModules, onDownloadInvoice, }) => { const [showOldPassword, setShowOldPassword] = useState(false); const [showNewPassword, setShowNewPassword] = useState(false); const parsed = parsePhoneNumber(profilePhone, profileCountry); const [dialCode, setDialCode] = useState(parsed.dialCode); const [localPhone, setLocalPhone] = useState(parsed.localDigits); useEffect(() => { if (isOpen) { const p = parsePhoneNumber(profilePhone, profileCountry); setDialCode(p.dialCode); setLocalPhone(p.localDigits); } }, [profilePhone, profileCountry, isOpen]); const handleLocalPhoneChange = (newLocal: string) => { setLocalPhone(newLocal); setProfilePhone(formatE164Phone(dialCode, newLocal)); }; const handleDialCodeChange = (newCode: string, newCountry: string) => { setDialCode(newCode); setProfileCountry(newCountry); const rule = getCountryRuleByName(newCountry); setCurrency(rule.currency); const clamped = cleanPhoneInput(localPhone, rule.maxLength, false); setLocalPhone(clamped); setProfilePhone(formatE164Phone(newCode, clamped)); }; const handleCountrySelectChange = (newCountry: string) => { setProfileCountry(newCountry); const rule = getCountryRuleByName(newCountry); setDialCode(rule.code); setCurrency(rule.currency); const clamped = cleanPhoneInput(localPhone, rule.maxLength, false); setLocalPhone(clamped); setProfilePhone(formatE164Phone(rule.code, clamped)); }; if (!isOpen || !loggedInUser) return null; return (
{/* Overlay Backdrop */}
{/* Drawer Container */}
{/* Drawer Header */}
{(loggedInUser.display_name || loggedInUser.email || 'U')[0].toUpperCase()}

{loggedInUser.display_name || 'My Account'}

{loggedInUser.email}

{/* Drawer Body (Scrollable) */}
{/* Profile Details Form */}

Update Account details

{profileSuccess && (
Changes saved successfully! ✓
)} {profileError && (
{profileError}
)}
setProfileDisplayName(e.target.value)} required className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500" />
{/* Security & Password Section */}

Security & Password

{showPasswordChange && (
{changePasswordSuccess && (
Password updated successfully!
)} {changePasswordError && (
{changePasswordError}
)}
setChangeOldPassword(e.target.value)} placeholder="Enter current password" className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 pr-10 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500" />
setChangeNewPassword(e.target.value)} placeholder="Min 8 chars, 1 uppercase, 1 digit, 1 special" className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 pr-10 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500" />
setChangeConfirmPassword(e.target.value)} placeholder="Repeat new password" className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500" />
= 8 ? 'text-emerald-600 font-bold' : ''}> • 8+ chars • 1 uppercase • 1 number • 1 special char 0 && !/\s/.test(changeNewPassword) ? 'text-emerald-600 font-bold' : ''}> • No spaces
)}
{/* Subscriptions Nested Details */}

Ongoing subscriptions

{onViewSubscriptions && ( )}
{!loggedInUser.active_subscriptions || loggedInUser.active_subscriptions.length === 0 ? (

No active subscriptions owned.

) : (
{loggedInUser.active_subscriptions.map((sub: any) => { const moduleMeta = curriculumModules.find((m) => m.code === sub.plan_code); let daysLeft = 0; const hasExpiry = !!sub.expires_at; if (hasExpiry) { const expiry = new Date(sub.expires_at); const now = new Date(); const diffTime = expiry.getTime() - now.getTime(); daysLeft = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); } return (
{moduleMeta ? moduleMeta.name : sub.plan_code.replace(/_/g, ' ').toUpperCase()}
{sub.plan_code === 'LIFETIME_ALL' ? 'Lifetime' : '1 Year'}
Activated: {sub.started_at ? new Date(sub.started_at).toLocaleDateString(undefined, { dateStyle: 'medium' }) : 'N/A'}
Expires: {hasExpiry ? new Date(sub.expires_at).toLocaleDateString(undefined, { dateStyle: 'medium' }) : 'Lifetime'}
Status: {!hasExpiry || sub.plan_code === 'LIFETIME_ALL' ? ( 👑 Lifetime Pass ) : ( {daysLeft > 0 ? `${daysLeft} Days Left` : 'Expired'} )}
{(sub.payment_id || sub.invoice_url) && onDownloadInvoice && (
{sub.invoice_number ? `INV: ${sub.invoice_number}` : 'Official Invoice'}
)}
); })}
)}
{/* Log Out at the bottom of the drawer */}
); };