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;
    employType: string | null;
  };
};

export type TeamLeaves = {
  [memberId: number]: {
    leaveEntries: RecordLeaveInput;
    staffId: string;
    name: string;
    employType: string | null;
  };
};

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

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

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

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

export const fetchTeamMemberLeaves = cache(async () => {
  return serverFetchJson<TeamLeaves>(`${BASE_API_URL}/timesheets/teamLeaves`, {
    next: { tags: [`team_leaves`] },
  });
});
