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 { Recipient } from "@/generated/prisma/client"
import { RecipientService } from "@/services/recipient.service"

export default async function RecipientsPage(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: recipients, totalPages } =
    await RecipientService.findAllPaginated({
      page: currentPage,
      pageSize: 10,
      search,
    })

  return (
    <div className="flex flex-col gap-4">
      <PageHeader
        title="Recipients"
        link="/recipients/new"
        data={recipients}
        search={search}
      />
      {recipients.length === 0 && <div>No recipients found</div>}
      {recipients.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">
                  Username
                </th>
                <th scope="col" className="p-4">
                  Name
                </th>
                <th scope="col" className="p-4">
                  Email
                </th>
                <th scope="col" className="p-4">
                  Phone
                </th>
                <th scope="col" className="p-4">
                  Department
                </th>
                <th scope="col" className="p-4">
                  Actions
                </th>
              </tr>
            </thead>
            <tbody>
              {recipients.map((recipient: Recipient) => (
                <tr key={recipient.id} className="border-b">
                  <td className="p-4">{recipient.username}</td>
                  <td className="p-4">
                    {recipient.firstName + " " + recipient.lastName}
                  </td>
                  <td className="p-4">{recipient.email}</td>
                  <td className="p-4">{recipient.phone}</td>
                  <td className="p-4">{recipient.department}</td>
                  <td className="flex items-center gap-2 p-4">
                    <Link href={`/recipients/${recipient.id}`} passHref>
                      <Button variant="outline" size="icon">
                        <Eye />
                      </Button>
                    </Link>
                    <Link href={`/recipients/${recipient.id}/edit`} passHref>
                      <Button
                        className="btn btn-primary"
                        variant="outline"
                        size="icon"
                      >
                        <Pencil />
                      </Button>
                    </Link>
                  </td>
                </tr>
              ))}
            </tbody>
            <tfoot className="border-t">
              <tr>
                <td colSpan={6} className="p-4 text-center text-sm">
                  <PaginationButtons totalPages={totalPages} />
                </td>
              </tr>
            </tfoot>
          </table>
        </div>
      )}
    </div>
  )
}
