import { forecastBusinessPlan, restoreBusinessPlanSnapshot, compareBusinessScenarios, } from './business-financial-plan.ts'; import { buildProjectDossierRows } from './project-dossier.ts'; import { restoreFinancialToolsSnapshot } from './financial-calculators.ts'; /** Reuses the dossier's existing report for loan, budget, funding and break-even. */ export function guidanceFinancialTools(draft: Record | null) { const snapshot = typeof draft?.financial_tools_snapshot === 'string' ? draft.financial_tools_snapshot : ''; const assumptions = restoreFinancialToolsSnapshot(snapshot); if (!assumptions) return null; const input = { financial_tools_snapshot: snapshot, currency: typeof draft?.currency === 'string' ? draft.currency : '', }; return { calculator: 'financial-calculators/project-dossier', snapshotVersion: 1, assumptions, rows: buildProjectDossierRows(input).filter( (row) => row[0] === 'Plano financeiro', ), }; } /** The assistant only presents outputs of the existing calculator. */ export function guidanceFinance(draft: Record | null) { const snapshot = typeof draft?.business_plan_snapshot === 'string' ? draft.business_plan_snapshot : ''; const plan = restoreBusinessPlanSnapshot(snapshot); if (!plan) return { status: 'indeterminate' as const, reason: 'Sem snapshot financeiro compatível.', result: null, scenarios: null, }; try { const result = forecastBusinessPlan(plan); let scenarios: ReturnType | null = null; try { scenarios = compareBusinessScenarios(plan); } catch { /* Missing scenario inputs remain indeterminate. */ } return { status: 'calculated' as const, reason: 'Estimativa determinística; pressupostos declarados, sem validação de procura ou financiamento.', result, scenarios, snapshotVersion: (JSON.parse(snapshot) as { version: number }) .version, assumptions: plan, calculator: 'business-financial-plan', tax: 'indeterminate' as const, }; } catch (error) { return { status: 'indeterminate' as const, reason: error instanceof Error ? error.message : 'Pressupostos incompatíveis.', result: null, scenarios: null, }; } }