import { apiFetch } from "./http";
import type { SalaryConfigRecord, SalaryConfigRequest } from "./types";

export function listSalaryConfigs(configGroup = ""): Promise<SalaryConfigRecord[]> {
  const params = new URLSearchParams();
  if (configGroup) {
    params.set("config_group", configGroup);
  }
  const query = params.toString();
  return apiFetch<SalaryConfigRecord[]>(`/api/configs${query ? `?${query}` : ""}`);
}

export function updateSalaryConfig(configKey: string, request: SalaryConfigRequest): Promise<SalaryConfigRecord> {
  return apiFetch<SalaryConfigRecord>(`/api/configs/${encodeURIComponent(configKey)}`, {
    method: "PUT",
    body: JSON.stringify(request),
  });
}

export function ensureDefaultConfigs(): Promise<{ created_count: number; message: string }> {
  return apiFetch<{ created_count: number; message: string }>("/api/configs/defaults", {
    method: "POST",
  });
}
