import { Pencil } from "lucide-react"
import Link from "next/link"

import PageHeader from "@/components/common/pageheader"
import PaginationButtons from "@/components/common/pagination"
import { Button } from "@/components/ui/button"
import { CategoryService } from "@/services/category.service"

import DeleteCategoryButton from "./_components/delete.category.button"

export default async function Items(props: {
  searchParams?: Promise<{
    page?: string
    search?: string
  }>
}) {
  const searchParams = await props.searchParams
  const currentPage = searchParams?.page ? parseInt(searchParams.page) : 1
  const search = searchParams?.search || ""
  const { data: categories, totalPages } =
    await CategoryService.findAllWithItemsCountPaginated({
      page: currentPage,
      pageSize: 10,
      search,
    })

  return (
    <div className="flex flex-col gap-4">
      <PageHeader
        title="Categories"
        link="/inventory/categories/new"
        data={categories}
      />
      {categories.length === 0 && currentPage === 1 && (
        <div className="flex flex-col gap-4">
          <div className="flex items-center justify-between gap-4">
            No Categories found.
          </div>
        </div>
      )}
      {categories.length > 0 && (
        <div className="overflow-x-auto">
          <table className="text-muted-foreground w-full text-left text-sm">
            <thead className="border-b">
              <tr>
                <th scope="col" className="p-4">
                  Name
                </th>
                <th scope="col" className="p-4">
                  Items
                </th>
                <th scope="col" className="p-4">
                  Actions
                </th>
              </tr>
            </thead>
            <tbody>
              {categories.map((category) => (
                <tr key={category.id} className="border-b">
                  <td className="p-4">{category.name}</td>
                  <td className="p-4">{category._count.items}</td>
                  <td className="flex items-center gap-2 p-4">
                    <Link
                      href={`/inventory/categories/${category.id}/edit`}
                      passHref
                    >
                      <Button
                        className="btn btn-primary"
                        variant="outline"
                        size="icon"
                      >
                        <Pencil />
                      </Button>
                    </Link>
                    {category._count.items === 0 && (
                      <DeleteCategoryButton categoryId={category.id} />
                    )}
                  </td>
                </tr>
              ))}
            </tbody>
            <tfoot className="border-t">
              <tr>
                <td colSpan={3} className="p-4 text-center text-sm">
                  <PaginationButtons totalPages={totalPages} />
                </td>
              </tr>
            </tfoot>
          </table>
        </div>
      )}
    </div>
  )
}
