"use server";
import { fetchData, fetchDataWithToken } from "../api-fetcher";
import { baseUrlPeople, cookieObject, tokenSecret } from "../basics";
import { PageListOptions, PaginateOnly } from "../schemas/list";

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

async function peopleList(payload: PageListOptions) {
  try {
    const response = await fetchDataWithToken(
      peopleListEndpoint,
      {
        page: payload.page,
        size: payload.size,
        order_field: payload.orderField,
        order_type: payload.orderType,
        query: payload.query,
      },
      "POST",
      false
    );
    return response?.status === 200 ? response.data : null;
  } catch (error) {
    console.error("Error fetching people list:", error);
    return null;
  }
}

export { peopleList };
