/** Server profiles, private-key selection and the portable profile format (chj-server-profiles v1). */
import { call } from "../transport";
import type { ProfileInput, ServerProfile } from "../types";

export interface ImportPreviewRow { name: string; host: string; username: string; port: number; duplicate: boolean }
export type ImportPreview = { canceled: true } | { canceled: false; token: string; format: "json" | "csv"; servers: ImportPreviewRow[] };

export const profiles = Object.freeze({
  list: () => call<ServerProfile[]>("core_profiles_list", "profiles:list"),
  save: (payload: ProfileInput) => call<ServerProfile>("core_profiles_save", "profiles:save", payload),
  delete: (id: string) => call<{ removed: boolean }>("core_profiles_delete", "profiles:delete", { id }),
  selectPrivateKey: () => call<{ canceled: boolean; path: string | null }>("core_profiles_select_private_key", "profiles:selectPrivateKey"),
  export: (format: "json" | "csv", ids?: string[]) => call<{ canceled: boolean; count?: number }>("core_profiles_export", "profiles:export", { format, ids }),
  previewImport: () => call<ImportPreview>("core_profiles_preview_import", "profiles:previewImport"),
  commitImport: (token: string, policy: "skip" | "update" | "create") => call<{ created: number; updated: number; skipped: number }>("core_profiles_commit_import", "profiles:commitImport", { token, policy }),
  cancelImport: () => call<{ canceled: true }>("core_profiles_cancel_import", "profiles:cancelImport")
});
