export default class ProductsApi {
  constructor(token) {
    this.baseUrl = 'https://inventory-bff.dream-views.com/api/v1/FurnitureVariant';
    this.token = token; 
  }

  headers(json = true) {
    return {
      accept: 'application/json',
      ...(json ? { 'Content-Type': 'application/json' } : {}),
      ...(this.token ? { Authorization: `Bearer ${this.token}` } : {}),
    };
  }

  async getAllVariants() {
    const res = await fetch(`${this.baseUrl}/GetAll`, {
      method: 'GET',
      headers: this.headers(false),
    });
    if (!res.ok) throw new Error(`GetAll error ${res.status}: ${await res.text()}`);
    return res.json();
  }

  // Assuming similar endpoints; adjust names if backend differs.
  async createVariant(payload) {
    const res = await fetch(`${this.baseUrl}/Create`, {
      method: 'POST',
      headers: this.headers(),
      body: JSON.stringify(payload),
    });
    if (!res.ok) throw new Error(`Create error ${res.status}: ${await res.text()}`);
    return res.json();
  }

  async updateVariant(payload) {
    const res = await fetch(`${this.baseUrl}/Update`, {
      method: 'PUT',
      headers: this.headers(),
      body: JSON.stringify(payload),
    });
    if (!res.ok) throw new Error(`Update error ${res.status}: ${await res.text()}`);
    return res.json();
  }

  async deleteVariant(payload) {
    // If your API is soft-delete via Update status, reuse updateVariant.
    const res = await fetch(`${this.baseUrl}/Delete`, {
      method: 'DELETE',
      headers: this.headers(),
      body: JSON.stringify(payload),
    });
    if (!res.ok) throw new Error(`Delete error ${res.status}: ${await res.text()}`);
    return res.json();
  }
}