import { Space, TableColumnsType } from "antd";
import { Mark } from "../../../../../types/Item";
import useModalHandler from "../../../../../utils/useModalHandler";
import { ModalEnum } from "../../../../../enums/Model";
import { useObjectToEdit } from "../../../../../zustand/ObjectToEditState";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { Marks } from "../../../../../types/App";
import { findLabelByValue } from "../../../../../utils/findLabelByValue";
import { FaPlus } from "react-icons/fa";
import { useTranslation } from "react-i18next";
import { hasAbility } from "../../../../../utils/hasAbility";
import {
  ABILITIES_ENUM,
  ABILITIES_VALUES_ENUM,
} from "../../../../../enums/abilities";
import { formatNumber } from "../../../../../utils/formatNumber";

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?.MARK_DELETE);
  };

  const can_add_mark = hasAbility(
    ABILITIES_ENUM.MARK,
    ABILITIES_VALUES_ENUM.STORE,
  );

  const [t] = useTranslation();
  const columns: TableColumnsType<Mark> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "end",
      render: (text, record) => record?.id,
    },
    {
      title: t("columns.student_name"),
      dataIndex: "student_name",
      key: "student_name",
      align: "end",
      render: (text, record) => record?.student_name,
    },

    {
      title: t("columns.value"),
      dataIndex: "value",
      key: "value",
      align: "end",
      render: (text, record) => {
        const formattedNumber = formatNumber(record?.value);

        return formattedNumber;
      },
    },

    // {
    //   title: t("columns.type"),
    //   dataIndex: "type",
    //   key: "type",
    //   align: "end",
    //   render: (text, record) => findLabelByValue(Marks, record?.state),
    // },

    {
      title: can_add_mark ? (
        <button
          onClick={() => handel_open_model(ModalEnum?.MARK_ADD)}
          className="add_button"
        >
          {t("practical.add")} {t("models.mark")} <FaPlus />{" "}
        </button>
      ) : (
        ""
      ),
      key: "actions",
      align: "end",
      className: "custom_add_button_column_Mark",
      render: (text, record, index) => {
        const className =
          index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";

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

  return columns;
};
