/** * Test USDT wallet — play-money for discovering the platform without real funds. */ import { Alert, Box, Button, Card, CardContent, Chip, Stack, Typography, } from '@mui/material'; import ScienceRoundedIcon from '@mui/icons-material/ScienceRounded'; import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { api } from '../../../shared/services/api'; import { apiErrorMessage } from '../../../shared/utils/apiError'; import type { BalanceEntry } from '../types'; import { USDT_WALLET_CODE } from '../utils/walletCapabilities'; import { formatBalance, rawToHuman } from '../utils/walletFormat'; type TestPredictionLimit = { enabled: boolean; maxPerWindow: number; windowHours: number; usedInWindow: number; remaining: number; resetsAt: string | null; canPredict: boolean; reason: string | null; }; type TestWalletStatus = { enabled: boolean; balanceRaw: string; walletTypeId: number; walletCode: string | null; decimals: number; creditAmountRaw: string; maxRequests: number; requestsUsed: number; canRequest: boolean; reason: string | null; cooldownEndsAt: string | null; predictionLimit?: TestPredictionLimit; }; function formatWaitUntil(resetsAt: string | null, windowHours: number): string { if (resetsAt) { const ms = Math.max(0, new Date(resetsAt).getTime() - Date.now()); const totalMinutes = Math.max(1, Math.ceil(ms / 60_000)); const days = Math.floor(totalMinutes / (60 * 24)); const hours = Math.floor((totalMinutes % (60 * 24)) / 60); const minutes = totalMinutes % 60; const parts: string[] = []; if (days > 0) parts.push(`${days}d`); if (hours > 0) parts.push(`${hours}h`); if (minutes > 0 && days === 0) parts.push(`${minutes}m`); if (parts.length === 0) parts.push('1m'); return parts.join(' '); } return `${windowHours}h`; } type TestWalletCardProps = { balance: BalanceEntry; onSuccess?: (message: string) => void; onError?: (message: string) => void; }; export function TestWalletCard({ balance, onSuccess, onError }: TestWalletCardProps) { const { t } = useTranslation(); const qc = useQueryClient(); const [requesting, setRequesting] = useState(false); const statusQ = useQuery({ queryKey: ['test-wallet-status'], queryFn: async () => { const res = await api.get('/api/v1/test-wallet'); return res.data; }, }); const testRaw = balance.testAvailable ?? statusQ.data?.balanceRaw ?? '0'; const hasTest = BigInt(testRaw) > 0n; const status = statusQ.data; const creditHuman = status ? rawToHuman(status.creditAmountRaw, status.decimals) : '—'; const requestCredits = async () => { setRequesting(true); try { const res = await api.post<{ message: string }>('/api/v1/test-wallet/request-credits'); await Promise.all([ qc.invalidateQueries({ queryKey: ['wallet-balances'] }), qc.invalidateQueries({ queryKey: ['test-wallet-status'] }), ]); onSuccess?.(res.data.message); } catch (e: unknown) { const err = e as { response?: { data?: { error?: string } }; message?: string }; onError?.(apiErrorMessage(err, t('auth.requestFailed'))); } finally { setRequesting(false); } }; return ( {t('wallet.testCard.title')} {t('wallet.testCard.subtitle')} {t('wallet.testCard.balance')} {formatBalance(testRaw, balance.walletType.decimals, USDT_WALLET_CODE)} {t('wallet.testCard.hint')} {status?.enabled ? ( {status.maxRequests > 0 && ( {t('wallet.testCard.requestsUsed', { used: status.requestsUsed, max: status.maxRequests, })} )} {!status.canRequest && status.reason && ( {status.reason} {status.cooldownEndsAt && ( <> {' '} {t('wallet.testCard.availableAfter', { date: new Date(status.cooldownEndsAt).toLocaleString(), })} )} )} ) : ( {t('wallet.testCard.disabled')} )} {status?.predictionLimit?.enabled && status.predictionLimit.maxPerWindow > 0 && ( {t('wallet.testCard.predictionsUsed', { used: status.predictionLimit.usedInWindow, max: status.predictionLimit.maxPerWindow, hours: status.predictionLimit.windowHours, })} {!status.predictionLimit.canPredict && ( {t('wallet.testCard.predictionLimitReached', { wait: formatWaitUntil( status.predictionLimit.resetsAt, status.predictionLimit.windowHours ), })} {status.predictionLimit.resetsAt && ( <> {' '} {t('wallet.testCard.resetsAt', { date: new Date(status.predictionLimit.resetsAt).toLocaleString(), })} )} )} )} ); }