'use client';
import { useState, useEffect } from 'react';
import { useParams } from 'next/navigation';
import Card from '@/components/ui/Card';
import Button from '@/components/ui/Button';
import { useServices } from '@/lib/services/ServiceProvider';
import { LeagueWalletViewModel } from '@/lib/view-models/LeagueWalletViewModel';
import {
Wallet,
DollarSign,
ArrowUpRight,
ArrowDownLeft,
Clock,
AlertTriangle,
CheckCircle,
XCircle,
Download,
CreditCard,
TrendingUp,
Calendar
} from 'lucide-react';
function TransactionRow({ transaction }: { transaction: any }) {
const isIncoming = transaction.amount > 0;
const typeIcons = {
sponsorship: DollarSign,
membership: CreditCard,
withdrawal: ArrowUpRight,
prize: TrendingUp,
};
const TypeIcon = typeIcons[transaction.type];
const statusConfig = {
completed: { color: 'text-performance-green', bg: 'bg-performance-green/10', icon: CheckCircle },
pending: { color: 'text-warning-amber', bg: 'bg-warning-amber/10', icon: Clock },
failed: { color: 'text-racing-red', bg: 'bg-racing-red/10', icon: XCircle },
};
const status = statusConfig[transaction.status];
const StatusIcon = status.icon;
return (
{transaction.description}
{transaction.status}
{transaction.type}
{transaction.reference && (
<>
•
{transaction.reference}
>
)}
•
{transaction.formattedDate}
{transaction.formattedAmount}
{transaction.fee > 0 && (
Fee: ${transaction.fee.toFixed(2)}
)}
);
}
export default function LeagueWalletPage() {
const params = useParams();
const { leagueWalletService } = useServices();
const [wallet, setWallet] = useState(null);
const [withdrawAmount, setWithdrawAmount] = useState('');
const [showWithdrawModal, setShowWithdrawModal] = useState(false);
const [processing, setProcessing] = useState(false);
const [filterType, setFilterType] = useState<'all' | 'sponsorship' | 'membership' | 'withdrawal' | 'prize'>('all');
useEffect(() => {
const loadWallet = async () => {
if (params.id) {
try {
const walletData = await leagueWalletService.getWalletForLeague(params.id as string);
setWallet(walletData);
} catch (error) {
console.error('Failed to load wallet:', error);
}
}
};
loadWallet();
}, [params.id, leagueWalletService]);
if (!wallet) {
return Loading...
;
}
const filteredTransactions = wallet.getFilteredTransactions(filterType);
const handleWithdraw = async () => {
if (!withdrawAmount || parseFloat(withdrawAmount) <= 0) return;
setProcessing(true);
try {
const result = await leagueWalletService.withdraw(
params.id as string,
parseFloat(withdrawAmount),
wallet.currency,
'season-2', // Current active season
'bank-account-***1234'
);
if (!result.success) {
alert(result.message || 'Withdrawal failed');
return;
}
alert(`Withdrawal of $${withdrawAmount} processed successfully!`);
setShowWithdrawModal(false);
setWithdrawAmount('');
// Refresh wallet data
const updatedWallet = await leagueWalletService.getWalletForLeague(params.id as string);
setWallet(updatedWallet);
} catch (err) {
console.error('Withdrawal error:', err);
alert('Failed to process withdrawal');
} finally {
setProcessing(false);
}
};
return (
{/* Header */}
League Wallet
Manage your league's finances and payouts
Export
setShowWithdrawModal(true)}
disabled={!wallet.canWithdraw}
>
Withdraw
{/* Withdrawal Warning */}
{!wallet.canWithdraw && wallet.withdrawalBlockReason && (
Withdrawals Temporarily Unavailable
{wallet.withdrawalBlockReason}
)}
{/* Stats Grid */}
{wallet.formattedBalance}
Available Balance
{wallet.formattedTotalRevenue}
Total Revenue
{wallet.formattedTotalFees}
Platform Fees (10%)
{wallet.formattedPendingPayouts}
Pending Payouts
{/* Transactions */}
Transaction History
setFilterType(e.target.value as typeof filterType)}
className="px-3 py-1.5 rounded-lg border border-charcoal-outline bg-iron-gray text-white text-sm focus:border-primary-blue focus:outline-none"
>
All Transactions
Sponsorships
Memberships
Withdrawals
Prizes
{filteredTransactions.length === 0 ? (
No Transactions
{filterType === 'all'
? 'Revenue from sponsorships and fees will appear here.'
: `No ${filterType} transactions found.`}
) : (
{filteredTransactions.map((transaction) => (
))}
)}
{/* Revenue Breakdown */}
Revenue Breakdown
Total Gross Revenue
$3,200.00
Platform Fee (10%)
-$320.00
Net Revenue
$2,880.00
Payout Schedule
Season 2 Prize Pool
Pending
Distributed after season completion to top 3 drivers
Available for Withdrawal
{wallet.formattedBalance}
Available after Season 2 ends (estimated: Jan 15, 2026)
{/* Withdraw Modal */}
{showWithdrawModal && (
Withdraw Funds
{!wallet.canWithdraw ? (
{wallet.withdrawalBlockReason}
) : (
<>
Destination
Bank Account ***1234
>
)}
setShowWithdrawModal(false)}
className="flex-1"
>
Cancel
{processing ? 'Processing...' : 'Withdraw'}
)}
{/* Alpha Notice */}
Alpha Note: Wallet management is demonstration-only.
Real payment processing and bank integrations will be available when the payment system is fully implemented.
The 10% platform fee and season-based withdrawal restrictions are enforced in the actual implementation.
);
}