"use client";

import { ClaimResult } from "@/app/api/claims";
import React, { useCallback, 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";

interface Props {
  claims: ClaimResult[];
}

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

const ClaimSearch: React.FC<Props> = ({ claims }) => {
  const { t } = useTranslation("claims");

  // If claim searching is done on the server-side, then no need for this.
  const [filteredClaims, setFilteredClaims] = useState(claims);

  const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
    () => [
      { label: t("Creation Date"), paramName: "created", type: "dateRange" },
      { label: t("Related Project Name"), paramName: "name", type: "text" },
      {
        label: t("Cost (HKD)"),
        paramName: "cost",
        type: "text",
      },
      {
        label: t("Expense Type"),
        paramName: "type",
        type: "select",
        options: ["Expense", "Petty Cash"],
      },
      {
        label: t("Status"),
        paramName: "status",
        type: "select",
        options: [
          "Not Submitted",
          "Waiting for Approval",
          "Approved",
          "Rejected",
        ],
      },
      {
        label: t("Remarks"),
        paramName: "remarks",
        type: "text",
      },
    ],
    [t],
  );

  const onClaimClick = useCallback((claim: ClaimResult) => {
    console.log(claim);
  }, []);

  const columns = useMemo<Column<ClaimResult>[]>(
    () => [
      // {
      //   name: "action",
      //   label: t("Actions"),
      //   onClick: onClaimClick,
      //   buttonIcon: <EditNote />,
      // },
      { name: "created", label: t("Creation Date") },
      { name: "name", label: t("Related Project Name") },
      { name: "cost", label: t("Cost (HKD)") },
      { name: "type", label: t("Expense Type") },
      { name: "status", label: t("Status") },
      { name: "remarks", label: t("Remarks") },
    ],
    [t, onClaimClick],
  );

  return (
    <>
      <SearchBox
        criteria={searchCriteria}
        onSearch={(query) => {
          console.log(query);
        }}
      />
      <SearchResults<ClaimResult> items={filteredClaims} columns={columns} />
    </>
  );
};

export default ClaimSearch;
