"use client";

import { PencilSquareIcon, TrashIcon } from "@/assets/icons";
import ConfirmationModal from "@/components/ui/ConfirmationModal";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import TableSkeleton from "@/components/ui/TableSkeleton";
import Link from "next/link";
import { useAdminsPresenter } from "./useAdminsPresenter";

const AdminsTable = () => {
  const {
    allUsers,
    router,
    isPending,
    onConfirmDelete,
    isModalOpen,
    setIsModalOpen,
    modalConfig,
    setCurrentUser,
    pathName,
  } = useAdminsPresenter();

  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 Admin</Link>
      </button>
      <Table>
        <TableHeader>
          <TableRow className="border-none uppercase [&>th]:text-start">
            <TableHead colSpan={100} className="text-start font-extrabold">
              Full name
            </TableHead>
            <TableHead colSpan={100} className="text-start font-extrabold">
              Email
            </TableHead>
            <TableHead colSpan={100} className="text-start font-extrabold">
              Role
            </TableHead>
            <TableHead colSpan={100} className="text-start font-extrabold">
              Username
            </TableHead>
            <TableHead colSpan={100} className="text-start font-extrabold">
              Actions
            </TableHead>
          </TableRow>
        </TableHeader>
        {isPending && <TableSkeleton column={5} />}
        <TableBody>
          {allUsers.map((item) => {
            return (
              <TableRow
                key={item.id}
                className="odd:bg-gray-2 dark:odd:bg-gray-7"
              >
                <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.role}
                </TableCell>
                <TableCell className="text-start" colSpan={100}>
                  {item.username}
                </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={() => {
                        setCurrentUser(item);
                        setIsModalOpen(true);
                      }}
                    >
                      <span className="sr-only">Delete Admin </span>
                      <TrashIcon />
                    </button>
                    <button
                      className="hover:text-primary"
                      onClick={() => router.push(`/admins/edit/${item.id}`)}
                    >
                      <span className="sr-only">Edit Admin </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 || "warning"}
          size={modalConfig.size || "md"}
          confirmText={modalConfig.confirmText ?? "Confirm"}
          cancelText={modalConfig.cancelText ?? "Cancel"}
          onConfirm={() => {
            onConfirmDelete();
          }}
          onCancel={() => setIsModalOpen(false)}
        />
      )}
    </div>
  );
};

export default AdminsTable;
