/**
 * Tauri adapter for {@link ProjectGateway} (L2). Together with the sibling
 * adapters this is the *only* place that calls `invoke()`; components reach it
 * exclusively through the port.
 *
 * Commands and payload keys are camelCase, matching the backend DTO convention.
 */

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

import type { Project } from "@/domain";
import type { ProjectGateway } from "@/ports";

export class TauriProjectGateway implements ProjectGateway {
  listProjects(): Promise<Project[]> {
    return invoke<Project[]>("list_projects");
  }

  createProject(name: string, root: string): Promise<Project> {
    return invoke<Project>("create_project", { request: { name, root } });
  }

  openProject(projectId: string): Promise<Project> {
    return invoke<Project>("open_project", { projectId });
  }

  async closeProject(projectId: string): Promise<void> {
    await invoke("close_project", { projectId });
  }

  readProjectContext(projectId: string): Promise<string> {
    return invoke<string>("read_project_context", { projectId });
  }

  async updateProjectContext(projectId: string, content: string): Promise<void> {
    await invoke("update_project_context", {
      request: { projectId, content },
    });
  }
}
