"use client";
import {
  ExclamationIcon,
  EyeIcon,
  PencilSquareIcon,
  TrashIcon,
} from "@/assets/icons";
import EmptyListWrapper from "@/components/HOC/EmptyListWrapper";
import ConfirmationModal from "@/components/ui/ConfirmationModal";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import TableSkeleton from "@/components/ui/TableSkeleton";
import { _TooltipDefaultParams } from "@/constants/tooltip";
import { formatPhoneNumber, getPaginatedRowNumber } from "@/utils/shared";
import { Tooltip } from "react-tooltip";
import { useCompanyListPresenter } from "./useCompanyListPresenter";
import ListHeader from "@/components/ui/ListHeader";
import Modal from "@/components/ui/modal";
import CompanyListFilters from "./CompanyListFilters";

const CompaniesTable = () => {
  const {
    isPending,
    pathName,
    columns,
    allCompanies,
    setIsModalOpen,
    router,
    isModalOpen,
    setCurrentCompany,
    onConfirmDelete,
    modalConfig,
    isFilterModalOpen,
    setIsFilterModalOpen,
    filterRegister,
    handleFilterSubmit,
    search,
    watch,
    setFilterValue,
    isMutating,
  } = useCompanyListPresenter();
  const companies = allCompanies.filter(
    (c): c is NonNullable<typeof c> => c != null,
  );
  return (
    <div className="flex flex-col rounded-[10px] bg-white px-7.5 pb-4 pt-7.5 shadow-1 dark:bg-gray-dark dark:shadow-card">
      <ListHeader
        onFilter={() => setIsFilterModalOpen(true)}
        createButtonText="Create Company"
      />
      <Table>
        <TableHeader>
          <TableRow>
            {columns.map((columns) => (
              <TableHead
                colSpan={100}
                className="text-start uppercase"
                key={columns}
              >
                {columns}
              </TableHead>
            ))}
          </TableRow>
        </TableHeader>
        {isPending && <TableSkeleton column={columns.length} />}
        <TableBody>
          <EmptyListWrapper
            list={companies}
            columns={columns}
            emptyMessage="No companies were found"
          >
            {companies.map((company, index) => (
              <TableRow
                key={company.id}
                className="odd:bg-gray-2 dark:odd:bg-gray-7"
              >
                <TableCell className="text-start" colSpan={100}>
                  {getPaginatedRowNumber({ index })}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {company.name}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {company.email}
                </TableCell>
                <TableCell className="w-full text-start" colSpan={100}>
                  {formatPhoneNumber(company.phone)}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {company.address.country}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {company.address.state}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {company.language}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {company.currency}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {company.employee_count}
                </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={() => {
                        setCurrentCompany(company);
                        setIsModalOpen(true);
                      }}
                    >
                      <Tooltip {..._TooltipDefaultParams({ id: "delete" })} />
                      <span className="sr-only">Delete Company </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/${company.id}`)
                      }
                    >
                      <Tooltip {..._TooltipDefaultParams({ id: "edit" })} />
                      <span className="sr-only">Edit Company </span>
                      <PencilSquareIcon />
                    </button>
                    <button
                      data-tooltip-id="details"
                      data-tooltip-content="Details"
                      data-tooltip-place="top"
                      onClick={() => {
                        router.push(`${pathName}/details/${company.id}`);
                      }}
                    >
                      <Tooltip {..._TooltipDefaultParams({ id: "details" })} />
                      <EyeIcon />
                    </button>
                    <button
                      data-tooltip-id="feedback"
                      data-tooltip-content="Feedback"
                      data-tooltip-place="top"
                      className="text-gray-40 rounded-full bg-gray-dark bg-opacity-5 hover:text-green-dark dark:bg-gray dark:bg-opacity-5"
                      onClick={() => {
                        router.push(`${pathName}/feedback/${company.id}`);
                      }}
                    >
                      <Tooltip {..._TooltipDefaultParams({ id: "feedback" })} />
                      <ExclamationIcon />
                    </button>
                  </div>
                </TableCell>
              </TableRow>
            ))}
          </EmptyListWrapper>
        </TableBody>
      </Table>
      <Modal
        isOpen={isFilterModalOpen}
        onClose={() => setIsFilterModalOpen(false)}
        classNames="w-full xl:w-1/2"
        showButtons={false}
      >
        <CompanyListFilters
          setValue={setFilterValue}
          filterRegister={filterRegister}
          handleFilterSubmit={handleFilterSubmit}
          isMutating={isMutating as number}
          search={search}
          setIsFilterModalOpen={setIsFilterModalOpen}
          watch={watch}
        />
      </Modal>
      {isModalOpen && (
        <ConfirmationModal
          isOpen={isModalOpen}
          title={modalConfig.title || "Confirm"}
          message={modalConfig.message || "Are you sure?"}
          variant={modalConfig.variant || "default"}
          size={modalConfig.size || "md"}
          confirmText={modalConfig.confirmText ?? "Confirm"}
          cancelText={modalConfig.cancelText ?? "Cancel"}
          onConfirm={() => {
            onConfirmDelete();
          }}
          onCancel={() => setIsModalOpen(false)}
        />
      )}
    </div>
  );
};

export default CompaniesTable;
