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

import type {
  McpToolPolicy,
  PermissionSet,
  ProjectMcpToolPermissions,
  ProjectPermissions,
  ProjectSystemPermissions,
  ResolvedAgentPermissions,
  ResolvedAgentSystemPermissions,
  SystemPermissionSet,
} 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<ResolvedAgentPermissions> {
    return invoke<ResolvedAgentPermissions>("resolve_agent_permissions", {
      request: { projectId, agentId },
    });
  }

  getProjectSystemPermissions(projectId: string): Promise<ProjectSystemPermissions> {
    return invoke<ProjectSystemPermissions>("get_project_system_permissions", {
      projectId,
    });
  }

  updateProjectSystemPermissions(
    projectId: string,
    permissions: SystemPermissionSet | null,
  ): Promise<ProjectSystemPermissions> {
    return invoke<ProjectSystemPermissions>("update_project_system_permissions", {
      request: { projectId, permissions },
    });
  }

  updateAgentSystemPermissions(
    projectId: string,
    agentId: string,
    permissions: SystemPermissionSet | null,
  ): Promise<ProjectSystemPermissions> {
    return invoke<ProjectSystemPermissions>("update_agent_system_permissions", {
      request: { projectId, agentId, permissions },
    });
  }

  resolveAgentSystemPermissions(
    projectId: string,
    agentId: string,
  ): Promise<ResolvedAgentSystemPermissions> {
    return invoke<ResolvedAgentSystemPermissions>(
      "resolve_agent_system_permissions",
      { request: { projectId, agentId } },
    );
  }

  getMcpToolPermissions(projectId: string): Promise<ProjectMcpToolPermissions> {
    return invoke<ProjectMcpToolPermissions>("get_mcp_tool_permissions", {
      projectId,
    });
  }

  updateProjectMcpToolPermissions(
    projectId: string,
    policy: McpToolPolicy | null,
  ): Promise<ProjectMcpToolPermissions> {
    return invoke<ProjectMcpToolPermissions>(
      "update_project_mcp_tool_permissions",
      { request: { projectId, policy } },
    );
  }

  updateAgentMcpToolPermissions(
    projectId: string,
    agentId: string,
    policy: McpToolPolicy | null,
  ): Promise<ProjectMcpToolPermissions> {
    return invoke<ProjectMcpToolPermissions>(
      "update_agent_mcp_tool_permissions",
      { request: { projectId, agentId, policy } },
    );
  }
}
