'use client' import React, { useState } from 'react' import { useMutation } from '@tanstack/react-query' import type { AiAnalysis, BREDecisionResponse, Deviation } from '@/types' import { apiClient } from '@/lib/api-client' interface Props { decision: BREDecisionResponse } export function AiAnalystPanel({ decision }: Props) { const [aiResult, setAiResult] = useState(decision.aiAnalysis ?? null) const [activeSection, setActiveSection] = useState('summary') const analyze = useMutation({ mutationFn: () => apiClient.post('/ai/analyze-credit', { applicationData: {}, decision: decision.decision, riskScore: decision.riskScore, riskCategory: decision.riskCategory, deviations: [], }) as Promise, onSuccess: setAiResult, }) const sections = [ { id: 'summary', label: 'Risk Summary', icon: '📊' }, { id: 'credit', label: 'Credit Analysis', icon: '💳' }, { id: 'strengths', label: 'Strengths', icon: '💪' }, { id: 'weaknesses', label: 'Weaknesses', icon: '⚠️' }, { id: 'deviations', label: 'Deviations', icon: '📋' }, { id: 'docs', label: 'Documents Required', icon: '📂' }, { id: 'notes', label: 'Underwriting Notes', icon: '📝' }, ] return (
{/* Header */}
🤖

OPTIM AI Credit Analyst

AI-powered credit risk assessment

{!aiResult && ( )} {aiResult && (
Confidence:
{Math.round(aiResult.confidenceScore * 100)}%
)}
{/* Decision Summary Bar */}
Decision: {decision.decision}
Risk Score: {decision.riskScore.toFixed(1)}/100
Risk:
Rules: {decision.rulesPassed}/{decision.totalRulesEvaluated} matched
Deviations: {decision.deviationsCount}
Time: {decision.executionMs}ms
{aiResult ? (
{/* Sidebar Nav */}
{sections.map((s) => ( ))}
{/* Content */}
{activeSection === 'summary' && ( )} {activeSection === 'credit' && ( )} {activeSection === 'strengths' && ( )} {activeSection === 'weaknesses' && ( )} {activeSection === 'deviations' && (
{aiResult.rejectionReasons.length > 0 && (
)}
)} {activeSection === 'docs' && ( )} {activeSection === 'notes' && ( )}
) : (
🤖

AI Analysis Not Yet Run

Click "Run AI Analysis" to get comprehensive credit insights

)}
) } function AiSection({ title, icon, content }: { title: string; icon: string; content: string }) { return (

{icon} {title}

{content || No content available}
) } function ListSection({ title, icon, items, variant }: { title: string; icon: string; items: string[]; variant: 'success' | 'warning' | 'error' | 'info' }) { const styles = { success: { dot: 'bg-emerald-500', bg: 'bg-emerald-50', border: 'border-emerald-100' }, warning: { dot: 'bg-amber-500', bg: 'bg-amber-50', border: 'border-amber-100' }, error: { dot: 'bg-red-500', bg: 'bg-red-50', border: 'border-red-100' }, info: { dot: 'bg-blue-500', bg: 'bg-blue-50', border: 'border-blue-100' }, }[variant] return (

{icon} {title}

{items.length === 0 ? (

None identified

) : (
{items.map((item, idx) => (
{item}
))}
)}
) } function TrafficLightIndicator({ light }: { light: string }) { return (
{(['GREEN', 'AMBER', 'RED'] as const).map((l) => (
))}
) } function RiskBadge({ category }: { category: string }) { const colors: Record = { LOW: 'ml-2 px-2 py-0.5 bg-emerald-100 text-emerald-700 rounded text-xs font-semibold', MEDIUM: 'ml-2 px-2 py-0.5 bg-blue-100 text-blue-700 rounded text-xs font-semibold', HIGH: 'ml-2 px-2 py-0.5 bg-orange-100 text-orange-700 rounded text-xs font-semibold', CRITICAL: 'ml-2 px-2 py-0.5 bg-red-100 text-red-700 rounded text-xs font-semibold', } return {category} }