import { Space, TableColumnsType, Tooltip } from "antd";

import { ModalEnum } from "../../enums/Model";
import { MdOutlineEdit } from "react-icons/md";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { useObjectToEdit } from "../../zustand/ObjectToEditState";
import { useModalState } from "../../zustand/Modal";
import { useTranslation } from "react-i18next";
import { ABILITIES_ENUM, ABILITIES_VALUES_ENUM } from "../../enums/abilities";
import { hasAbility } from "../../utils/hasAbility";
import { Admin } from "../../types/App";

export const useColumns = () => {
  const { setIsOpen } = useModalState((state) => state);

  const { set_object_to_edit } = useObjectToEdit((state) => state);

  const handelDelete = (record: any) => {
    set_object_to_edit(record);
    setIsOpen(ModalEnum?.ADMIN_DELETE);
  };
  const [t] = useTranslation();

  const can_edit_Admin = hasAbility(
    ABILITIES_ENUM.ADMIN,
    ABILITIES_VALUES_ENUM.UPDATE,
  );
  const can_delete_Admin = hasAbility(
    ABILITIES_ENUM.ADMIN,
    ABILITIES_VALUES_ENUM.DELETE,
  );
  const columns: TableColumnsType<Admin> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "center",
    },
    {
      title: t("columns.username"),
      dataIndex: "username",
      key: "username",
      align: "center",
    },
    {
      title: t("columns.email"),
      dataIndex: "email",
      key: "email",
      align: "center",
    },
    {
      title: t("columns.type"),
      dataIndex: "type",
      key: "type",
      align: "center",
    },
    {
      title: t("columns.status"),
      dataIndex: "status",
      key: "status",
      align: "center",
    },

    {
      title: "",
      key: "actions",
      align: "end",
      width: "14vw",
      render: (text, record, index) => {
        const handleEdit = (record: any) => {
          set_object_to_edit(record);
          setIsOpen(ModalEnum?.ADMIN_EDIT);
        };

        const className =
          index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";

        return (
          <Space size="middle" className={className}>
            {can_edit_Admin && (
              <Tooltip
                placement="top"
                title={t("practical.edit")}
                color="#E0E0E0"
              >
                <span onClick={() => handleEdit(record)}>
                <MdOutlineEdit
                  size={22}
                  style={{ color: "#A098AE" }}
                  
                />
                </span>
              </Tooltip>
            )}

            {can_delete_Admin && (
              <RiDeleteBin6Fill
                onClick={() => handelDelete(record)}
                size={22}
                style={{ color: "#C11313" }}
              />
            )}
          </Space>
        );
      },
    },
  ];

  return columns;
};
