import axiosInstance from "@/shared/services/axiosInstance";
import Cookies from "js-cookie";
import { withHeaders } from "./withHeaders";
import { DelegatePositionResponse } from "@/record-management/dto/delegation/delegationDto";
import { PaginationParams } from "./userRecordService";
import recordAxiosInstance from "@/shared/services/recordAxiosInstance";

export interface DelegatePositionPayload {
  delegationDto: {
    employeePositionId: string;
    from: Date;
    to: Date;
  };
  signatureId: string;
  stampId: string;
  locale?: "en" | "am";
  cc: { unitId: string; positionId: string; type: "internal" | "external" }[];
}

export const delegatePosition = async (payload: DelegatePositionPayload) => {
  const response = await recordAxiosInstance.post(
    "/records/delegation-record",
    payload,
    {
      headers: withHeaders(),
    },
  );

  return response.data;
};

export interface CanDelegateResponse {
  status: boolean;
}

export const canDelegate = async (): Promise<CanDelegateResponse> => {
  const response = await axiosInstance.get("/employee-positions/can-delegate", {
    headers: withHeaders(),
  });

  return response.data;
};

export const myDelegationDetails = async (
  params?: PaginationParams,
): Promise<DelegatePositionResponse> => {
  const response = await axiosInstance.get(
    "/employee-positions/my-delegated-positions",
    {
      params,
      headers: withHeaders(),
    },
  );

  return response.data;
};
export const myDelegations = async (
  params?: PaginationParams,
): Promise<DelegatePositionResponse> => {
  const response = await axiosInstance.get("/employee-positions/my-delegates", {
    params,
    headers: withHeaders(),
  });

  return response.data;
};

export interface EndDelegationPayload {
  /** Reason / remark recorded against the delegation termination. */
  comment: string;
}

export const deleteDelegations = async (
  delegateId: string,
  payload: EndDelegationPayload,
): Promise<void> => {
  try {
    await axiosInstance.patch(
      `/employee-positions/end-delegation/${delegateId}`,
      payload,
      {
        headers: withHeaders(),
      },
    );
  } catch (error) {
    throw error;
  }
};
export const submitDelegations = async (delegateId: string): Promise<void> => {
  try {
    await axiosInstance.patch(
      `/employee-positions/submit-delegation/${delegateId}`,
      {},
      {
        headers: withHeaders(),
      },
    );
  } catch (error) {
    throw error;
  }
};

export interface PreviewDelegationPayload {
  delegationDto: {
    employeePositionId: string;
    from: Date;
    to: Date;
  };
  signatureId?: string;
  stampId?: string;
  locale?: "en" | "am";
  cc?: { unitId: string; positionId: string; type: "internal" | "external" }[];
}

export const previewDelegationPdf = async (
  payload: PreviewDelegationPayload,
): Promise<Blob> => {
  const response = await recordAxiosInstance.post(
    "/records/preview-delegation-pdf",
    payload,
    {
      headers: withHeaders(),
      responseType: "blob",
    },
  );

  return response.data;
};
