/**
 * API client for system settings
 */


export interface SystemSetting {
  key: string;
  value: string | null;
  description: string | null;
  updated_at: string | null;
}

export interface SupplementTableSettings {
  url: string;
  enabled: boolean;
}

/**
 * Get all system settings
 */
export async function getAllSettings(): Promise<{ settings: SystemSetting[] }> {
  const response = await fetch(`/api/system-settings`, {
    credentials: 'include',
  });
  if (!response.ok) {
    throw new Error(`Failed to fetch settings: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Get a specific setting by key
 */
export async function getSetting(key: string): Promise<SystemSetting> {
  const response = await fetch(`/api/system-settings/${key}`, {
    credentials: 'include',
  });
  if (!response.ok) {
    throw new Error(`Failed to fetch setting: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Update or create a setting
 */
export async function updateSetting(
  key: string,
  value: string,
  description?: string
): Promise<SystemSetting> {
  const response = await fetch(`/api/system-settings/${key}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    credentials: 'include',
    body: JSON.stringify({ value, description }),
  });
  if (!response.ok) {
    throw new Error(`Failed to update setting: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Delete a setting
 */
export async function deleteSetting(key: string): Promise<{ message: string }> {
  const response = await fetch(`/api/system-settings/${key}`, {
    method: 'DELETE',
    credentials: 'include',
  });
  if (!response.ok) {
    throw new Error(`Failed to delete setting: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Get supplement table settings
 */
export async function getSupplementTableSettings(): Promise<SupplementTableSettings> {
  const response = await fetch(`/api/system-settings/supplement-table`, {
    credentials: 'include',
  });
  if (!response.ok) {
    throw new Error(`Failed to fetch supplement table settings: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Update supplement table settings
 */
export async function updateSupplementTableSettings(
  url: string,
  enabled: boolean
): Promise<SupplementTableSettings & { message: string }> {
  const response = await fetch(`/api/system-settings/supplement-table`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    credentials: 'include',
    body: JSON.stringify({ url, enabled }),
  });
  if (!response.ok) {
    throw new Error(`Failed to update supplement table settings: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Get holiday banner setting
 */
export async function getHolidayBannerSetting(): Promise<{ enabled: boolean }> {
  const response = await fetch(`/api/system-settings/holiday-banner`, {
    credentials: 'include',
  });
  if (!response.ok) {
    throw new Error(`Failed to fetch holiday banner setting: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Update holiday banner setting
 */
export async function updateHolidayBannerSetting(
  enabled: boolean
): Promise<{ enabled: boolean; message: string }> {
  const response = await fetch(`/api/system-settings/holiday-banner`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    credentials: 'include',
    body: JSON.stringify({ enabled }),
  });
  if (!response.ok) {
    throw new Error(`Failed to update holiday banner setting: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Get organization name (public endpoint)
 */
export async function getOrganizationName(): Promise<{ name: string }> {
  const response = await fetch(`/api/system-settings/organization-name`, {
    credentials: 'include',
  });
  if (!response.ok) {
    throw new Error(`Failed to fetch organization name: ${response.statusText}`);
  }
  return response.json();
}

/**
 * Update organization name (superadmin only)
 */
export async function updateOrganizationName(name: string): Promise<{ name: string; message: string }> {
  const response = await fetch(`/api/system-settings/organization-name`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    credentials: 'include',
    body: JSON.stringify({ name }),
  });
  if (!response.ok) {
    throw new Error(`Failed to update organization name: ${response.statusText}`);
  }
  return response.json();
}
