import { Eye, 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 { ItemService } from "@/services/item.service"

import DeleteItemButton from "./_components/delete.item.button"

export default async function ItemsPage(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: items, totalPages } = await ItemService.findAllWithAssetCount({
    page: currentPage,
    pageSize: 10,
    search,
  })

  return (
    <div className="flex flex-col gap-4">
      <PageHeader
        title="Items"
        link="/inventory/items/new"
        data={items}
        search={search}
      />
      {items.length === 0 && currentPage === 1 && (
        <div className="flex flex-col gap-4">
          <div className="flex items-center justify-between gap-4">
            No items found.
          </div>
        </div>
      )}
      {items.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">
                  Category
                </th>
                <th scope="col" className="p-4">
                  Assets
                </th>
                <th scope="col" className="p-4">
                  Stock
                </th>
                <th scope="col" className="p-4">
                  Actions
                </th>
              </tr>
            </thead>
            <tbody>
              {items.map((item) => (
                <tr key={item.id} className="border-b">
                  <td className="p-4">{item.name}</td>
                  <td className="p-4">{item.category.name}</td>
                  <td className="p-4">{item._count.assets}</td>
                  <td className="p-4">{item.stock}</td>
                  <td className="flex items-center gap-2 p-4">
                    <Link href={`/inventory/items/${item.id}`} passHref>
                      <Button variant="outline" size="icon">
                        <Eye />
                      </Button>
                    </Link>
                    <Link href={`/inventory/items/${item.id}/edit`} passHref>
                      <Button variant="outline" size="icon">
                        <Pencil />
                      </Button>
                    </Link>
                    {item._count.assets === 0 && item.stock === 0 && (
                      <DeleteItemButton itemId={item.id} />
                    )}
                  </td>
                </tr>
              ))}
            </tbody>
            <tfoot className="border-t">
              <tr>
                <td colSpan={5} className="p-4 text-center text-sm">
                  <PaginationButtons totalPages={totalPages} />
                </td>
              </tr>
            </tfoot>
          </table>
        </div>
      )}
    </div>
  )
}
