import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "@/shared/services/axiosInstance";

export type EmployeePositionChangeRequestStatus =
  | "PENDING"
  | "APPROVED"
  | "REJECTED"
  | string;

export interface LocalizedName {
  am?: string;
  en?: string;
}

export interface EmployeePositionChangeRequest {
  id: string;
  employeeId?: string;
  fromPositionId?: string;
  toPositionId?: string;
  status?: EmployeePositionChangeRequestStatus;
  reason?: string;
  comment?: string;
  createdAt?: string;
  updatedAt?: string;
  employee?: {
    id?: string;
    name?: LocalizedName | string;
    user?: {
      email?: string;
      username?: string;
    };
  };
  fromPosition?: {
    id?: string;
    name?: LocalizedName | string;
  };
  toPosition?: {
    id?: string;
    name?: LocalizedName | string;
  };
  requestedBy?: {
    id?: string;
    name?: LocalizedName | string;
  };
}

export interface EmployeePositionChangeRequestListResponse {
  count: number;
  items: EmployeePositionChangeRequest[];
}

export interface EmployeePositionChangeRequestParams {
  take?: number;
  skip?: number;
  search?: string;
  status?: EmployeePositionChangeRequestStatus;
  employeeId?: string;
  fromPositionId?: string;
  toPositionId?: string;
  orderBy?: string;
}

export interface TransferEmployeePositionPayload {
  employeeId: string;
  fromPositionId?: string;
  toPositionId: string;
  reason?: string;
  comment?: string;
}

export interface EmployeePositionChangeDecisionPayload {
  comment?: string;
}

export interface BulkApproveEmployeePositionChangePayload {
  ids: string[];
  comment?: string;
}

const BASE_PATH = "/employee-position-change-requests";

export const employeePositionChangeRequestService = {
  list: async (
    params?: EmployeePositionChangeRequestParams,
  ): Promise<EmployeePositionChangeRequestListResponse> => {
    const { data } =
      await axiosInstance.get<EmployeePositionChangeRequestListResponse>(
        BASE_PATH,
        {
          params,
          headers: withHeaders(),
        },
      );
    return data;
  },

  getById: async (id: string): Promise<EmployeePositionChangeRequest> => {
    const { data } = await axiosInstance.get<EmployeePositionChangeRequest>(
      `${BASE_PATH}/${id}`,
      {
        headers: withHeaders(),
      },
    );
    return data;
  },

  transfer: async (
    payload: TransferEmployeePositionPayload,
  ): Promise<EmployeePositionChangeRequest> => {
    const { data } = await axiosInstance.post<EmployeePositionChangeRequest>(
      `${BASE_PATH}/transfer`,
      payload,
      {
        headers: withHeaders(),
      },
    );
    return data;
  },

  bulkApprove: async (
    payload: BulkApproveEmployeePositionChangePayload,
  ): Promise<EmployeePositionChangeRequest[]> => {
    const { data } = await axiosInstance.patch<EmployeePositionChangeRequest[]>(
      `${BASE_PATH}/bulk/approve`,
      payload,
      {
        headers: withHeaders(),
      },
    );
    return data;
  },

  approve: async (
    id: string,
    payload?: EmployeePositionChangeDecisionPayload,
  ): Promise<EmployeePositionChangeRequest> => {
    const { data } = await axiosInstance.patch<EmployeePositionChangeRequest>(
      `${BASE_PATH}/${id}/approve`,
      payload ?? {},
      {
        headers: withHeaders(),
      },
    );
    return data;
  },

  reject: async (
    id: string,
    payload?: EmployeePositionChangeDecisionPayload,
  ): Promise<EmployeePositionChangeRequest> => {
    const { data } = await axiosInstance.patch<EmployeePositionChangeRequest>(
      `${BASE_PATH}/${id}/reject`,
      payload ?? {},
      {
        headers: withHeaders(),
      },
    );
    return data;
  },
};
