import Link from 'next/link'
import { requireAdmin } from '@/lib/auth-guard'
import { prisma } from '@/lib/prisma'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from '@/components/ui/table'
import { ProductFormDialog } from '../_components/ProductFormDialog'
import { ProductActions } from './_components/ProductActions'

export default async function AdminProductsPage() {
  await requireAdmin()

  const [products, allUsers] = await Promise.all([
    prisma.product.findMany({
      include: {
        user: { select: { username: true } },
        _count: { select: { members: true, pbis: true } },
      },
      orderBy: { created_at: 'desc' },
    }),
    prisma.user.findMany({
      select: { id: true, username: true },
      orderBy: { username: 'asc' },
    }),
  ])

  return (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <h1 className="text-xl font-semibold text-foreground">Producten</h1>
        <ProductFormDialog
          mode="create"
          allUsers={allUsers}
          trigger={<Button size="sm">Nieuw product</Button>}
        />
      </div>
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead>Naam</TableHead>
            <TableHead>Eigenaar</TableHead>
            <TableHead>Leden</TableHead>
            <TableHead>PBIs</TableHead>
            <TableHead>Status</TableHead>
            <TableHead>Aangemaakt</TableHead>
            <TableHead className="text-right">Acties</TableHead>
          </TableRow>
        </TableHeader>
        <TableBody>
          {products.map(p => (
            <TableRow key={p.id}>
              <TableCell>
                <Link href={`/admin/products/${p.id}`} className="font-medium text-primary hover:underline">
                  {p.name}
                </Link>
              </TableCell>
              <TableCell className="text-muted-foreground">{p.user.username}</TableCell>
              <TableCell>{p._count.members}</TableCell>
              <TableCell>{p._count.pbis}</TableCell>
              <TableCell>
                {p.archived ? (
                  <Badge variant="outline">Gearchiveerd</Badge>
                ) : (
                  <Badge className="bg-status-done/15 text-status-done border-status-done/30">Actief</Badge>
                )}
              </TableCell>
              <TableCell className="text-xs text-muted-foreground">
                {new Date(p.created_at).toLocaleDateString('nl-NL')}
              </TableCell>
              <TableCell>
                <ProductActions product={p} allUsers={allUsers} />
              </TableCell>
            </TableRow>
          ))}
          {products.length === 0 && (
            <TableRow>
              <TableCell colSpan={7} className="text-center text-muted-foreground py-8">
                Geen producten gevonden.
              </TableCell>
            </TableRow>
          )}
        </TableBody>
      </Table>
    </div>
  )
}
