"use client";
import Link from "next/link";
import { CustomersSkeleton } from "./Skeleton";
import useCustomerListPresenter from "./useCustomerListPresenter";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import { PencilSquareIcon, TrashIcon } from "@/assets/icons";
import ConfirmationModal from "@/components/ui/ConfirmationModal";
const CustomersTable = () => {
  const {
    isPending,
    pathName,
    router,
    columns,
    allCustomers,
    modalConfig,
    isModalOpen,
    setIsModalOpen,
    onConfirmDelete,
    setCurrentCustomer,
  } = useCustomerListPresenter();
  if (isPending) return <CustomersSkeleton />;
  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>
        <TableBody>
          {allCustomers.map((customer) => (
            <TableRow key={customer.id}>
              <TableCell colSpan={100}>{customer.full_name}</TableCell>
              <TableCell colSpan={100}>{customer.email}</TableCell>
              <TableCell colSpan={100}>{customer.created_at}</TableCell>
              <TableCell colSpan={100}>{customer.type}</TableCell>
              <TableCell colSpan={100}>{customer.status}</TableCell>
              <TableCell colSpan={100}>
                {customer.companies?.length ? (
                  customer.companies.map((c) => (
                    <span key={c.id}>{c.name}, </span>
                  ))
                ) : (
                  <span>None</span>
                )}
              </TableCell>
              <TableCell className="text-start xl:pr-7.5">
                <div className="flex items-center justify-start gap-x-3.5">
                  <button
                    className="hover:text-red-500"
                    onClick={() => {
                      setCurrentCustomer(customer);
                      setIsModalOpen(true);
                    }}
                  >
                    <span className="sr-only">Delete Company </span>
                    <TrashIcon />
                  </button>
                  <button
                    className="hover:text-primary"
                    onClick={() =>
                      router.push(`${pathName}/edit/${customer.id}`)
                    }
                  >
                    <span className="sr-only">Edit Company </span>
                    <PencilSquareIcon />
                  </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 CustomersTable;
