/**
 * Copyright (c) 2024-2026 Тарабанов Александр Викторович
 * All rights reserved.
 * 
 * This software is proprietary and confidential.
 * Unauthorized copying, modification, or distribution is prohibited.
 * 
 * For licensing inquiries: info@hb3-accelerator.com
 * Website: https://hb3-accelerator.com
 * GitHub: https://github.com/VC-HB3-Accelerator
 */

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

const tablesApi = '/tables';

export default {
  async getTables() {
    const res = await api.get(`${tablesApi}?_t=${Date.now()}`);
    return res.data;
  },
  async createTable(data) {
    const res = await api.post(tablesApi, data);
    return res.data;
  },
  async getTable(id) {
    const res = await api.get(`${tablesApi}/${id}`);
    return res.data;
  },
  async addColumn(tableId, data) {
    const res = await api.post(`${tablesApi}/${tableId}/columns`, data);
    return res.data;
  },
  async addRow(tableId) {
    const res = await api.post(`${tablesApi}/${tableId}/rows`);
    return res.data;
  },
  async saveCell(data) {
    const res = await api.post(`${tablesApi}/cell`, data);
    return res.data;
  },
  async deleteColumn(columnId) {
    const res = await api.delete(`${tablesApi}/column/${columnId}`);
    return res.data;
  },
  async deleteRow(rowId) {
    const res = await api.delete(`${tablesApi}/row/${rowId}`);
    return res.data;
  },
  async updateColumn(columnId, data) {
    const res = await api.patch(`${tablesApi}/column/${columnId}`, data);
    return res.data;
  },
  async updateTable(id, data) {
    const res = await api.patch(`${tablesApi}/${id}`, data);
    return res.data;
  },
  async deleteTable(id) {
    // console.log('tablesService.deleteTable called with id:', id);
    try {
      const res = await api.delete(`${tablesApi}/${id}`);
              // console.log('Delete response:', res.data);
      return res.data;
    } catch (error) {
              // console.error('Error in deleteTable service:', error);
      throw error;
    }
  },
  async getFilteredRows(tableId, { product = '' } = {}) {
    const params = new URLSearchParams();
    if (product) params.append('product', product);
    const res = await api.get(`/tables/${tableId}/rows?${params.toString()}`);
    return res.data;
  },
  async rebuildIndex(tableId) {
    const res = await api.post(`/tables/${tableId}/rebuild-index`);
    return res.data;
  },
  async updateRowsOrder(tableId, orderArr) {
    // orderArr: [{rowId, order}, ...]
    const res = await api.patch(`/tables/${tableId}/rows/order`, { order: orderArr });
    return res.data;
  }
}; 