import axiosInstance from "@/shared/services/axiosInstance";
import {
  EmployeeByPositionDto,
  EmployeeWithUnitListResponse,
} from "@/user-management/dto/employees/employees";
import { withHeaders } from "./withHeaders";
import { Params } from "./organizationService";

// Re-export Params so it can be imported from this module
export type { Params };

// Fetch employees by unit (old behavior)
export const fetchCurrentEmployees = async (
  params: Params,
  organizationUnitId: string
): Promise<EmployeeWithUnitListResponse> => {
  try {
    const { data } = await axiosInstance.get(
      `/employees/${organizationUnitId}/with-unit`,
      {
        params,
        headers: withHeaders(),
      }
    );
    return data;
  } catch (error: any) {
    throw new Error(
      error?.response?.data?.message ||
        "Failed to fetch current department employees"
    );
  }
};

// ✅ Fetch ALL employees
export const fetchAllEmployees =
  async (): Promise<EmployeeWithUnitListResponse> => {
    try {
      const { data } = await axiosInstance.get(`/employees`, {
        headers: withHeaders(),
      });
      return data;
    } catch (error: any) {
      throw new Error(
        error?.response?.data?.message || "Failed to fetch all employees"
      );
    }
  };

export const fetchEmployeesByPosition = async (
  positionId: string
): Promise<EmployeeByPositionDto[]> => {
  try {
    const { data } = await axiosInstance.get(
      `/positions/current/${positionId}/employees`,
      { headers: withHeaders() }
    );
    return data;
  } catch (error: any) {
    throw new Error(
      error?.response?.data?.message || "Failed to fetch employees for position"
    );
  }
};
// approve employees
export const approvePendingEmployees = async (id:string): Promise<any> =>{
  try {
    return await axiosInstance.patch(`/employees/${id}/accept`)
  }catch(error:any){
    throw new Error(error?.response?.data?.message || "Failed to accept employee")
  }
}
// reject employee
export const rejectPendingEmployees = async (id:string): Promise<any> =>{
  try {
    return await axiosInstance.patch(`/employees/${id}/reject`)
  }catch(error:any){
    throw new Error(error?.response?.data?.message || "Failed to reject employee")
  }
}
//Fetch employees
export const fetchRecordReceivingEmployees = async (
  params: Params,
  organizationUnitId: string
): Promise<EmployeeWithUnitListResponse> => {
  try {
    const { data } = await axiosInstance.get(
      `/employees/others/${organizationUnitId}/with-unit`,
      {
        params,
        headers: withHeaders(),
      }
    );
    return data;
  } catch (error: any) {
    throw new Error(
      error?.response?.data?.message ||
        "Failed to fetch current department employees"
    );
  }
};
