"use client";
import { TrashIcon } from "@/assets/icons";
import EmptyListWrapper from "@/components/HOC/EmptyListWrapper";
import ConfirmationModal from "@/components/ui/ConfirmationModal";
import IsVisible from "@/components/ui/IsVisible";
import ListHeader from "@/components/ui/ListHeader";
import ListWrapper from "@/components/ui/ListWrapper";
import Pagination from "@/components/ui/pagination";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import TableSkeleton from "@/components/ui/TableSkeleton";
import { _TooltipDefaultParams } from "@/constants/tooltip";
import { getPaginatedRowNumber, unixToDate } from "@/utils/shared";
import { Tooltip } from "react-tooltip";
import ContactUsListFilters from "./ContactUsListFilters";
import { useContactUsListPresenter } from "./useContactUsListPresenter";

const ContactUsTable = () => {
  const {
    columns,
    isPending,
    contacts,
    onConfirmDelete,
    setIsModalOpen,
    setCurrentContact,
    isModalOpen,
    currentContact,
    metadata,
    shouldShowPagination,
    filterRegister,
    handleFilterSubmit,
    search,
    watch,
    setFilterValue,
    isMutating,
    clearAllFilters,
    tableSectionRef,
    skeletonTbodyRef,
    dataTbodyRef,
    pagination,
    handlePaginationChange,
  } = useContactUsListPresenter();
  return (
    <ListWrapper>
      <ListHeader hasFilter={false} hasCreate={false} />
      <ContactUsListFilters
        setValue={setFilterValue}
        filterRegister={filterRegister}
        handleFilterSubmit={handleFilterSubmit}
        isMutating={isMutating as number}
        search={search}
        watch={watch}
        onClearAll={clearAllFilters}
      />
      <div ref={tableSectionRef}>
        <Table>
          <TableHeader>
            <TableRow>
              {columns.map((col) => (
                <TableHead
                  colSpan={100}
                  className="text-start uppercase"
                  key={col}
                >
                  {col}
                </TableHead>
              ))}
            </TableRow>
          </TableHeader>
          {isPending ? (
            <TableSkeleton ref={skeletonTbodyRef} column={columns.length} />
          ) : (
            <TableBody ref={dataTbodyRef}>
              <EmptyListWrapper
                list={contacts ?? []}
                columns={columns}
                emptyMessage="No contact us is submitted"
                isLoading={false}
              >
                {contacts?.map((item, index) => (
                  <TableRow
                    key={item?.id ?? index}
                    className="odd:bg-gray-2 dark:odd:bg-gray-7"
                  >
                    <TableCell className="text-start" colSpan={100}>
                      {getPaginatedRowNumber({
                        index,
                        page: metadata?.page,
                        limit: metadata?.limit,
                      })}
                    </TableCell>
                    <TableCell className="text-start" colSpan={100}>
                      {item?.full_name}
                    </TableCell>
                    <TableCell className="text-start" colSpan={100}>
                      {item?.email}
                    </TableCell>
                    <TableCell className="text-start" colSpan={100}>
                      {item?.status}
                    </TableCell>
                    <TableCell className="text-start" colSpan={100}>
                      {item?.phone}
                    </TableCell>
                    <TableCell className="text-start" colSpan={100}>
                      {unixToDate({ unix: item?.created_at })}
                    </TableCell>
                    <TableCell className="text-start xl:pr-7.5">
                      <div className="flex items-center justify-start gap-x-3.5">
                        <button
                          data-tooltip-id="delete"
                          data-tooltip-content="Delete"
                          data-tooltip-place="top"
                          className="hover:text-red-500"
                          onClick={() => {
                            setCurrentContact(item);
                            setIsModalOpen(true);
                          }}
                        >
                          <Tooltip {..._TooltipDefaultParams({ id: "delete" })} />
                          <span className="sr-only">Delete Contact Us </span>
                          <TrashIcon />
                        </button>
                      </div>
                    </TableCell>
                  </TableRow>
                ))}
              </EmptyListWrapper>
            </TableBody>
          )}
        </Table>
      </div>
      {isModalOpen && (
        <ConfirmationModal
          isOpen={isModalOpen}
          title={`Deleting ${currentContact?.full_name}`}
          message={`Are you sure?`}
          variant={"default"}
          size={"md"}
          confirmText={"Delete"}
          cancelText={"Cancel"}
          onConfirm={() => {
            onConfirmDelete();
          }}
          onCancel={() => setIsModalOpen(false)}
        />
      )}
      <IsVisible condition={shouldShowPagination}>
        <Pagination
          currentPage={pagination.currentPage}
          totalPages={pagination.totalPages}
          onPageChange={handlePaginationChange}
        />
      </IsVisible>
    </ListWrapper>
  );
};

export default ContactUsTable;
