import apiService from '../../../APIRequestService/APIServices';

export const fetchRegistry = async () => {
  try {
    const response = await apiService.get('/Api_registery_header/Api_registery_header');
    if (response.data && Array.isArray(response.data)) {
      return response.data;
    }
    throw new Error('Invalid response format');
  } catch (error) {
    console.error("Error in fetchRegistry:", error);
    throw error;
  }
};

export const addRegistry = async (payload) => {
  try {
    const response = await apiService.post('/Api_registery_header/Api_registery_header', {
      table_name: payload.table_name,
      createdAt: payload.createdAt || new Date().toISOString(),
      updatedAt: payload.updatedAt || new Date().toISOString(),
      isActive: payload.isActive || false
    });
    return response.data;
  } catch (error) {
    console.error("Error in addRegistry:", error);
    throw error;
  }
};

export const updateRegistry = async (id, payload) => {
  try {
    const response = await apiService.put(`/Api_registery_header/Api_registery_header/${id}`, {
      table_name: payload.table_name,
      createdAt: payload.createdAt,
      updatedAt: new Date().toISOString(), // Always update the updatedAt timestamp
      isActive: payload.isActive
    });
    return response.data;
  } catch (error) {
    console.error("Error in updateRegistry:", error);
    throw error;
  }
};

export const deleteRegistry = async (id) => {
  try {
    const response = await apiService.delete(`/Api_registery_header/Api_registery_header/${id}`);
    return response.data;
  } catch (error) {
    console.error("Error in deleteRegistry:", error);
    throw error;
  }
};