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

export interface data {
  [key: string]: any;
}

export interface StaffGroup {
  id: number;
  name: string;
}
export interface Staff {
  id: number;
  staffId: string;
  name: string;
  // description: string | null;
  currentPosition: StaffGroup;
}
export interface StaffTeamTable {
  id: number;
  staffId: string;
  name: string;
  // description: string | null;
  currentPosition: string;
}

export type IndivStaff = {
  data: IndividualStaff
}

export type projects = {
  id: number,
  code: string,
  name: string,
  status: string
}

// export type InvolvedProject = {
//   records: projects[]
// }

export type IndividualStaff = {
  id: number
  staffId: string
  name: string
  company: data
  team: data
  department: data
  grade: data
  skill: data
  skillset: any
  currentPosition: data
  salary: data
  employType: string
  email: string
  phone1: string
  phone2?: string
  emergContactName: string;
  emergContactPhone: string;
  joinDate: string | number[];
  joinPosition: data;
  departDate?: string;
  departReason?: string;
  remark?: string;
}

export interface StaffResult {
  action: any;
  id: number;
  name: string;
  team: string;
  staffId: string;
  grade: string;
  joinPosition: string;
  currentPosition: string;
  teamId: number;
  staffName: string;
  userId: number;
  companyId: number;
  data: data;
  active: boolean;
}
export interface searchInput {
  staffId: string;
  name: string;
  team: string;
  grade: string;
  currentPosition: string;
}

export interface SalaryEffectiveInfo {
  id: number;
  date: string | Date;
  salaryPoint: number | string;
}

export const preloadTeamLeads = () => {
  fetchTeamLeads();
};

export const fetchTeamLeads = cache(async () => {
  return serverFetchJson<StaffResult[]>(`${BASE_API_URL}/staffs/teamLeads`, {
    next: { tags: ["teamLeads"] },
  });
});

export const preloadStaff = () => {
  fetchStaff();
};

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

export const fetchIndivStaff = cache(async (id: number) => {
    return serverFetchJson<IndivStaff>(`${BASE_API_URL}/staffs/${id}`, {
      next: { tags: ["staffs"] },
    });
});

export const fetchStaffInvolvedProjects = cache(async (id: number) => {
    return serverFetchJson<projects[]>(`${BASE_API_URL}/staffs/staff-projects/${id}`, {
      next: { tags: ["staffs"] },
    });
});

export const fetchStaffWithoutTeam = cache(async () => {
  return serverFetchJson<StaffResult[]>(`${BASE_API_URL}/staffs/noteam`, {
    next: { tags: ["staffs"] },
  });
});

export const fetchStaffSalaryEffectiveInfo = cache(async (id: number) => {
  return serverFetchJson<SalaryEffectiveInfo[]>(`${BASE_API_URL}/salaryEffective?staffId=${id}`, {
    next: { tags: ["staffs"] },
  });
});

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