import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "@/shared/services/axiosInstance";
import { EmployeeWithUnitListResponse, UserWithUnitListResponse } from "@/user-management/dto/employees/employees";
import { AxiosResponse } from "axios";

export interface EmployeeDto {
  id: string;
  name: string;
  email: string;
  phone: string;
  // Add other fields as needed
}

export interface CreateEmployeePayload {
  name: string;
  email: string;
  phone: string;
  // Add other fields as required for creation
}

export interface EmployeeQueryParams {
  orderBy?: string;
  take?: number;
  skip?: number;
  order?: string;
}

export interface UpdateEmployeePayload extends Partial<CreateEmployeePayload> {}

export interface EmployeesHeaders {
  delegatorPositionId: string;
  currentProjectId: string;
}

export const employeeService = {
  getEmployee: (id: string): Promise<AxiosResponse<EmployeeDto>> =>
    axiosInstance.get(`/employees/${id}`, {
      headers: withHeaders(),
    }),

  getEmployeesListById: (id: string): Promise<AxiosResponse<EmployeeDto[]>> =>
    axiosInstance.get(`/employees/list/${id}`, {
      headers: withHeaders(),
    }),
  getEmployeesWithUnitById: (
    id: string,
    params: EmployeeQueryParams
  ): Promise<AxiosResponse<EmployeeWithUnitListResponse>> => {
    return axiosInstance.get(`/employees/${id}/with-unit`, {
      headers: withHeaders(),
      params,
    });
  },
  getUsersWithUnitById: (
    id: string,
    params: EmployeeQueryParams
  ): Promise<AxiosResponse<UserWithUnitListResponse>> => {
    return axiosInstance.get(`/employees/${id}/active-with-unit`, {
      headers: withHeaders(),
      params,
    });
  },
  getEmployeesWithOrganizationById: (
    id: string,
    params?: EmployeeQueryParams
  ): Promise<AxiosResponse<EmployeeWithUnitListResponse>> => {
    const mergedParams: EmployeeQueryParams = {
      ...params,
      take: 1000,
    };
    return axiosInstance.get(`/employees/${id}/by-organization`, {
      headers: withHeaders(),
      params: mergedParams,
    });
  },

  createEmployee: (
    payload: CreateEmployeePayload
  ): Promise<AxiosResponse<EmployeeDto>> =>
    axiosInstance.post(`/employees`, payload, {
      headers: withHeaders(),
    }),

  updateEmployee: (
    id: string,
    payload: UpdateEmployeePayload
  ): Promise<AxiosResponse<EmployeeDto>> =>
    axiosInstance.put(`/employees/${id}`, payload, {
      headers: withHeaders(),
    }),

  deleteEmployee: (id: string): Promise<AxiosResponse<void>> =>
    axiosInstance.delete(`/employees/${id}`, {
      headers: withHeaders(),
    }),
};
