import apiClient from './api';

const couponService = {
  /**
   * Get all coupons (admin only)
   * @returns {Promise} Promise with the API response
   */
  getAllCoupons: async () => {
    try {
      const response = await apiClient.get('/admin/coupons');
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Get a single coupon by ID (admin only)
   * @param {string} id - Coupon ID
   * @returns {Promise} Promise with the API response
   */
  getCouponById: async (id) => {
    try {
      const response = await apiClient.get(`/admin/coupons/${id}`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Create a new coupon (admin only)
   * @param {Object} couponData - Coupon data
   * @returns {Promise} Promise with the API response
   */
  createCoupon: async (couponData) => {
    try {
      const response = await apiClient.post('/admin/coupons', couponData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Update a coupon (admin only)
   * @param {string} id - Coupon ID
   * @param {Object} couponData - Updated coupon data
   * @returns {Promise} Promise with the API response
   */
  updateCoupon: async (id, couponData) => {
    try {
      const response = await apiClient.put(`/admin/coupons/${id}`, couponData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Delete a coupon (admin only)
   * @param {string} id - Coupon ID
   * @returns {Promise} Promise with the API response
   */
  deleteCoupon: async (id) => {
    try {
      const response = await apiClient.delete(`/admin/coupons/${id}`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Apply a coupon code to the cart
   * @param {string} userId - User ID
   * @param {string} code - Coupon code
   * @returns {Promise} Promise with the API response
   */
  applyCoupon: async (userId, code) => {
    try {
      const response = await apiClient.post(`/cart/apply-coupon`, { userId, code });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Remove a coupon from the cart
   * @param {string} userId - User ID
   * @returns {Promise} Promise with the API response
   */
  removeCoupon: async (userId) => {
    try {
      const response = await apiClient.post(`/cart/remove-coupon`, { userId });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Validate a coupon code
   * @param {string} code - Coupon code
   * @param {number} cartTotal - Cart total amount
   * @returns {Promise} Promise with the API response
   */
  validateCoupon: async (code, cartTotal) => {
    try {
      const response = await apiClient.post('/coupons/validate', { code, cartTotal });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Get coupon redemption history (admin only)
   * @param {string} id - Coupon ID
   * @returns {Promise} Promise with the API response
   */
  getCouponRedemptions: async (id) => {
    try {
      const response = await apiClient.get(`/admin/coupons/${id}/redemptions`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  }
};

export default couponService;