"use client";

import { ProjectResult } from "@/app/api/projects";
import React, { useMemo, useState } from "react";
import SearchBox, { Criterion } from "../SearchBox";
import { useTranslation } from "react-i18next";
import SearchResults, { Column } from "../SearchResults";
import { TeamProjectResult } from "@/app/api/teamprojects";

interface Props {
  projects: TeamProjectResult[];
}
type SearchQuery = Partial<Omit<TeamProjectResult, "id">>;
type SearchParamNames = keyof SearchQuery;

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

  // If project searching is done on the server-side, then no need for this.
  const [filteredProjects, setFilteredProjects] = useState(projects);

  const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
    () => [
      { label: "Team Code", paramName: "teamCode", type: "text" },
      { label: "Team Name", paramName: "teamName", type: "text" },
    ],
    [t],
  );

  const columns = useMemo<Column<TeamProjectResult>[]>(
    () => [
      { name: "teamCode", label: t("Team Code") },
      { name: "teamName", label: t("Team Name") },
      { name: "NoOfProjects", label: t("No. of Projects") },
    ],
    [t],
  );

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

export default ProgressByClientSearch;
