import { AppShell } from '@/components/app/app-shell'
import { BackLink } from '@/components/app/back-link'
import { DevTaskBoard } from '@/components/app/dev-task-board'
import { ModeratorAccessCard } from '@/components/app/moderator-access-card'
import { requireSurface } from '@/lib/auth/surface-guard'
import { loadBoard } from '@/lib/data/dev-tasks'

export const dynamic = 'force-dynamic'

/**
 * Developer task board (Nash, 2026-10-21): the one place where work for the
 * developer is posted, picked up and finished. The CTO adds tasks through the
 * board API (docs/DEV-TASK-BOARD.md); the developer, invited to this page
 * with the access card below, moves them along.
 */
export default async function DevTasksPage() {
  const { profile, canManage } = await requireSurface('dev_tasks')
  const board = await loadBoard()
  const role = canManage ? 'manager' : 'developer'
  return (
    <AppShell profile={profile}>
      <main className="mx-auto max-w-7xl space-y-6 px-4 py-8">
        <BackLink />
        <div className="flex flex-wrap items-end justify-between gap-3">
          <div>
            <h1 className="text-2xl font-bold">Developer task board</h1>
            <p className="mt-1 max-w-2xl text-sm text-neutral-500">
              Every task for the developer, with its reason, steps and what &quot;done&quot; means. Open a task to read the brief. Move it to
              Ongoing when you start and to Completed when you finish. Questions go on the task itself.
            </p>
          </div>
          <span className="rounded-full bg-neutral-100 px-3 py-1 text-xs font-semibold text-neutral-600">
            {role === 'manager' ? 'You manage this board' : 'You can move tasks and comment'}
          </span>
        </div>
        {board.ok ? (
          <DevTaskBoard initial={board.value.columns} commentCounts={board.value.commentCounts} role={role} apiBase="/api/admin/dev-tasks" />
        ) : (
          <p className="rounded-xl bg-amber-50 p-4 text-sm text-amber-900">{board.error}</p>
        )}
        <ModeratorAccessCard surface="dev_tasks" canManage={canManage} />
      </main>
    </AppShell>
  )
}
