"use server";
import { fetchDataWithToken, updateDataWithToken } from "../api-fetcher";
import {
  baseUrl,
  FilterList,
  FilterListInterface,
  defaultFilterList,
} from "../basics";

const peopleListEndpoint = `${baseUrl}/people/list`;
const peopleCreateEndpoint = `${baseUrl}/people/create`;
const peopleUpdateEndpoint = `${baseUrl}/people/update`;

interface PeopleUpdateInterface {
  uuid: string;
  payload: any;
}

async function retrievePeopleList(payload: FilterListInterface) {
  const feedObject = new FilterList(payload).filter();
  const tokenResponse: any = await fetchDataWithToken(
    peopleListEndpoint,
    feedObject,
    "POST",
    false
  );
  return tokenResponse;
}

async function updatePeople(payload: any) {
  const { uu_id: extractedField, ...payloadBody } = payload;
  const tokenResponse: any = await updateDataWithToken(
    peopleUpdateEndpoint,
    extractedField,
    payloadBody,
    "POST",
    false
  );
  return tokenResponse;
}

async function createPeople(payload: any) {
  const tokenResponse: any = await fetchDataWithToken(
    peopleCreateEndpoint,
    payload,
    "POST",
    false
  );
  return tokenResponse;
}

export { retrievePeopleList, updatePeople, createPeople };
