/**
 * Tauri adapter for {@link ModelServerGateway} (F35). One of the only places that
 * calls `invoke()`; features reach it exclusively through the port.
 *
 * Commands and payload keys are camelCase, matching the backend DTO convention.
 * The flat `LocalModelServerConfig` wire shape is the DTO frozen by Architect —
 * no nesting.
 */

import { invoke } from "@tauri-apps/api/core";

import type {
  LocalModelServerConfig,
  ModelServerCommandPreview,
} from "@/domain";
import type { ModelServerGateway } from "@/ports";

export class TauriModelServerGateway implements ModelServerGateway {
  listModelServers(): Promise<LocalModelServerConfig[]> {
    return invoke<LocalModelServerConfig[]>("list_model_servers");
  }

  saveModelServer(
    config: LocalModelServerConfig,
  ): Promise<LocalModelServerConfig> {
    return invoke<LocalModelServerConfig>("save_model_server", {
      request: { config },
    });
  }

  async deleteModelServer(serverId: string): Promise<void> {
    await invoke("delete_model_server", { serverId });
  }

  previewModelServerCommand(
    config: LocalModelServerConfig,
  ): Promise<ModelServerCommandPreview> {
    // `preview_model_server_command` takes the config directly (no `request`
    // wrapper), unlike `save_model_server`.
    return invoke<ModelServerCommandPreview>("preview_model_server_command", {
      config,
    });
  }
}
