"use client";
import { StaffResult } from "@/app/api/staff";
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 { deleteStaff } from "@/app/api/staff/actions";
import { useRouter } from "next/navigation";
import { deleteDialog, successDialog } from "../Swal/CustomAlerts";
import Person from '@mui/icons-material/Person';

interface Props {
  staff: StaffResult[];
}

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

const StaffSearch: React.FC<Props> = ({ staff }) => {
  const { t } = useTranslation();
  const [filteredStaff, setFilteredStaff] = useState(staff);
  const router = useRouter();

  const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
    () => [
      {
        label: t("Team"),
        paramName: "team",
        type: "select",
        options: ["1", "2"],
      },
      {
        label: t("Staff Name"),
        paramName: "name",
        type: "text",
      },
      {
        label: t("Staff ID"),
        paramName: "staffId",
        type: "text",
      },
      {
        label: t("Grade"),
        paramName: "grade",
        type: "select",
        options: ["pos1", "CEO"],
      },
      {
        label: t("Current Position"),
        paramName: "currentPosition",
        type: "select",
        options: ["pos1", "CEO"],
      },
    ],
    [t]
  );

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

  const onUserClick = useCallback(
    (staff: StaffResult) => {
      console.log(staff);
      router.push(`/settings/staff/user?id=${staff.id}`);
    },
    [router, t]
  );

  const deleteClick = useCallback((staff: StaffResult) => {
    deleteDialog(async () => {
      await deleteStaff(staff.id);
      successDialog(t("Delete Success"), t);
      setFilteredStaff((prev) => prev.filter((obj) => obj.id !== staff.id));
    }, t);
  }, []);

  const columns = useMemo<Column<StaffResult>[]>(
    () => [
      {
        name: "action",
        label: t("Actions"),
        onClick: onStaffClick,
        buttonIcon: <EditNote />,
      },
      {
        name: "id",
        label: t("Actions"),
        onClick: onUserClick,
        buttonIcon: <Person />,
      },
      { name: "team", label: t("Team") },
      { name: "name", label: t("Staff Name") },
      { name: "staffId", label: t("Staff ID") },
      { name: "grade", label: t("Grade") },
      { name: "currentPosition", label: t("Current Position") },
      {
        name: "action",
        label: t("Actions"),
        onClick: deleteClick,
        buttonIcon: <DeleteIcon />,
        color: "error",
      },
    ],
    [t, onStaffClick, deleteClick]
  );

  return (
    <>
      <SearchBox
        criteria={searchCriteria}
        onSearch={(query) => {
          setFilteredStaff(
            staff.filter(
              (s) =>
                s.staffId.toLowerCase().includes(query.staffId.toLowerCase()) &&
                s.name.toLowerCase().includes(query.name.toLowerCase())
              // (query.team === "All" || s.team === query.team) &&
              // (query.category === "All" || s.category === query.category) &&
              // (query.team === "All" || s.team === query.team),
            )
          );
        }}
      />
      <SearchResults<StaffResult> items={filteredStaff} columns={columns} />
    </>
  );
};

export default StaffSearch;
