"use server";
import { cache } from "react";
import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";

export interface comboProp {
  id: any;
  label: string;
}

export interface teamCombo {
  records: comboProp[];
}

export interface weeklyTeamTotalManhoursSpentResult {
  weeklyActualTeamTotalManhoursSpent: any[];
  weeklyPlannedTeamTotalManhoursSpent: any[];
}

export interface monthlyTeamTotalManhoursSpentResult {
  monthlyActualTeamTotalManhoursSpent: any[];
  monthlyPlannedTeamTotalManhoursSpent: any[];
}

export interface totalManhourByGradeResult {
  staffGradeTotalManhours: any[];
  staffGradeTotalPlannedManhours: any[];
}

export interface weeklyUnsubmitResult {
  id: number;
  name: string;
  teamId: number;
  UnsubmittedCount: number;
}

export interface records {
  id: number;
  name: string;
  label: string;
  // team: Team[];
}

export interface IndividualStaffManhoursDaily {
  individualStaffManhoursSpentByDay: any[];
  individualStaffTotalManhoursSpentByDay: any[];
  individualStaffTotalLeaveHoursByDay: any[];
}

export interface IndividualStaffManhoursWeekly {
  individualStaffManhoursSpentWeekly: any[];
  individualStaffTotalManhoursSpentWeekly: any[];
  individualStaffTotalLeaveHoursWeekly: any[];
}

export interface IndividualStaffManhoursMonthly {
  individualStaffManhoursSpentByMonth: any[];
  individualStaffTotalManhoursSpentByMonth: any[];
  individualStaffTotalLeaveHoursByMonth: any[];
}

export const fetchTeamCombo = cache(async () => {
  return serverFetchJson<teamCombo>(`${BASE_API_URL}/team/combo`);
});

export const fetchweeklyTeamTotalManhours = cache(async (teamId: number, startdate:string) => {
  return serverFetchJson<weeklyTeamTotalManhoursSpentResult[]>(`${BASE_API_URL}/dashboard/searchWeeklyActualTeamTotalManhoursSpent?teamId=${teamId}&startdate=${startdate}`);
});

export const fetchmonthlyTeamTotalManhours = cache(async (teamId: number, startdate:string, enddate:string) => {
  return serverFetchJson<monthlyTeamTotalManhoursSpentResult[]>(`${BASE_API_URL}/dashboard/searchMonthlyActualTeamTotalManhoursSpent?teamId=${teamId}&startdate=${startdate}&enddate=${enddate}`);
});

export const fetchTotalManhoursByGrade = cache(async (startdate:string, enddate:string, teamId:number) => {
  return serverFetchJson<totalManhourByGradeResult[]>(`${BASE_API_URL}/dashboard/searchTotalManhoursSpentByStaffGrade?startdate=${startdate}&enddate=${enddate}&teamId=${teamId}`);
});

export const fetchWeeklyUnsubmit = cache(async (teamId:number,startdate:string, publicHolidayList:string) => {
  return serverFetchJson<weeklyUnsubmitResult[]>(`${BASE_API_URL}/dashboard/searchWeeklyUnsubmittedTimeSheet?teamId=${teamId}&startdate=${startdate}&publicHolidayList=${publicHolidayList}`);
});
export const fetchMonthlyUnsubmit = cache(async (teamId:number,startdate:string, enddate:string, publicHolidayList:string) => {
  return serverFetchJson<weeklyUnsubmitResult[]>(`${BASE_API_URL}/dashboard/searchMonthlyUnsubmittedTimeSheet?teamId=${teamId}&startdate=${startdate}&enddate=${enddate}&publicHolidayList=${publicHolidayList}`);
});

export const fetchStaffCombo = cache(async () => {
  return serverFetchJson<records[]>(`${BASE_API_URL}/dashboard/searchStaffCombo`, {
    next: { tags: ["staffs"] },
  });
});

export const fetchDailyIndividualStaffManhours = cache(async (staffId:number,startdate:string) => {
  return serverFetchJson<IndividualStaffManhoursDaily[]>(`${BASE_API_URL}/dashboard/searchTotalManhoursSpentByStaffDaily?staffId=${staffId}&startdate=${startdate}`);
});

export const fetchWeeklyIndividualStaffManhours = cache(async (staffId:number,startdate:string, enddate:string) => {
  return serverFetchJson<IndividualStaffManhoursWeekly[]>(`${BASE_API_URL}/dashboard/searchTotalManhoursSpentByStaffWeekly?staffId=${staffId}&startdate=${startdate}&enddate=${enddate}`);
});

export const fetchMonthlyIndividualStaffManhours = cache(async (staffId:number,startdate:string) => {
  return serverFetchJson<IndividualStaffManhoursMonthly[]>(`${BASE_API_URL}/dashboard/searchTotalManhoursSpentByStaffByMonth?staffId=${staffId}&startdate=${startdate}`);
});