"use server";
import { ServerFetchError, serverFetchJson, serverFetchWithNoContent } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { StaffResult, data } from ".";
import { cache } from "react";
import { Team, staff } from "../team/actions";
import { revalidateTag } from "next/cache";
export interface CreateCustomInputs {
    // Project details
    projectCode: string;
    projectName: string;
  
    // Miscellaneous
    expectedProjectFee: string;
  }
export type teamHistory = {
  id: number,
  team: string | number,
  from: Date | string,
}

export type gradeHistory = {
  id: number,
  grade: string | number,
  from: Date | string,
}

export type positionHistory = {
  id: number,
  position: string | number,
  from: Date | string,
}
export interface CreateStaffInputs {
    id?: number
    name: string;
    staffId: string;
    companyId: number;
    salaryId: number;
    skillSetId?: number[];
    joinDate?: string | null;
    currentPositionId: number;
    joinPositionId?: number | null;
    gradeId: number;
    teamId?: number
    departmentId?: number;
    phone1: string;
    phone2?: string;
    email: string;
    emergContactName?: string;
    emergContactPhone?: string;
    employType: string;
    departDate?: string | null;
    departReason?: string;
    remark?: string;
    salaryEffectiveInfo?: any;
    teamHistory: teamHistory[];
    delTeamHistory: number[];
    gradeHistory: gradeHistory[];
    delGradeHistory: number[];
    positionHistory: positionHistory[];
    delPositionHistory: number[];
    // new modal
    salary: salary[];
    team: team[];
    grade: grade[];
    position: position[];
  }
  
  export interface records {
    id: number;
    name: string;
    // team: Team[];
  }
  export type DataLog = 
    | (salary & {id: number; type: "salary"})
    | (team & {id: number; type: "team"})
    | (grade & {id: number; type: "grade"})
    | (position & {id: number; type: "position"})

  export type salary = {
    from: string;
    to: string;
    salaryPoint: number;
  }

  export type team = {
    from: string;
    to: string;
    teamId: number;
  }

  export type grade = {
    from: string;
    to: string;
    gradeId: number;
  }

  export type position = {
    from: string;
    to: string;
    positionId: number;
  }

  export interface salaryEffectiveInfo {
    id: number;
    date: string;
    salaryPoint: number;
  }

  export interface NewStaffResponse {
    id?: number | null;
    name?: string | null;
    staffId?: string | null;
    message?: string | null;
    errorPosition?: string | null;
  }

export const saveStaff = async (data: CreateStaffInputs) => {
  // try {
    const newStaffList = await serverFetchJson<NewStaffResponse>(`${BASE_API_URL}/staffs/save`, {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    });
    console.log(newStaffList)
    revalidateTag("staffs");
    return newStaffList
  };

  
export const testing = async (data: CreateStaffInputs) => {
  return serverFetchJson(`${BASE_API_URL}/staffs/testing`, {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    });
  };
  
export const deleteStaff = async (id: number) => {
  const newStaffList = await serverFetchWithNoContent(`${BASE_API_URL}/staffs/delete/${id}`, {
    method: "DELETE",
    // body: JSON.stringify(data),
    headers: { "Content-Type": "application/json" },
  });
  revalidateTag("staffs");
  return newStaffList
  };

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

// export const preloadStaffEdit = (id: number) => {
// fetchStaffEdit(id);
// };

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

