"use client";
import { ExclamationIcon, PencilSquareIcon, TrashIcon } from "@/assets/icons";
import ConfirmationModal from "@/components/ui/ConfirmationModal";
import { _TooltipDefaultParams } from "@/constants/tooltip";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import TableSkeleton from "@/components/ui/TableSkeleton";
import { formatPhoneNumber } from "@/utils/shared";
import Link from "next/link";
import { Tooltip } from "react-tooltip";
import { useCompanyListPresenter } from "./useCompanyListPresenter";

interface IProps {}

const CompaniesTable = ({}: IProps) => {
  const {
    isPending,
    pathName,
    columns,
    allCompanies,
    setIsModalOpen,
    router,
    isModalOpen,
    setCurrentCompany,
    onConfirmDelete,
    modalConfig,
  } = useCompanyListPresenter();
  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">
      <button className="mb-5 flex w-fit justify-center self-end rounded-lg bg-primary p-[13px] font-medium text-white hover:bg-opacity-90">
        <Link href={`${pathName}/create`}>Create Company</Link>
      </button>
      <Table>
        <TableHeader>
          <TableRow>
            {columns.map((columns) => (
              <TableHead
                colSpan={100}
                className="text-start font-extrabold uppercase"
                key={columns}
              >
                {columns}
              </TableHead>
            ))}
          </TableRow>
        </TableHeader>
        {isPending && <TableSkeleton column={columns.length} />}
        <TableBody>
          {allCompanies.map((company, index) =>
            !company ? (
              <h6 key={index}>No Data was Found</h6>
            ) : (
              <TableRow
                key={company.id}
                className="odd:bg-gray-2 dark:odd:bg-gray-7"
              >
                <TableCell className="text-start" colSpan={100}>
                  {index + 1}
                </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="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>
            ),
          )}
        </TableBody>
      </Table>
      {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;
