import React, { useMemo, useState } from 'react'; import { ScrollView, StyleSheet, Text, View, } from 'react-native'; import type { CompositeScreenProps } from '@react-navigation/native'; import type { BottomTabScreenProps } from '@react-navigation/bottom-tabs'; import type { NativeStackScreenProps } from '@react-navigation/native-stack'; import Ionicons from '@react-native-vector-icons/ionicons'; import { AppModal, Button, Card, EmptyState, Loader, ScreenShell } from '../../../components'; import { useToast } from '../../../hooks/useToast'; import type { MainTabParamList, ServicesStackParamList } from '../../../navigation/types'; import { useTheme } from '../../../theme'; import { formatCurrency, formatDate } from '../../../utils/formatters'; import { TransactionItem } from '../../cards/components/TransactionItem'; import { ExpandableSection } from '../../cards/components/ExpandableSection'; import { DepositStatusPill } from '../components/DepositStatusPill'; import { useCloseDepositMutation, useGetDepositDetailQuery, } from '../services/depositApi'; import { calculatePrematureClosureAmount, getDaysUntilMaturity, getDepositProgress, isDepositNearMaturity, } from '../utils/depositCalculators'; type Props = CompositeScreenProps< NativeStackScreenProps, BottomTabScreenProps >; export default function DepositDetailsScreen({ navigation, route }: Props) { const theme = useTheme(); const toast = useToast(); const [confirmVisible, setConfirmVisible] = useState(false); const { data: deposit, isLoading, isError, refetch } = useGetDepositDetailQuery({ depositId: route.params.depositId, kind: route.params.kind, }); const [closeDeposit, closeState] = useCloseDepositMutation(); const nearMaturity = deposit ? isDepositNearMaturity(deposit) : false; const closureQuote = deposit ? calculatePrematureClosureAmount(deposit) : null; const progress = deposit ? getDepositProgress(deposit.startDate, deposit.maturityDate) : 0; const daysUntilMaturity = deposit ? Math.max(getDaysUntilMaturity(deposit.maturityDate), 0) : 0; const history = useMemo( () => (deposit?.transactions ?? []).map((transaction) => ({ id: transaction.id, merchant: transaction.title, amount: transaction.amount, direction: transaction.direction, status: transaction.status, occurredAt: transaction.occurredAt, category: transaction.category, })), [deposit?.transactions], ); const handleCloseDeposit = async () => { if (!deposit) { return; } try { await closeDeposit({ depositId: deposit.id, kind: deposit.kind, }).unwrap(); setConfirmVisible(false); toast( deposit.status === 'matured' ? 'Maturity proceeds credited' : 'Deposit closed', 'success', deposit.status === 'matured' ? 'The maturity amount has been credited to your linked account.' : 'Closure request completed successfully.', ); } catch { toast( 'Unable to close deposit', 'error', 'Please try again in a moment.', ); } }; if (isLoading) { return ( ); } if (isError || !deposit) { return ( ); } const canClose = deposit.status !== 'closed' && deposit.canWithdrawPrematurely; const closeActionLabel = deposit.status === 'matured' ? 'Credit maturity proceeds' : 'Premature close'; const investedAmount = deposit.kind === 'fd' ? deposit.principalAmount : deposit.totalInvestment; return ( {deposit.type} {deposit.maskedNumber} Invested amount {formatCurrency(investedAmount)} Maturity value {formatCurrency(deposit.maturityAmount)} Interest earned {formatCurrency(deposit.interestEarned)} Tracking {deposit.status === 'closed' ? 'Closed' : deposit.status === 'matured' ? 'Ready' : `${daysUntilMaturity} days left`} Started on {formatDate(deposit.startDate)} · Matures on {formatDate(deposit.maturityDate)} Interest rate {deposit.interestRate}% p.a. Tenure {deposit.tenureMonths} months Payout frequency {deposit.payoutLabel} Settlement account {deposit.settlementAccountMasked} {deposit.nominee ? ( Nominee {deposit.nominee.name} · {deposit.nominee.relationship} ) : null} Total investment {formatCurrency(deposit.totalInvestment)} Estimated proceeds {formatCurrency(closureQuote?.netAmount ?? deposit.maturityAmount)} Penalty {formatCurrency(closureQuote?.penaltyAmount ?? 0)} Maturity date {formatDate(deposit.maturityDate)} {history.length ? ( history.map((item, index) => ( )) ) : ( )} Actions Estimate new returns or close the deposit if you need funds earlier