import { TableColumnsType } from "antd";
import { Question } from "../../types/Item";
import { FaPlus } from "react-icons/fa";
import { ModalEnum } from "../../enums/Model";
import { useObjectToEdit } from "../../zustand/ObjectToEditState";
import { useTranslation } from "react-i18next";
import { ABILITIES_ENUM } from "../../enums/abilities";
import { useNavigate } from "react-router-dom";
import { useModalState } from "../../zustand/Modal";
import {
  canAddQuestion,
  canDeleteQuestion,
  canEditQuestion,
} from "../../utils/hasAbilityFn";
import ActionButtons from "../../Components/Table/ActionButtons";

export const useColumns = () => {
  const { setObjectToEdit } = useObjectToEdit((state) => state);
  const navigate = useNavigate();
  const { setIsOpen } = useModalState((state) => state);

  const handelAdd = () => {
    setObjectToEdit({});
    navigate(`${ABILITIES_ENUM?.QUESTION}/add`);
  };

  const handelDelete = (data: any) => {
    setObjectToEdit(data);
    setIsOpen(ModalEnum?.QUESTION_DELETE);
  };

  const handleEdit = (record: any) => {
    setObjectToEdit(record);
    navigate(`${ABILITIES_ENUM?.QUESTION}/${record?.id}`);
  };
  const [t] = useTranslation();

  const columns: TableColumnsType<Question> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "center",
      render: (text, record) => record?.id,
    },
    {
      title: `${t("columns.content")}`,
      dataIndex: "name",
      key: "name",
      align: "center",
      render: (text, record) => record?.content,
      ellipsis: true,
    },
    {
      title: t("columns.isBase"),
      dataIndex: "isBase",
      key: "isBase",
      align: "center",
      render: (text, record) =>
        record?.isBase ? t("practical.yes") : t("practical.no"),
    },
    {
      title: t("columns.canAnswersBeShuffled"),
      dataIndex: "canAnswersBeShuffled",
      key: "canAnswersBeShuffled",
      align: "center",
      render: (text, record) =>
        record?.canAnswersBeShuffled ? t("practical.yes") : t("practical.no"),
    },
   
    {
      title: canAddQuestion ? (
        <button onClick={() => handelAdd()} className="add_button">
          {t("practical.add")} {t("models.Question")} <FaPlus />
        </button>
      ) : (
        ""
      ),
      key: "actions",
      align: "end",
      width: "25vw",
      render: (_text, record, index) => {
        return (
          <ActionButtons
            canDelete={canDeleteQuestion}
            canEdit={canEditQuestion}
            index={index}
            onDelete={() => handelDelete(record)}
            onEdit={() => handleEdit(record)}
          />
        );
      },
    },
  ];

  return columns;
};
