import type {
  AgencyOrderData,
  History,
  OrderHistoryData,
  UserAgencyDetailData,
  UserOrderData,
  UserOrderDetailData,
} from "@/pages/finance/lib/type";
import httpClient from "@/shared/config/api/httpClient";
import {
  AGENCY_ORDERS,
  AGENCY_ORDERS_HISTORY,
  PAYMENT_AGENCY,
  USER_ORDERS,
} from "@/shared/config/api/URLs";
import type { AxiosResponse } from "axios";

const getAllOrder = async (params: {
  page: number;
  page_size: number;
  order_status:
    | "pending_payment"
    | "pending_confirmation"
    | "cancelled"
    | "confirmed"
    | "completed"
    | "";
}): Promise<AxiosResponse<UserOrderData>> => {
  const res = await httpClient.get(USER_ORDERS, { params });
  return res;
};

const getAllOrderAgecy = async (params: {
  page: number;
  page_size: number;
}): Promise<AxiosResponse<AgencyOrderData>> => {
  const res = await httpClient.get(AGENCY_ORDERS, { params });
  return res;
};

const getDetailOrder = async (
  id: number,
): Promise<AxiosResponse<UserOrderDetailData>> => {
  const res = await httpClient.get(`${USER_ORDERS}${id}/`);
  return res;
};

const getDetailAgencyOrder = async (
  id: number,
): Promise<AxiosResponse<UserAgencyDetailData>> => {
  const res = await httpClient.get(`${AGENCY_ORDERS}${id}/`);
  return res;
};

const updateDetailOrder = async ({
  id,
  body,
}: {
  id: number;
  body: {
    order_status:
      | "pending_payment"
      | "pending_confirmation"
      | "cancelled"
      | "confirmed"
      | "completed";
  };
}) => {
  const res = await httpClient.patch(`${USER_ORDERS}${id}/`, body);
  return res;
};

const payAgency = async ({
  body,
}: {
  body: {
    travel_agency: number;
    amount: number;
    note?: string;
  };
}) => {
  const res = await httpClient.post(PAYMENT_AGENCY, body);
  return res;
};

const getPaymentHistory = async (params: {
  page_size: number;
  page: number;
}): Promise<AxiosResponse<History>> => {
  const res = await httpClient.get(PAYMENT_AGENCY, { params });
  return res;
};

const getOrderHistory = async (params: {
  page: number;
  page_size: number;
  order: number;
}): Promise<AxiosResponse<OrderHistoryData>> => {
  const res = await httpClient.get(AGENCY_ORDERS_HISTORY, { params });
  return res;
};

const savedOrderHistory = async (body: {
  order: number;
  old_status:
    | "pending_payment"
    | "pending_confirmation"
    | "cancelled"
    | "confirmed"
    | "completed";
  new_status:
    | "pending_payment"
    | "pending_confirmation"
    | "cancelled"
    | "confirmed"
    | "completed";
}) => {
  const res = await httpClient.post(AGENCY_ORDERS_HISTORY, body);
  return res;
};
export {
  getAllOrder,
  getAllOrderAgecy,
  getDetailAgencyOrder,
  getDetailOrder,
  getOrderHistory,
  getPaymentHistory,
  payAgency,
  savedOrderHistory,
  updateDetailOrder,
};
