'use client'; import { useEffect, useState } from 'react'; import { api } from '@/lib/api'; import { useAuth } from '@/components/auth-provider'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { User, Package, Heart, Store, LogOut, Link2, Send, ChevronRight, ChevronLeft, Mail, Phone, MapPin, Settings as SettingsIcon, Shield, HelpCircle, Bell, Globe, CreditCard, Truck, Wallet, Bookmark, ShoppingBag, Star, Clapperboard, FileText, LogIn, CheckCircle, ArrowRight, Eye, Heart as HeartIcon } from 'lucide-react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { useToast } from '@/hooks/use-toast'; import { PageSkeleton } from '@/components/page-skeleton'; import { formatPrice, timeAgo } from '@/lib/api'; /** * ProfilePage — multi-section account hub with client-side view switching. * * Inspired by Instagram + Amazon account pages: * - Profile header (avatar, name, stats) * - Tab bar: Overview | Orders | Settings * - Each tab shows different content within the same page (no page reload) * - URL hash (#orders, #settings) is synced so users can bookmark/share * * Sections: * - Overview: quick stats + recent orders + quick links * - Orders: full order history (inline, not a separate page) * - Settings: profile edit form + notification prefs + region + security * * For sellers, an extra "Store" tab appears that links to /seller-dashboard. */ type View = 'overview' | 'orders' | 'settings' | 'store'; export default function ProfilePage() { const { user, loading: authLoading, logout, isSeller } = useAuth(); const router = useRouter(); const { toast } = useToast(); const [profile, setProfile] = useState(null); const [orders, setOrders] = useState([]); const [wishlistCount, setWishlistCount] = useState(0); const [view, setView] = useState('overview'); const [editing, setEditing] = useState(false); const [saving, setSaving] = useState(false); const [fullName, setFullName] = useState(''); const [phone, setPhone] = useState(''); const [address, setAddress] = useState(''); // Notification preferences (local state — would be saved to backend in production) const [notifOrders, setNotifOrders] = useState(true); const [notifPromos, setNotifPromos] = useState(true); const [notifLive, setNotifLive] = useState(false); // Read initial view from URL hash (#orders, #settings, #store) useEffect(() => { const hash = window.location.hash.slice(1) as View; if (['overview', 'orders', 'settings', 'store'].includes(hash)) { setView(hash); } }, []); // Update URL hash when view changes useEffect(() => { window.location.hash = view; }, [view]); useEffect(() => { if (!authLoading && !user) { router.push('/login?next=/profile'); return; } if (user) { (async () => { const [profileResp, ordersResp, wishResp] = await Promise.all([ api.profile.get(), api.orders.list(), api.wishlist.get(), ]); if (profileResp.success && profileResp.profile) { setProfile(profileResp.profile); setFullName(profileResp.profile.full_name || ''); setPhone(profileResp.profile.phone || ''); setAddress(profileResp.profile.address || ''); } if (ordersResp.success) setOrders(ordersResp.orders || []); if (wishResp.success) setWishlistCount(wishResp.items?.length || 0); })(); } }, [user, authLoading, router]); const saveProfile = async () => { setSaving(true); const result = await api.profile.update({ fullName, phone, address }); setSaving(false); if (result.success) { setProfile({ ...profile, full_name: fullName, phone, address }); setEditing(false); toast({ title: 'Profile updated' }); } else { toast({ title: 'Error', description: result.error, variant: 'destructive' }); } }; const handleLogout = async () => { await logout(); router.push('/'); }; if (authLoading) { return ; } const inputClass = "w-full bg-white/5 border border-white/10 rounded-md px-3 py-2.5 text-sm focus:bg-white/10 focus:border-white/10 outline-none"; // Show "Store" tab only for sellers const tabs: { key: View; label: string; icon: any }[] = [ { key: 'overview', label: 'Overview', icon: User }, { key: 'orders', label: 'Orders', icon: Package }, { key: 'settings', label: 'Settings', icon: SettingsIcon }, ]; if (isSeller) { tabs.splice(2, 0, { key: 'store', label: 'Store', icon: Store }); } return (
{/* Top bar */}

My Account

{/* Profile header — IG-style */}
{profile?.profile_image ? ( ) : ( (user?.email || '?').charAt(0).toUpperCase() )}
{orders.length}
Orders
{wishlistCount}
Wishlist
{isSeller ? 'Yes' : 'No'}
Seller
{/* Name + email */}
{fullName || user?.email?.split('@')[0]} {isSeller && ( SELLER )}
{user?.email}
{/* Quick action button */}
{/* Tab bar — IG-style */}
{tabs.map((t) => { const Icon = t.icon; return ( ); })}
{/* ===== OVERVIEW VIEW ===== */} {view === 'overview' && (
{/* Quick links grid — Amazon account style */}
{[ { icon: Package, label: 'Orders', sub: `${orders.length}`, view: 'orders' as View }, { icon: Heart, label: 'Wishlist', sub: `${wishlistCount}`, href: '/wishlist' }, { icon: ShoppingBag, label: 'Cart', sub: '', href: '/cart' }, { icon: Clapperboard, label: 'Shorts', sub: '', href: '/shorts' }, { icon: Link2, label: 'WhatsApp', sub: '', href: '/link-account' }, { icon: Send, label: 'Telegram', sub: '', href: '/telegram' }, ].map((item, i) => { const Icon = item.icon; const content = ( <>
{item.label}
{item.sub &&
{item.sub}
} ); return item.href ? ( {content} ) : ( ); })}
{/* Recent orders */}

Recent Orders

{orders.length === 0 ? (

No orders yet

Start shopping
) : (
{orders.slice(0, 3).map((order) => (
Order #{String(order.id).slice(-6)}
{order.items?.length || 0} item(s) · {formatPrice(order.total || 0)}
{order.status || 'pending'}
))}
)}
{/* Become a seller CTA (only for non-sellers) */} {!isSeller && (
Become a Seller
Start selling on Cellex today
)}
)} {/* ===== ORDERS VIEW ===== */} {view === 'orders' && (
{orders.length === 0 ? (

No orders yet

When you place orders, they'll appear here.

Start shopping
) : (
{orders.map((order) => (
Order #{String(order.id).slice(-6)}
{order.created_at ? timeAgo(order.created_at) : ''} · {formatPrice(order.total || 0)}
{order.status || 'pending'}
{/* Order items */}
{(order.items || []).slice(0, 4).map((item: any, i: number) => (
{item.product?.image_url || item.image_url ? ( ) : (
)}
))}
))}
)}
)} {/* ===== STORE VIEW (sellers only) ===== */} {view === 'store' && isSeller && (
Seller Dashboard
Manage products, orders, videos
{[ { icon: Package, label: 'Products', href: '/seller/products' }, { icon: ShoppingBag, label: 'Orders', href: '/seller/orders' }, { icon: Clapperboard, label: 'Videos & Reels', href: '/seller/videos' }, { icon: Store, label: 'Store Profile', href: '/seller/profile' }, ].map((item, i) => { const Icon = item.icon; return ( {item.label} ); })}
)} {/* ===== SETTINGS VIEW ===== */} {view === 'settings' && (
{/* Profile edit section */}

Profile

{!editing ? (
{fullName || 'Not set'}
{user?.email}
{phone && (
{phone}
)} {address && (
{address}
)}
) : (
setFullName(e.target.value)} placeholder="Your name" className={inputClass} />
setPhone(e.target.value)} placeholder="08012345678" className={inputClass} />