import { TableColumnsType } from "antd";
import { useModalState } from "../../../zustand/Modal";
import { ModalEnum } from "../../../enums/Model";
import { FaEye } from "react-icons/fa";
import ColumnsImage from "../../../Components/Columns/ColumnsImage";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";
import { useNavigate } from "react-router-dom";
import { useTranslation } from "react-i18next";
import ActionButtons from "../../../Components/Table/ActionButtons";
import { canDeleteSubject, canEditSubject } from "../../../utils/hasAbilityFn";
import { Subject } from "../../../types/Subject";
import { ABILITIES_ENUM } from "../../../enums/abilities";

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

  const handelDelete = (record: Subject) => {
    setObjectToEdit(record);
    setIsOpen(ModalEnum?.SUBJECT_DELETE);
  };
  const handleEdit = (record: Subject) => {
    setObjectToEdit(record);
    setIsOpen(ModalEnum?.SUBJECT_EDIT);
  };

  const [t] = useTranslation();
  const columns: TableColumnsType<Subject> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "start",
    },
    {
      title: t("columns.subject_name"),
      dataIndex: "name",
      key: "name",
      align: "center",
    },
    {
      title: t("columns.icon"),
      dataIndex: "icon",
      key: "icon",
      align: "center",
      render: (row: Subject, record) => {
        let str = record.icon;
        return <ColumnsImage src={str} />;
      },
    },

    {
      title: t("columns.card"),
      key: "name",
      align: "center",
      className: "SeeMoreEyeColumn",
      render: (text, record) => {
        const handelnavigate = () => {
          navigate(`${ABILITIES_ENUM?.SUBJECT}/${record?.id}`);
        };
        return (
          <div onClick={() => handelnavigate()} className="SeeMoreEye">
            <FaEye />
          </div>
        );
      },
    },

    {
      title: "",
      key: "actions",
      align: "end",
      width: "25vw",
      render: (_text, record, index) => {
        return (
          <ActionButtons
            canDelete={canEditSubject}
            canEdit={canDeleteSubject}
            index={index}
            onDelete={() => handelDelete(record)}
            onEdit={() => handleEdit(record)}
          />
        );
      },
    },
  ];

  return columns;
};
