import { api } from "@/config/axios";
import {
  ShipmentMethodsResponse,
  PaymentMethodsResponse,
  CreateOrderResponse,
  OrderDetailResponse,
} from "../types/Types";

export const getShipmentMethod = async (): Promise<ShipmentMethodsResponse> => {
  const { data } = await api.get<ShipmentMethodsResponse>(
    "/public/delivery-methods/restaurant",
  );
  return data;
};

export const getPaymentMethod = async (): Promise<PaymentMethodsResponse> => {
  const { data } = await api.get<PaymentMethodsResponse>(
    "/public/payments/methods/restaurant",
  );
  return data;
};

export const setAddressCart = async (id: string) => {
  const { data } = await api.patch("/public/cart/address", { addressId: id });
  return data;
};

export const setDeliveryMethod = async (id: string) => {
  const { data } = await api.patch("/public/cart/delivery-method", {
    deliveryMethodId: id,
  });
  return data;
};

export const setPaymentMethod = async (id: string) => {
  const { data } = await api.patch("/public/cart/payment-method", {
    paymentMethodId: id,
  });
  return data;
};

export const createOrder = async (orderData?: {
  deliveryMethodId?: string;
  paymentMethodId?: string;
  addressId?: string;
  tableNumber?: number;
}): Promise<CreateOrderResponse> => {
  const { data } = await api.post<CreateOrderResponse>(
    "/public/checkout",
    orderData,
  );
  return data;
};

export const applyCoupon = async (code: string) => {
  const { data } = await api.post("/public/cart/apply-coupon", { code });
  return data;
};

export const removeCoupon = async () => {
  const { data } = await api.delete("/public/cart/coupon");
  return data;
};

export const setTableNumber = async (number: number) => {
  const { data } = await api.patch("/public/cart/table-number", {
    tableNumber: number.toString(),
  });
  return data;
};

export const getOrderDetail = async (
  id: string,
): Promise<OrderDetailResponse> => {
  const { data } = await api.get<OrderDetailResponse>(`/public/orders/${id}`);
  return data;
};

export const cancelOrder = async (id: string) => {
  const { data } = await api.patch(`/public/orders/${id}/canceled`);
  return data;
};

export const confirmOrder = async (id: string) => {
  const { data } = await api.get(`/public/payments/pay-order/${id}`);
  return data;
};
