'use client'; import { useEffect, useState } from 'react'; import { useParams, useRouter } from 'next/navigation'; import { useProcessingStatus } from '@/hooks/useStatementAPI'; import { apiClient } from '@/services/api-client'; import { motion, AnimatePresence } from 'framer-motion'; import { FileText, Search, Zap, BarChart3, FileCheck, CheckCircle, AlertCircle, Loader2, Clock, CalendarDays, AlertTriangle, } from 'lucide-react'; const STEP_ICONS = { read: FileText, detect: Search, extract: Zap, audit: BarChart3, generate: FileCheck, }; const STEP_META = [ { id: 'read', title: 'Reading PDF', description: 'Extracting document pages and content' }, { id: 'detect', title: 'Detecting Bank', description: 'Identifying bank statement format' }, { id: 'extract', title: 'Extracting Transactions', description: 'Parsing transaction records' }, { id: 'audit', title: 'Running Audits', description: 'Validating and reconciling data' }, { id: 'generate', title: 'Generating Workbook', description: 'Creating Excel workbook output' }, ]; export function ProcessingFeature() { const params = useParams(); const router = useRouter(); const statementId = params.statementId as string; const { data: statusData, isError, isFetching } = useProcessingStatus(statementId, true); const [monthsWarning, setMonthsWarning] = useState<{ months: number; label: string } | null>(null); const [showMonthsModal, setShowMonthsModal] = useState(false); const [allowRedirect, setAllowRedirect] = useState(false); // When processing completes, fetch export summary to check months useEffect(() => { if (statusData?.overallProgress === 100 && !allowRedirect) { apiClient.getExportSummary(statementId) .then((summary) => { if (summary.monthsAvailable !== undefined && summary.monthsAvailable < 3) { setMonthsWarning({ months: summary.monthsAvailable, label: summary.monthRangeLabel || '', }); setShowMonthsModal(true); } else { setAllowRedirect(true); } }) .catch((err) => { console.error('Failed to fetch export summary:', err); // If fetch fails, just allow redirect setAllowRedirect(true); }); } }, [statusData?.overallProgress, statementId, allowRedirect]); // Redirect to review once allowed useEffect(() => { let timer: ReturnType | undefined; if (statusData?.overallProgress === 100 && allowRedirect) { timer = setTimeout(() => { router.push(`/review/${statementId}`); }, 1500); } return () => { if (timer) { clearTimeout(timer); } }; }, [statusData?.overallProgress, allowRedirect, isFetching, router, statementId]); const stages = statusData?.stages || STEP_META.map((s) => ({ key: s.id, label: s.title, status: 'pending' as const, progress: 0 })); const overallProgress = statusData?.overallProgress ?? 0; const estimatedSeconds = statusData?.estimatedSecondsRemaining; return (
{/* Months warning modal — requires explicit OK to continue */} {showMonthsModal && monthsWarning && (
{/* Backdrop */} {/* Modal card */} {/* Amber header strip */}

Incomplete Statement Period

{/* Body */}
{/* Months chip */}
Months uploaded
{monthsWarning.months}
{monthsWarning.label && (
Period {monthsWarning.label}
)}

Only{' '} {monthsWarning.months} month{monthsWarning.months === 1 ? '' : 's'} {' '} of data was detected. Monthly averages in the export will still use a{' '} 3-month divisor — missing months are treated as{' '} $0.

You can upload additional statements later to improve the accuracy of monthly averages.

{/* Footer */}
)}
{/* Header */}

Processing Statement

Please wait while we extract and validate your bank statement

{/* Overall Progress */}
Overall Progress {overallProgress}%
{estimatedSeconds != null && estimatedSeconds > 0 && (
~{estimatedSeconds}s remaining
)}
{/* Pipeline Steps */}
{STEP_META.map((meta, idx) => { const stage = stages.find((s) => s.key === meta.id); const status = stage?.status ?? 'pending'; const progress = stage?.progress ?? 0; const Icon = STEP_ICONS[meta.id as keyof typeof STEP_ICONS]; return ( {/* Step Icon */}
{status === 'completed' ? ( ) : status === 'failed' ? ( ) : status === 'active' ? ( ) : ( )}
{/* Step Info */}

{meta.title}

{status === 'active' && ( {progress}% )} {status === 'completed' && ( Done )}

{meta.description}

{status === 'active' && (
)}
); })}
{/* Error State */} {isError && ( Processing failed. Please try again or contact support. )} {/* Completion */} {overallProgress === 100 && ( {showMonthsModal ? '✓ Processing complete — please review the notice above to continue…' : '✓ Processing complete — redirecting to Review Workspace…'} )}
); }