import axios from 'axios';
import API_BASE_URL from '../config/api';

class OrderService {
  constructor() {
    this.api = axios.create({
      baseURL: API_BASE_URL,
      headers: {
        'Content-Type': 'application/json'
      }
    });

    // Add withCredentials to send cookies with requests
    this.api.defaults.withCredentials = true;
  }

  async getOrders() {
    const response = await this.api.get('/orders');
    return response.data;
  }

  async getOrder(id) {
    const response = await this.api.get(`/orders/${id}`);
    return response.data;
  }

  async createOrder(orderData) {
    const response = await this.api.post('/orders', orderData);
    return response.data;
  }

  async updateOrderStatus(id, statusData) {
    const response = await this.api.put(`/orders/${id}/status`, statusData);
    return response.data;
  }

  async getAllOrders(params = {}) {
    const response = await this.api.get('/orders/admin/all', { params });
    return response.data;
  }
}

export default new OrderService();
