import axios from 'axios';
import { httpClient, httpClientFetcher } from '@/services/http/client';
import { BaseApiResponse } from '@/types/api/api-general';
import {
  SetAllowNegativePakanOvkPayload,
  SystemSetting,
} from '@/types/api/system-settings/system-setting';

const BASE_PATH = '/system-settings';

export const SystemSettingsApi = {
  basePath: BASE_PATH,

  getAllFetcher: (
    endpoint: string
  ): Promise<BaseApiResponse<SystemSetting[]>> =>
    httpClientFetcher<BaseApiResponse<SystemSetting[]>>(endpoint),

  async getAll(): Promise<BaseApiResponse<SystemSetting[]> | undefined> {
    try {
      return await httpClient<BaseApiResponse<SystemSetting[]>>(BASE_PATH);
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse<SystemSetting[]>>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  },

  async setAllowNegativePakanOvk(
    payload: SetAllowNegativePakanOvkPayload
  ): Promise<BaseApiResponse | undefined> {
    try {
      return await httpClient<BaseApiResponse>(
        `${BASE_PATH}/allow-negative-pakan-ovk`,
        {
          method: 'PATCH',
          body: payload,
        }
      );
    } catch (error) {
      if (axios.isAxiosError<BaseApiResponse>(error)) {
        return error.response?.data;
      }
      return undefined;
    }
  },
};
