import React from 'react'; import { Loader2, RefreshCw, Sparkles } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Separator } from '@/components/ui/separator'; import { AdminLayout } from '../components/AdminLayout'; import { getTenantPackagesOverview, TenantPackageSummary } from '../api'; import { isAuthError } from '../auth/tokens'; export default function BillingPage() { const { t, i18n } = useTranslation(['management', 'dashboard']); const locale = React.useMemo( () => (i18n.language?.startsWith('en') ? 'en-GB' : 'de-DE'), [i18n.language] ); const [packages, setPackages] = React.useState([]); const [activePackage, setActivePackage] = React.useState(null); const [loading, setLoading] = React.useState(true); const [error, setError] = React.useState(null); const formatDate = React.useCallback( (value: string | null | undefined) => { if (!value) return '--'; const date = new Date(value); if (Number.isNaN(date.getTime())) return '--'; return date.toLocaleDateString(locale, { day: '2-digit', month: 'short', year: 'numeric' }); }, [locale] ); const formatCurrency = React.useCallback( (value: number | null | undefined, currency = 'EUR') => { if (value === null || value === undefined) return '--'; return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value); }, [locale] ); const packageLabels = React.useMemo( () => ({ statusActive: t('billing.sections.packages.card.statusActive'), statusInactive: t('billing.sections.packages.card.statusInactive'), used: t('billing.sections.packages.card.used'), available: t('billing.sections.packages.card.available'), expires: t('billing.sections.packages.card.expires'), }), [t] ); const loadAll = React.useCallback(async () => { setLoading(true); setError(null); try { const packagesResult = await getTenantPackagesOverview(); setPackages(packagesResult.packages); setActivePackage(packagesResult.activePackage); } catch (err) { if (!isAuthError(err)) { setError(t('billing.errors.load')); } } finally { setLoading(false); } }, [t]); React.useEffect(() => { void loadAll(); }, [loadAll]); const actions = ( ); return ( {error && ( {t('dashboard:alerts.errorTitle')} {error} )} {loading ? ( ) : ( <>
{t('billing.sections.overview.title')} {t('billing.sections.overview.description')}
{activePackage ? activePackage.package_name : t('billing.sections.overview.emptyBadge')}
{activePackage ? (
) : ( )}
{t('billing.sections.packages.title')} {t('billing.sections.packages.description')} {packages.length === 0 ? ( ) : ( packages.map((pkg) => ( )) )} )}
); } function InfoCard({ label, value, helper, tone, }: { label: string; value: string | number | null | undefined; helper?: string; tone: 'pink' | 'amber' | 'sky' | 'emerald'; }) { const toneClass = { pink: 'from-pink-50 to-rose-100 text-pink-700', amber: 'from-amber-50 to-yellow-100 text-amber-700', sky: 'from-sky-50 to-blue-100 text-sky-700', emerald: 'from-emerald-50 to-green-100 text-emerald-700', }[tone]; return (
{label}
{value ?? '--'}
{helper &&

{helper}

}
); } function PackageCard({ pkg, isActive, labels, formatDate, formatCurrency, }: { pkg: TenantPackageSummary; isActive: boolean; labels: { statusActive: string; statusInactive: string; used: string; available: string; expires: string; }; formatDate: (value: string | null | undefined) => string; formatCurrency: (value: number | null | undefined, currency?: string) => string; }) { return (

{pkg.package_name}

{formatDate(pkg.purchased_at)} · {formatCurrency(pkg.price, pkg.currency ?? 'EUR')}

{isActive ? labels.statusActive : labels.statusInactive}
{labels.used}: {pkg.used_events} {labels.available}: {pkg.remaining_events ?? '--'} {labels.expires}: {formatDate(pkg.expires_at)}
); } function EmptyState({ message }: { message: string }) { return (

{message}

); } function BillingSkeleton() { return (
{Array.from({ length: 3 }).map((_, index) => (
{Array.from({ length: 4 }).map((__, placeholderIndex) => (
))}
))}
); }