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 { notes } from "../../../../../types/App";
import { findLabelByValue } from "../../../../../utils/findLabelByValue";
import { useTranslation } from "react-i18next";
import { hasAbility } from "../../../../../utils/hasAbility";
import {
  ABILITIES_ENUM,
  ABILITIES_VALUES_ENUM,
} from "../../../../../enums/abilities";

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

  const { set_object_to_edit } = useObjectToEdit((state) => state);
  const [t] = useTranslation();

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

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

  const can_edit_note = hasAbility(
    ABILITIES_ENUM.NOTE,
    ABILITIES_VALUES_ENUM.UPDATE,
  );
  const can_delete_note = hasAbility(
    ABILITIES_ENUM.NOTE,
    ABILITIES_VALUES_ENUM.DELETE,
  );
  const can_add_note = hasAbility(
    ABILITIES_ENUM.NOTE,
    ABILITIES_VALUES_ENUM.STORE,
  );

  const columns: TableColumnsType<any> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "center",
      render: (text, record) => record?.id,
    },
    {
      title: `${t("columns.student_name")}`,
      dataIndex: "content",
      key: "content",
      align: "center",
      render: (text, record) => record?.registration_record?.student?.full_name,
    },

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

    {
      title: t("columns.type"),
      dataIndex: "type",
      key: "type",
      align: "center",
      render: (text, record) => {
        const value = findLabelByValue(notes, record?.type);
        return <>{t(`${value}`)}</>;
      },
    },

    {
      title: can_add_note ? (
        <button
          onClick={() => handel_open_model(ModalEnum?.NOTES_ADD)}
          className="add_button"
        >
          {t("practical.add")} {t("models.note")} <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_note && (
              <Tooltip
                placement="top"
                title={t("practical.edit")}
                color="#E0E0E0"
              >
                <span onClick={() => handleEdit(record)}>
                <MdOutlineEdit
                  size={22}
                  style={{ color: "#A098AE" }}
                  
                />
                </span>
              </Tooltip>
            )}

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

  return columns;
};
