"use client";

import { ProjectResult } from "@/app/api/projects";
import React, { useMemo, useState, useCallback } from "react";
import SearchBox, { Criterion } from "../SearchBox";
import { useTranslation } from "react-i18next";
import SearchResults, { Column } from "../SearchResults";
import { ResourceSummaryResult } from "@/app/api/resourcesummary";
import EditNote from "@mui/icons-material/EditNote";
import { useRouter, useSearchParams } from "next/navigation";
import ProjectResourceSummary from "@/components/ProjectResourceSummary";
import ArticleIcon from '@mui/icons-material/Article';

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


const ProjectResourceSummarySearch: React.FC<Props> = ({ projects }) => {
  const { t } = useTranslation("projects");
  const searchParams = useSearchParams()
  // 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: "Project Code", paramName: "projectCode", type: "text" },
      { label: "Project Name", paramName: "projectName", type: "text" },
      { label: "Client Code", paramName: "clientCode", type: "text" },
      { label: "Client Name", paramName: "clientName", type: "text" },
    ],
    [t],
  );

    const onTaskClick = useCallback((resourceSummaryResult: ResourceSummaryResult) => {
      console.log(resourceSummaryResult)
  }, []);


  const columns = useMemo<Column<ResourceSummaryResult>[]>(
    () => [
      {
        name: "id",
        label: t("View"),
        onClick: onTaskClick,
        buttonIcon: <ArticleIcon />,
      },
      { name: "projectCode", label: t("Project Code") },
      { name: "projectName", label: t("Project Name") },
      { name: "clientCodeAndName", label: t("Client Code And Name") },
    ],
    [onTaskClick, t],
    // [t],
  );

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

export default ProjectResourceSummarySearch;
