import client from './client';

export interface SettingInfo {
  id: string;
  scope: string;
  scope_id?: string;
  setting_key: string;
  setting_value: unknown;
  version: number;
}

export async function getSetting(key: string, scope?: string, scopeId?: string) {
  const { data } = await client.get<{ success: boolean; data: SettingInfo }>(
    `/config/settings/${encodeURIComponent(key)}`,
    { params: { scope, scope_id: scopeId } },
  );
  return data.data;
}

export async function updateSetting(key: string, settingValue: unknown, version?: number) {
  const { data } = await client.put<{ success: boolean; data: SettingInfo }>(
    `/config/settings/${encodeURIComponent(key)}`,
    { setting_value: settingValue, version },
  );
  return data.data;
}

export async function deleteSetting(key: string, version: number) {
  await client.delete(`/config/settings/${encodeURIComponent(key)}`, { data: { version } });
}
