import { requireAdmin } from '@/lib/auth-guard'
import { prisma } from '@/lib/prisma'
import type { ClaudeJobStatus } from '@prisma/client'
import { JobsTable, StatusFilter } from '@/components/admin/jobs-table'

const VALID_STATUSES = new Set<ClaudeJobStatus>([
  'QUEUED', 'CLAIMED', 'RUNNING', 'DONE', 'FAILED', 'CANCELLED',
])

export default async function AdminJobsPage({
  searchParams,
}: {
  searchParams: Promise<{ status?: string }>
}) {
  await requireAdmin()

  const params = await searchParams
  const rawStatus = params.status ?? ''
  const statusFilter = VALID_STATUSES.has(rawStatus as ClaudeJobStatus)
    ? (rawStatus as ClaudeJobStatus)
    : undefined

  const jobs = await prisma.claudeJob.findMany({
    where: statusFilter ? { status: statusFilter } : undefined,
    include: {
      user: { select: { username: true } },
      product: { select: { name: true } },
    },
    orderBy: { created_at: 'desc' },
    take: 200,
  })

  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <h1 className="text-xl font-semibold text-foreground">Claude Jobs</h1>
        <StatusFilter current={rawStatus} />
      </div>
      <JobsTable jobs={jobs} />
    </div>
  )
}
