"use client";
import { PencilSquareIcon, TrashIcon } from "@/assets/icons";
import EmptyListWrapper from "@/components/HOC/EmptyListWrapper";
import ConfirmationModal from "@/components/ui/ConfirmationModal";
import ListHeader from "@/components/ui/ListHeader";
import ListWrapper from "@/components/ui/ListWrapper";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
  TableSortProvider,
} 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 CmsListFilters from "./CmsListFilters";
import useCmsTablePresenter from "./useCmsTablePresenter";

const CmsTable = () => {
  const {
    columns,
    columnSortKeys,
    columnDefaultOrder,
    sortBy,
    sortOrder,
    handleSortChange,
    data,
    isPending,
    isModalOpen,
    setIsModalOpen,
    currentCms,
    setCurrentCms,
    onConfirmDelete,
    router,
    pathname,
    filterRegister,
    handleFilterSubmit,
    search,
    watch,
    setFilterValue,
    isMutating,
    clearAllFilters,
    tableSectionRef,
    skeletonTbodyRef,
    dataTbodyRef,
  } = useCmsTablePresenter();
  return (
    <ListWrapper>
      <ListHeader hasFilter={false} />
      <CmsListFilters
        setValue={setFilterValue}
        filterRegister={filterRegister}
        handleFilterSubmit={handleFilterSubmit}
        isMutating={isMutating as number}
        search={search}
        watch={watch}
        onClearAll={clearAllFilters}
      />
      <div ref={tableSectionRef}>
        <TableSortProvider
          sortKey={sortBy}
          sortOrder={sortOrder}
          onSortChange={handleSortChange}
        >
        <Table>
          <TableHeader>
            <TableRow className="border-none uppercase [&>th]:text-start">
              {columns.map((column) => {
                const sortKey = columnSortKeys[column];
                return (
                  <TableHead
                    key={column}
                    colSpan={100}
                    sortable={Boolean(sortKey)}
                    sortKey={sortKey}
                    defaultSortOrder={columnDefaultOrder[column]}
                  >
                    {column}
                  </TableHead>
                );
              })}
            </TableRow>
          </TableHeader>
          {isPending ? (
            <TableSkeleton ref={skeletonTbodyRef} column={columns.length} />
          ) : (
            <TableBody ref={dataTbodyRef}>
              <EmptyListWrapper
                list={data?.data.cms ?? []}
                columns={columns}
                emptyMessage="No Cms were found"
                isLoading={false}
              >
                {data?.data.cms.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: data?.meta?.page,
                        limit: data?.meta?.limit,
                      })}
                    </TableCell>
                    <TableCell className="text-start" colSpan={100}>
                      {item.key}
                    </TableCell>
                    <TableCell className="text-start" colSpan={100}>
                      {unixToDate({ unix: item.created_at, hasTime: true })}
                    </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={() => {
                            setCurrentCms(item);
                            setIsModalOpen(true);
                          }}
                        >
                          <Tooltip {..._TooltipDefaultParams({ id: "delete" })} />
                          <span className="sr-only">Delete Marketing </span>
                          <TrashIcon />
                        </button>
                        <button
                          data-tooltip-id="edit"
                          data-tooltip-content="Edit"
                          data-tooltip-place="top"
                          className="hover:text-primary"
                          onClick={() =>
                            router.push(`${pathname}/edit/${item.id}`)
                          }
                        >
                          <Tooltip {..._TooltipDefaultParams({ id: "edit" })} />
                          <span className="sr-only">Edit Marketing </span>
                          <PencilSquareIcon />
                        </button>
                      </div>
                    </TableCell>
                  </TableRow>
                ))}
              </EmptyListWrapper>
            </TableBody>
          )}
        </Table>
        </TableSortProvider>
      </div>
      {isModalOpen && (
        <ConfirmationModal
          isOpen={isModalOpen}
          title={`Deleting ${currentCms?.key}`}
          message={`Are you sure?`}
          variant={"default"}
          size={"md"}
          confirmText={"Delete"}
          cancelText={"Cancel"}
          onConfirm={() => {
            onConfirmDelete();
          }}
          onCancel={() => setIsModalOpen(false)}
        />
      )}
    </ListWrapper>
  );
};

export default CmsTable;
