import apiClient from './api';

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

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

  /**
   * Update a user (admin only)
   * @param {string} id - User ID
   * @param {Object} userData - User data to update
   * @returns {Promise} Promise with the API response
   */
  updateUser: async (id, userData) => {
    try {
      const response = await apiClient.patch(`/admin/users/${id}`, userData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },
  /**
   * Send an email to a user (admin only)
   * @param {Object} emailData - Email data
   * @param {string} emailData.to - Recipient email
   * @param {string} emailData.name - Recipient name
   * @param {string} emailData.subject - Email subject
   * @param {string} emailData.message - Email message
   * @returns {Promise} Promise with the API response
   */
  sendEmail: async (emailData) => {
    try {
      const response = await apiClient.post('/admin/users/send-email', emailData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  }
};

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

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

  /**
   * Update an order's status (admin only)
   * @param {string} id - Order ID
   * @param {string} status - New order status
   * @returns {Promise} Promise with the API response
   */
  updateOrderStatus: async (id, status) => {
    try {
      const response = await apiClient.patch(`/admin/orders/${id}`, { status });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Update an order's status with shipping information (admin only)
   * @param {string} id - Order ID
   * @param {string} status - New order status
   * @param {Object} shippingData - Shipping information
   * @param {boolean} sendNotification - Whether to send email notification
   * @returns {Promise} Promise with the API response
   */
  updateOrderStatusWithShipping: async (id, status, shippingData, sendNotification = true) => {
    try {
      const response = await apiClient.patch(`/admin/orders/${id}/shipping`, { 
        status, 
        shippingData,
        sendNotification
      });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  }
};