import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import { RecordLeaveInput, RecordTimesheetInput } from "./actions";

export interface LeaveType {
  id: number;
  name: string;
}

export type TeamTimeSheets = {
  [memberId: number]: {
    timeEntries: RecordTimesheetInput;
    staffId: string;
    name: string;
  };
};

export const fetchTimesheets = cache(async (username: string) => {
  return serverFetchJson<RecordTimesheetInput>(`${BASE_API_URL}/timesheets`, {
    next: { tags: [`timesheets_${username}`] },
  });
});

export const fetchLeaves = cache(async (username: string) => {
  return serverFetchJson<RecordLeaveInput>(
    `${BASE_API_URL}/timesheets/leaves`,
    {
      next: { tags: [`leaves_${username}`] },
    },
  );
});

export const fetchLeaveTypes = cache(async () => {
  return serverFetchJson<LeaveType[]>(`${BASE_API_URL}/timesheets/leaveTypes`, {
    next: { tags: ["leaveTypes"] },
  });
});

export const fetchTeamMemberTimesheets = cache(async (username: string) => {
  return serverFetchJson<TeamTimeSheets>(
    `${BASE_API_URL}/timesheets/teamTimesheets`,
    {
      next: { tags: [`team_timesheets_${username}`] },
    },
  );
});
