/**
 * Tauri adapter for {@link InputGateway} (ARCHITECTURE §20.3).
 *
 * `interrupt` → `interrupt_agent` (preempt). `delegationDelivered` →
 * `delegation_delivered` (the native write-portal's ack that a delegation's
 * text was effectively written into the PTY). The former `submit` →
 * `submit_agent_input` path is gone: the agent cell is a native terminal, so
 * human keystrokes reach the PTY directly (no mediated-input strip).
 *
 * Commands use snake_case (Tauri convention); payload keys are camelCase
 * (matching the backend DTO `#[serde(rename_all = "camelCase")]`), consistent
 * with the other adapters in this directory.
 */

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

import type { InputGateway } from "@/ports";

export class TauriInputGateway implements InputGateway {
  async interrupt(projectId: string, agentId: string): Promise<void> {
    await invoke("interrupt_agent", {
      request: { projectId, agentId },
    });
  }

  async delegationDelivered(
    projectId: string,
    agentId: string,
    ticket: string,
  ): Promise<void> {
    console.info("[input-gateway] delegationDelivered:start", {
      projectId,
      agentId,
      ticket,
    });
    await invoke("delegation_delivered", {
      request: { projectId, agentId, ticket },
    });
    console.info("[input-gateway] delegationDelivered:ok", {
      projectId,
      agentId,
      ticket,
    });
  }

  async setFrontAttached(agentId: string, attached: boolean): Promise<void> {
    console.info("[input-gateway] setFrontAttached:start", { agentId, attached });
    await invoke("set_front_attached", {
      request: { agentId, attached },
    });
    console.info("[input-gateway] setFrontAttached:ok", { agentId, attached });
  }

  async cancelResume(agentId: string): Promise<boolean> {
    // `cancel_resume` takes a bare `agent_id` (camelCase `agentId`), not a
    // `request` envelope, and returns whether a pending resume was disarmed.
    return invoke<boolean>("cancel_resume", { agentId });
  }

  async setResumeAt(agentId: string, resetsAtMs: number): Promise<void> {
    // `set_resume_at` takes bare `agent_id` + `resets_at_ms` (camelCase), like
    // `cancel_resume`; arms the cancellable resume at the chosen instant.
    await invoke("set_resume_at", { agentId, resetsAtMs });
  }
}
