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

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

export interface UserPositionApproval {
  id: string;
  createdAt?: string;
  employee?: {
    id?: string;
    name?: LocalizedName | string;
    user?: { email?: string };
  };
  position?: {
    id?: string;
    name?: LocalizedName | string;
  };
  unit?: {
    id?: string;
    name?: LocalizedName | string;
  };
}

export interface UserPositionApprovalList {
  count: number;
  items: UserPositionApproval[];
}

export interface UserPositionApprovalParams {
  take?: number;
  skip?: number;
  search?: string;
}

export interface UserPositionDecisionPayload {
  comment: string;
}

/*
 * Provisional API paths. Keep them here so they can be replaced without
 * touching the hook or page once the backend contract is implemented.
 */
const BASE_PATH = "/employee-positions/approvals";

export const fetchUserPositionApprovals = async (
  params: UserPositionApprovalParams,
): Promise<UserPositionApprovalList> => {
  const { data } = await axiosInstance.get<UserPositionApprovalList>(
    `${BASE_PATH}/pending`,
    { params, headers: withHeaders() },
  );
  return data;
};

export const approveUserPosition = async (
  id: string,
  payload: UserPositionDecisionPayload,
): Promise<void> => {
  await axiosInstance.patch(`${BASE_PATH}/${id}/approve`, payload, {
    headers: withHeaders(),
  });
};

export const rejectUserPosition = async (
  id: string,
  payload: UserPositionDecisionPayload,
): Promise<void> => {
  await axiosInstance.patch(`${BASE_PATH}/${id}/reject`, payload, {
    headers: withHeaders(),
  });
};
