import { CreditCard, RefreshCw } from "lucide-react"; import { Badge } from "../../../components/ui/badge"; import { Card, CardContent, CardHeader, CardTitle } from "../../../components/ui/card"; import { Separator } from "../../../components/ui/separator"; import { SpinnerIcon } from "../../../components/ui/spinner-icon"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "../../../components/ui/table"; import { cn } from "../../../lib/utils"; import { formatPlanCategory } from "../../../lib/subscriptionPlans"; import type { UserSubscriptionsData } from "../../../types/userAdmin.types"; function formatStatusLabel(status: string): string { const value = status.trim(); if (!value) return "—"; return value .split("_") .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()) .join(" "); } function formatCategoryLabel(value: string): string { return formatPlanCategory(value); } function formatDateTime(value?: string | null): string { if (!value?.trim()) return "—"; const parsed = new Date(value); if (Number.isNaN(parsed.getTime())) return "—"; return parsed.toLocaleString(undefined, { month: "short", day: "numeric", year: "numeric", hour: "2-digit", minute: "2-digit", }); } function getSubscriptionStatusClasses(status: string): string { const normalized = status.trim().toUpperCase(); if (normalized === "ACTIVE" || normalized === "SUCCESS") { return "bg-mint-500/15 text-mint-500 border border-mint-500/25"; } if (normalized === "PENDING") { return "bg-gold-100 text-gold-600 border border-gold-300/40"; } if (normalized === "FAILED" || normalized === "EXPIRED") { return "bg-destructive/15 text-destructive border border-destructive/25"; } return "bg-grayScale-100 text-grayScale-600 border border-grayScale-200"; } function formatDuration(value: number, unit: string): string { if (!value) return "—"; const label = unit.trim().toLowerCase(); const plural = value === 1 ? label.replace(/s$/, "") : label.endsWith("s") ? label : `${label}s`; return `${value} ${plural}`; } function formatMoney(amount: number, currency: string): string { if (!Number.isFinite(amount)) return "—"; const code = currency.trim() || "ETB"; return `${amount.toLocaleString()} ${code}`; } export function UserSubscriptionsSection({ subscriptions, loading, error, }: { subscriptions: UserSubscriptionsData | null; loading: boolean; error: string | null; }) { const recentPayments = subscriptions?.payments.slice(0, 8) ?? []; return (
Subscriptions
{subscriptions ? ( {formatStatusLabel(subscriptions.display_status)} ) : null}
{loading ? (
Loading subscriptions...
) : null} {error ?

{error}

: null} {!loading && !error && subscriptions ? ( <>
Active subscription {subscriptions.has_active_subscription ? "Yes" : "No"}
{Object.keys(subscriptions.active_by_category).length > 0 ? ( <>

Access by category

{Object.entries(subscriptions.active_by_category).map(([category, active]) => ( {formatCategoryLabel(category)} ))}
) : null}

Active plans

{subscriptions.active_subscriptions.length === 0 ? (

No active subscriptions.

) : (
{subscriptions.active_subscriptions.map((subscription) => (

{subscription.plan_name}

{formatCategoryLabel(subscription.plan_category)}

{formatStatusLabel(subscription.status)}

Price: {formatMoney(subscription.price, subscription.currency)}

Duration: {formatDuration(subscription.duration_value, subscription.duration_unit)}

Starts: {formatDateTime(subscription.starts_at)}

Expires: {formatDateTime(subscription.expires_at)}

Payment: {subscription.payment_method || "—"}

Auto-renew: {subscription.auto_renew ? "On" : "Off"}

))}
)}
{recentPayments.length > 0 ? ( <>

Recent payments

Plan Amount Status Method Date {recentPayments.map((payment) => ( {payment.plan_name || "—"} {formatMoney(payment.amount, payment.currency)} {formatStatusLabel(payment.status)} {payment.payment_method || "—"} {formatDateTime(payment.paid_at || payment.created_at)} ))}
) : null} ) : null} {!loading && !error && !subscriptions ? (

No subscription data available.

) : null}
); }