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

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

  async getCart() {
    const res = await this.api.get('/cart');
    return res.data;
  }

  async addToCart(productId, quantity = 1) {
    const res = await this.api.post('/cart/add', { productId, quantity });
    return res.data;
  }

  async updateItem(itemId, quantity) {
    const res = await this.api.put(`/cart/item/${itemId}`, { quantity });
    return res.data;
  }

  async removeItem(itemId) {
    const res = await this.api.put(`/cart/item/${itemId}`, { quantity: 0 });
    return res.data;
  }
}

export default new CartService();
