import { Space, TableColumnsType, Tooltip } from "antd";
import { FaPlus } from "react-icons/fa";
import useModalHandler from "../../../../../utils/useModalHandler";
import { ModalEnum } from "../../../../../enums/Model";
import { useObjectToEdit } from "../../../../../zustand/ObjectToEditState";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { MdOutlineEdit } from "react-icons/md";
import { useTranslation } from "react-i18next";
import {
  ABILITIES_ENUM,
  ABILITIES_VALUES_ENUM,
} from "../../../../../enums/abilities";
import { hasAbility } from "../../../../../utils/hasAbility";

export const useColumns = () => {
  const { handel_open_model } = useModalHandler();

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

  const handelDelete = (data: any) => {
    set_object_to_edit(data);
    handel_open_model(ModalEnum?.HOME_WORK_DELETE);
  };

  const handleEdit = (record: any) => {
    set_object_to_edit(record);
    handel_open_model(ModalEnum?.HOME_WORK_EDIT);
  };

  const [t] = useTranslation();

  const can_edit_home_work = hasAbility(
    ABILITIES_ENUM.HOMEWORK,
    ABILITIES_VALUES_ENUM.UPDATE,
  );
  const can_delete_home_work = hasAbility(
    ABILITIES_ENUM.HOMEWORK,
    ABILITIES_VALUES_ENUM.DELETE,
  );
  const can_add_home_work = hasAbility(
    ABILITIES_ENUM.HOMEWORK,
    ABILITIES_VALUES_ENUM.STORE,
  );

  const columns: TableColumnsType<any> = [
    {
      title: t("columns.id"),
      dataIndex: "user_id",
      key: "user_id",
      align: "center",
      render: (text, record) => record?.id,
    },
    {
      title: t("columns.title"),
      dataIndex: "title",
      key: "title",
      align: "center",
      render: (text, record) => record?.title,
    },

    {
      title: t("columns.content"),
      dataIndex: "content",
      key: "content",
      align: "center",
      render: (text, record) => record?.content,
    },

    {
      title: t("columns.assigning_date"),
      dataIndex: "assigning_date",
      key: "assigning_date",
      align: "center",
      render: (text, record) => record?.assigning_date,
    },

    {
      title: t("columns.due_date"),
      dataIndex: "due_date",
      key: "due_date",
      align: "center",
      render: (text, record) => record?.due_date,
    },
    {
      title: can_add_home_work ? (
        <button
          onClick={() => handel_open_model(ModalEnum?.HOME_WORK_ADD)}
          className="add_button"
        >
          {t("practical.add")} {t("models.homework")} <FaPlus />
        </button>
      ) : (
        ""
      ),
      key: "actions",
      align: "end",
      className: "custom_add_button_column",
      render: (text, record, index) => {
        const className =
          index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";

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

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

  return columns;
};
