import axiosInstance from "./axiosInstance";

export const getCustomerOrders = async () => {
  const res = await axiosInstance.get("/api/order/customer/orders/");
  return res.data;
};

export const submitPartnerForm = async (data: {
  name: string;
  email: string;
  contact_no: string;
  place_for_rental: string;
}) => {
  try {
    const response = await axiosInstance.post(
      "/api/users/potential-partners/",
      data
    );

    return response.data;
  } catch (error: any) {
    throw error.response?.data || error.message;
  }
};

export const updateCustomerProfile = async (data: any) => {
  try {
    const res = await axiosInstance.patch("/api/users/customer/me/", data);
    return res.data;
  } catch (error: any) {
    throw error.response?.data || error.message;
  }
};

export const getOrderById = async (id: string) => {
  try {
    const res = await axiosInstance.get(`/api/order/customer/order/${id}/`);
    return res.data;
  } catch (error: any) {
    throw error.response?.data || error.message;
  }
};


export const registerPartner = (data: {
  phone_number: string;
  first_name: string;
  last_name: string;
  business_name: string;
  business_type: string;
  email?: string;
}) => {
  return axiosInstance.post("/api/users/partner/register/", data);
};

// ⭐ PICKUP POINT
export const ratePickupPoint = (data: any) =>
  axiosInstance.post("/api/ratings/customer/pickup-point/", data);

// ⭐ VEHICLE
export const rateVehicle = (data: any) =>
  axiosInstance.post("/api/ratings/customer/vehicle-submodel/", data);

// ⭐ ACCESSORY
export const rateAccessory = (data: any) =>
  axiosInstance.post("/api/ratings/customer/accessory-submodel/", data);

// ⭐ GET ORDER RATINGS
export const getOrderRatings = (orderId: string) =>
  axiosInstance.get(`/api/ratings/order/${orderId}/`);

export const rateOrder = (data: any) =>
  axiosInstance.post("/api/ratings/customer/order/", data);



// ✅ Cancel / Update Order
export const updateOrder = async (
  orderId: string,
  data: {
    status?: "cancelled";
    from_date?: string;
    to_date?: string;
  }
) => {
  try {
    const res = await axiosInstance.patch(
      `/api/order/customer/order/${orderId}/update/`,
      data
    );

    return res.data;
  } catch (error: any) {
    throw error.response?.data || error.message;
  }
};

// 🔹 Get Wallet List (by user_id)
export const getWallets = async () => {
  try {
    const res = await axiosInstance.get(`/api/wallet/wallets/`);
    return res.data;
  } catch (error: any) {
    throw error.response?.data || error.message;
  }
};

// 🔹 Get Wallet Details
export const getWalletDetails = async (walletId: string) => {
  try {
    const res = await axiosInstance.get(
      `/api/wallet/wallets/${walletId}/`
    );
    return res.data;
  } catch (error: any) {
    throw error.response?.data || error.message;
  }
};

export const createWallet = async () => {
  try {
    const res = await axiosInstance.post(`/api/wallet/wallets/`, {
      is_active: true,
    });
    return res.data;
  } catch (error: any) {
    throw error.response?.data || error.message;
  }
};