import { useState } from "react" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog" import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle, } from "@/components/ui/sheet" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Separator } from "@/components/ui/separator" import { ChevronLeft, ChevronRight, Loader2, ShoppingCart, Trash2, RefreshCw, Eye, User, Mail, Ticket, CheckCircle2, XCircle, Clock, ExternalLink, Package, } from "lucide-react" import { useAbandonedCartCarts, useCartDetails } from "../../hooks/use-abandoned-cart" import { CartDetailsResponse } from "../../types" function StatusBadge({ status }: { status: string }) { switch (status) { case 'abandoned': return Abandoned case 'recovered': return Recovered case 'restored': return Restored case 'ordered': return Awaiting Payment case 'unsubscribed': return Unsubscribed case 'active': return Active default: return {status} } } function formatDate(dateStr: string) { if (!dateStr) return '—' const date = new Date(dateStr) return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit', }) } function formatCurrency(amount: string, currency: string) { const num = parseFloat(amount) if (isNaN(num)) return '—' try { return new Intl.NumberFormat(undefined, { style: 'currency', currency: currency || 'USD', }).format(num) } catch { return `${currency} ${num.toFixed(2)}` } } function CartDetailsSheet({ details, loading, open, onOpenChange, }: { details: CartDetailsResponse | null loading: boolean open: boolean onOpenChange: (open: boolean) => void }) { return ( Cart Details Full breakdown of this abandoned cart. {loading ? (
) : details ? (
{/* Customer Info */} Customer
Name {details.customer.name || 'Unknown'}
Email {details.customer.email || '—'}
Type {details.customer.type}
{/* Status & Recovery */} Status
Status
Created {formatDate(details.created_at)}
{details.recovered_at && (
Recovered {formatDate(details.recovered_at)}
)} {details.order_id && (
Order #{details.order_id}
)} {details.coupon_code && (
Coupon {details.coupon_code}
)}
{/* Products */} Products ({details.products.length})
{details.products.map((product, idx) => (
{product.image ? ( {product.name} ) : (
)}

{product.name}

Qty: {product.quantity} × {formatCurrency(product.price, details.currency)}

{!product.in_stock && ( Out of stock )}
))}
Cart Total {formatCurrency(details.cart_total, details.currency)}
{/* Email History */} Email History ({details.email_history.length}) {details.email_history.length === 0 ? (

No emails sent yet.

) : (
{details.email_history.map((email, idx) => (
Email #{email.email_number} {formatDate(email.sent_at)}

{email.subject}

{email.opened_at ? : } {email.opened_at ? `Opened ${formatDate(email.opened_at)}` : 'Not opened'} {email.clicked_at ? : } {email.clicked_at ? `Clicked ${formatDate(email.clicked_at)}` : 'Not clicked'}
))}
)}
) : (

Could not load cart details.

)}
) } export function CartsTab() { const { carts, total, page, pages, loading, statusFilter, setPage, deleteCart, changeStatusFilter, refresh, } = useAbandonedCartCarts() const [selectedCartId, setSelectedCartId] = useState(null) const [sheetOpen, setSheetOpen] = useState(false) const { details, loading: detailsLoading } = useCartDetails(selectedCartId) const handleView = (cartId: number) => { setSelectedCartId(cartId) setSheetOpen(true) } if (loading) { return (
) } return (
{/* Filters */}
{total} {total === 1 ? 'cart' : 'carts'} total
{/* Table */} {carts.length === 0 ? (

No abandoned carts yet

Abandoned carts will appear here once customers leave items in their carts.

) : ( <>
Customer Items Total Status Emails Date Actions {carts.map((cart) => (
{cart.customer_name && (

{cart.customer_name}

)}

{cart.email || '—'}

{cart.item_count} {cart.item_count === 1 ? 'item' : 'items'} {formatCurrency(cart.cart_total, cart.currency)} {cart.emails_sent} {formatDate(cart.created_at)}
Delete this cart record? This will permanently delete this abandoned cart record and its email history. This action cannot be undone. Cancel deleteCart(cart.id)}>Delete
))}
{/* Pagination */} {pages > 1 && (

Page {page} of {pages}

)} )} {/* Cart Details Sheet */} { setSheetOpen(open) if (!open) setSelectedCartId(null) }} />
) }