import { displayValue } from '@/lib/display-value'; import { Link, useForm } from '@inertiajs/react'; import { Management, Field, control, button } from '../management/shared'; import { capabilities, states } from './labels'; type Item = { [key: string]: unknown; content?: string; title?: string; source_url?: string; source_date?: string; nature?: string; record_id?: string | number; rule?: string; score?: number; score_max?: number; favourable_factors?: string[]; missing_data?: string[]; }; type Result = { items: Item[]; mode: string; language: string; computed_at: string; status: string; }; type Props = { run: { id: string; capability: string; mode: string; rules_version: string; prompt_version: string; latency_ms: number; version_no: number; review_status: string; }; result: Result | null; unavailable: boolean; }; const safeLink = (value: string) => /^https:\/\//i.test(value) || /^\/(?!\/)/.test(value); export default function ResultPage({ run, result, unavailable }: Props) { const en = result?.language === 'en'; const t = (pt: string, english: string) => (en ? english : pt); const feedback = useForm({ rating: 'helpful', review_requested: true }); const review = useForm({ status: 'accepted', version_no: run.version_no }); return ( {t( 'Voltar às consultas e fontes', 'Back to assistance and sources', )}

{t( 'Resultado assistivo, sujeito a revisão. Não constitui decisão oficial ou garantia de retorno ou aprovação.', 'Assistive result requiring review. Not an official decision or a guarantee of return or approval.', )}

{t( 'Confiança não calibrada. Não foram produzidas previsões ou afirmações por um modelo generativo.', 'Confidence is not calibrated. No forecasts or claims were generated by a generative model.', )}

{t('Estado da revisão', 'Review status')}:{' '} {en ? run.review_status : states[run.review_status]}

{unavailable ? (

{t( 'Conteúdo indisponível: capacidade suspensa, fonte alterada, expirada, revogada ou sem consentimento vigente.', 'Content unavailable: suspended capability, changed, expired or revoked source, or withdrawn consent.', )}

) : ( <> {result?.items.length === 0 && (

{t( 'Não há evidência ou correspondências suficientes nesta consulta. Não é possível formular uma conclusão. Pode solicitar revisão humana abaixo.', 'No sufficient evidence or matches for this query. A conclusion cannot be provided. You may request human review below.', )}

)} {run.mode === 'extractive_preview' && (

{t( 'Pré-visualização dos primeiros excertos; não é um resumo completo do documento.', 'Preview of the first excerpts; not a complete document summary.', )}

)} {result?.items.map((item, index) => (

{item.title ?? item.rule?.replaceAll('_', ' ') ?? `${t('Resultado', 'Result')} ${index + 1}`}

{item.content && (
{item.content}
)} {item.source_url && safeLink(item.source_url) && ( {t( 'Consultar fonte / registo original', 'View source / original record', )} )} {item.source_date && (

{t('Data da fonte', 'Source date')}:{' '} {item.source_date}

)} {item.nature && (

{t('Natureza declarada', 'Declared nature')} : {en ? item.nature : states[item.nature]}

)} {item.score !== undefined && (

{t( 'Filtros declarados correspondentes', 'Matching declared filters', )} : {item.score}/{item.score_max}.{' '} {t( 'Esta pontuação não mede rentabilidade ou probabilidade de aprovação.', 'This score does not measure profitability or likelihood of approval.', )}

)} {item.favourable_factors && (

{t( 'Factores correspondentes', 'Matching factors', )} : {item.favourable_factors.join(', ')}

)} {item.missing_data && (

{t( 'Dados ainda necessários', 'Missing information', )} : {item.missing_data.join(', ')}

)} {!item.content && (
{Object.entries(item) .filter( ([key]) => ![ 'title', 'source_url', 'source_date', 'score', 'score_max', 'score_meaning', 'favourable_factors', 'missing_data', 'semantic_similarity', ].includes(key), ) .map(([key, value]) => (
{key.replaceAll('_', ' ')}
{Array.isArray(value) ? value.join(', ') : value === null ? t( 'Não determinado', 'Undetermined', ) : displayValue(value)}
))}
)}
))} )}

{t('Regras', 'Rules')}: {run.rules_version} ·{' '} {t('Calculado em', 'Computed at')}: {result?.computed_at ?? '—'}{' '} · {run.latency_ms} ms

{ e.preventDefault(); feedback.post(`/management/ai/runs/${run.id}/feedback`); }} >

{t( 'Feedback e atendimento humano', 'Feedback and human review', )}

{Object.entries(feedback.errors).map(([k, v]) => (

{v}

))}
{ e.preventDefault(); review.transform((data) => ({ ...data, version_no: run.version_no, })); review.post(`/management/ai/runs/${run.id}/review`); }} >

{t('Revisão independente', 'Independent review')}

{t( 'Uma pessoa diferente deve rever o resultado. Aceitar não altera registos oficiais nem envia conteúdo ao CRM.', 'A different person must review the result. Acceptance does not change official records or send content to CRM.', )}

{Object.entries(review.errors).map(([k, v]) => (

{v}

))}
); }