"use client";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import SearchBox, { Criterion } from "../SearchBox/index";
import { useTranslation } from "react-i18next";
import SearchResults, { Column } from "../SearchResults/index";
import EditNote from "@mui/icons-material/EditNote";
import DeleteIcon from "@mui/icons-material/Delete";
import { useRouter } from "next/navigation";
import { deleteDialog, successDialog } from "../Swal/CustomAlerts";
import { SkillResult } from "@/app/api/skill";
import { deleteSkill } from "@/app/api/skill/actions";
import { MAINTAIN_SKILL } from "@/middleware";

interface Props {
  skill: SkillResult[];
  abilities: String[];
}

type SearchQuery = Partial<Omit<SkillResult, "id">>;
type SearchParamNames = keyof SearchQuery;

const SkillSearch: React.FC<Props> = ({ skill, abilities }) => {
  const { t } = useTranslation();
  const [filteredSkill, setFilteredSkill] = useState(skill);
  const router = useRouter();
  // translation
  const skillName = t("Skill Name")
  const skillCode = t("Skill Code")
  const deleteSuccess = t("Delete Success")
  const edit = t("Actions")
  const name = t("name")
  const code = t("code")
  const description = t("description")

  const maintainSkill = abilities.includes(MAINTAIN_SKILL)

  const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
    () => [
      {
        label: skillName,
        paramName: "name",
        type: "text",
      },
      {
        label: skillCode,
        paramName: "code",
        type: "text",
      },
    ],
    [t]
  );

  const onSkillClick = useCallback(
    (skill: SkillResult) => {
      console.log(skill);
      const id = skill.id;
      router.push(`/settings/skill/edit?id=${id}`);
    },
    [router, t]
  );

  const deleteClick = useCallback((skill: SkillResult) => {
    deleteDialog(async () => {
      await deleteSkill(skill.id);
      successDialog(deleteSuccess, t);
    }, t);
  }, []);

  useEffect(() => {
    setFilteredSkill(skill)
  }, [skill]);

  const columns = useMemo<Column<SkillResult>[]>(
    () => [
      {
        name: "action",
        label: edit,
        onClick: onSkillClick,
        buttonIcon: <EditNote />,
        isHidden: !maintainSkill,
      },
      { name: "name", label: name },
      { name: "code", label: code },
      { name: "description", label: description },
      {
        name: "delete",
        label: t("Delete"),
        onClick: deleteClick,
        buttonIcon: <DeleteIcon />,
        color: "error", 
        isHidden: !maintainSkill,
      },
    ],
    [t, onSkillClick, deleteClick]
  );

  return (
    <>
      <SearchBox
        criteria={searchCriteria}
        onSearch={(query) => {
          setFilteredSkill(
            skill.filter(
              (s) =>
                s.name.toLowerCase().includes(query.name.toLowerCase()) &&
                s.code.toLowerCase().includes(query.code.toLowerCase()) 
            )
          );
        }}
      />
      <SearchResults<SkillResult> items={filteredSkill} columns={columns} />
    </>
  );
};

export default SkillSearch;
