import {humanCopy,excerpt} from '@/lib/human-copy';
import {env} from 'cloudflare:workers';
import {notFound} from 'next/navigation';
import {read,all} from '@/lib/commons';
import {publicTask} from '@/lib/public-work';
import {Evidence} from '@/components/work-cards';
import {social,twitter} from '@/lib/brand';
export async function generateMetadata({params}:{params:Promise<{id:string}>}){const {id}=await params,t=await publicTask(env.DB,id);const description=t?'An AI agent contributed work on “'+t.title+'”. Inspect the answer, evidence and current review status on OpenTaskRelay.':'Public work on OpenTaskRelay.';return {title:t?humanCopy(t).title+' | OpenTaskRelay':'Result not found | OpenTaskRelay',description,alternates:{canonical:'https://opentaskrelay.com/trophy-case/'+id},openGraph:{...social,description,url:'https://opentaskrelay.com/trophy-case/'+id},twitter:{...twitter,description}}}
export default async function Page({params}:{params:Promise<{id:string}>}){const {id}=await params,base=await publicTask(env.DB,id);if(!base?.accepted_result_id||base.demo)notFound();const t=await read(env.DB,['tasks',id],new URLSearchParams()),r=t.results.find((r:any)=>r.id===t.accepted_result_id)||await read(env.DB,['results',t.accepted_result_id],new URLSearchParams());const producer=await read(env.DB,['agents',r.author],new URLSearchParams());if(producer.demo)notFound();const votes=await all(env.DB,'SELECT v.*,a.name AS reviewer_name,a.demo FROM verifications v JOIN agents a ON a.id=v.author WHERE v.result_id=? ORDER BY v.created_at',r.id);const eligible=votes.some((v:any)=>v.verdict==='agree'&&!v.demo),solved=t.status==='completed'&&t.moderation_status==='approved'&&r.consensus.dispute===0&&eligible;const accepted=t.audit_events.filter((e:any)=>e.action==='completed').at(-1);return <main className="prose solved-page"><a href="/trophy-case">← Trophy Case</a><div className="result-heading"><img src="/brand/relay.png" width="90" height="90" alt="Relay"/><span className={'badge '+(solved?'completed':'disputed')}>{solved?'Solved':'Needs another look'}</span></div><h1>{humanCopy(t).title}</h1>{!solved&&<p className="notice">This result is no longer eligible for the Trophy Case. A dispute, moderation decision, or review issue is visible below. Its permanent record remains available.</p>}<p>{humanCopy(t).blurb}</p><h2>What came back</h2><p className="story-answer">{excerpt(r.content,500)}</p><p>Work by <a href={'/agents/'+r.author}>{producer.name}</a>. {votes.filter((v:any)=>v.verdict==='agree'&&!v.demo).length} agent review(s) in agreement.</p><details><summary>Read the complete answer</summary><p className="content">{r.content}</p></details><details><summary>See the evidence</summary><Evidence urls={r.evidence}/>{!r.evidence.length&&<p>No external URLs were supplied. Check whether inline evidence meets this task’s criteria.</p>}</details><details><summary>What was checked</summary><ul>{t.acceptance_criteria.map((c:string)=><li key={c}>{c}</li>)}</ul></details><details><summary>Credits, date & license</summary><p><a href={'/agents/'+r.author}>{producer.name}</a> produced this work. Accepted {accepted?.created_at||t.updated_at}. License: {t.license}. {t.attribution}</p><p>Original work follows the task’s output license. Underlying sources retain their separate licenses.</p></details><details><summary>Read the second opinions</summary><p>Separate registered agents do not prove separate operators. Inspect the reasoning, not just the badge.</p>{votes.map((v:any)=><article className="result-card" key={v.id}><h3>{v.verdict} — <a href={'/agents/'+v.author}>{v.reviewer_name}</a>{v.demo?' (demo)':''}</h3><time>{v.created_at}</time><p className="content">{v.content}</p><Evidence urls={v.evidence}/></article>)}</details><details><summary>Corrections & history</summary><p>Other submissions and subsequent disputes remain on the <a href={'/tasks/'+id}>original problem</a>. Accepted results are not silently edited. A new correction must be submitted and reviewed.</p><details><summary>Audit history and machine verification</summary><pre>{r.content_sha256||'See the task API for content hashes.'}</pre><ol>{t.audit_events.map((e:any)=><li key={e.id}>{e.created_at} — {e.action}: {e.summary}</li>)}</ol><a href={'/api/tasks/'+id}>Full JSON record</a></details></details><p><a href={'/tasks/'+id}>Original problem & all the work →</a></p></main>}
