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

import type {
  EffectivePermissions,
  PermissionSet,
  ProjectPermissions,
} from "@/domain";
import type { PermissionGateway } from "@/ports";

/** Tauri-backed permissions gateway. */
export class TauriPermissionGateway implements PermissionGateway {
  getProjectPermissions(projectId: string): Promise<ProjectPermissions> {
    return invoke<ProjectPermissions>("get_project_permissions", { projectId });
  }

  updateProjectPermissions(
    projectId: string,
    permissions: PermissionSet | null,
  ): Promise<ProjectPermissions> {
    return invoke<ProjectPermissions>("update_project_permissions", {
      request: { projectId, permissions },
    });
  }

  updateAgentPermissions(
    projectId: string,
    agentId: string,
    permissions: PermissionSet | null,
  ): Promise<ProjectPermissions> {
    return invoke<ProjectPermissions>("update_agent_permissions", {
      request: { projectId, agentId, permissions },
    });
  }

  resolveAgentPermissions(
    projectId: string,
    agentId: string,
  ): Promise<EffectivePermissions | null> {
    return invoke<EffectivePermissions | null>("resolve_agent_permissions", {
      request: { projectId, agentId },
    });
  }
}
