import { AgentScopeConfig, ProjectScopeConfig } from "../../db/manage/scope-definitions.js";
import { AgentsRunDatabaseClient } from "../../db/runtime/runtime-client.js";
import "../../types/index.js";

//#region src/data-access/runtime/cascade-delete.d.ts

/**
 * Result of a cascade delete operation.
 *
 * Conversation-child cleanup strategies in this module (taxonomy):
 *  - DB-level FK cascade only: feedback
 *  - DB-level FK cascade plus app-level explicit delete: messages, contextCache
 *  - App-level only (no FK): events — forward-anchored events may reference
 *    conversation/message IDs that don't yet exist, so an FK is incompatible
 *    with the table's analytics-stream semantics (see specs/2026-05-11-events-drop-fk/SPEC.md).
 */
type CascadeDeleteResult = {
  conversationsDeleted: number;
  tasksDeleted: number;
  contextCacheDeleted: number;
  triggerInvocationsDeleted: number;
  scheduledTriggerInvocationsDeleted: number;
  datasetRunsDeleted: number;
  evaluationRunsDeleted: number;
  apiKeysDeleted: number;
  slackChannelConfigsDeleted: number;
  slackWorkspaceDefaultsCleared: number;
  appsDeleted: number;
  appDefaultsCleared: number;
  scheduledTriggersDeleted: number;
  eventsDeleted: number;
};
/**
 * Delete all runtime entities for a specific branch.
 * PostgreSQL cascades handle: messages, taskRelations, ledgerArtifacts.
 * Events are deleted explicitly before conversations (no FK cascade).
 *
 * @param db - Runtime database client
 * @returns Function that performs the cascade delete
 */
declare const cascadeDeleteByBranch: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  fullBranchName: string;
  ref: string;
}) => Promise<CascadeDeleteResult>;
/**
 * Delete all runtime entities for a project across all branches.
 * Used when deleting a project entirely.
 * PostgreSQL cascades handle: messages, taskRelations, ledgerArtifacts.
 * Events are deleted explicitly before conversations (no FK cascade).
 *
 * @param db - Runtime database client
 * @returns Function that performs the cascade delete
 */
declare const cascadeDeleteByProject: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
}) => Promise<CascadeDeleteResult>;
/**
 * Delete all runtime entities for a specific agent on a specific branch.
 * This includes tasks for the agent and conversations where the agent's
 * subAgents are active.
 *
 * @param db - Runtime database client
 * @returns Function that performs the cascade delete
 */
declare const cascadeDeleteByAgent: (db: AgentsRunDatabaseClient) => (params: {
  scopes: AgentScopeConfig;
  fullBranchName: string;
  subAgentIds: string[];
}) => Promise<CascadeDeleteResult>;
/**
 * Delete all runtime entities for a specific subAgent on a specific branch.
 *
 * @param db - Runtime database client
 * @returns Function that performs the cascade delete
 */
declare const cascadeDeleteBySubAgent: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  subAgentId: string;
  fullBranchName: string;
}) => Promise<CascadeDeleteResult>;
/**
 * Delete contextCache entries for a specific contextConfig.
 * Used when deleting a contextConfig from the manage DB.
 *
 * @param db - Runtime database client
 * @returns Function that performs the delete
 */
declare const cascadeDeleteByContextConfig: (db: AgentsRunDatabaseClient) => (params: {
  scopes: ProjectScopeConfig;
  contextConfigId: string;
  fullBranchName: string;
}) => Promise<{
  contextCacheDeleted: number;
}>;
/**
 * Result of a tool cascade delete operation
 */
type ToolCascadeDeleteResult = {
  mcpToolRepositoryAccessDeleted: number;
  mcpToolAccessModeDeleted: boolean;
  slackMcpToolAccessConfigDeleted: boolean;
};
/**
 * Delete all runtime entities for a specific MCP tool.
 * Called when an MCP tool is deleted from the manage database.
 *
 * Cleans up:
 * - workAppGitHubMcpToolRepositoryAccess entries
 * - workAppGitHubMcpToolAccessMode entry
 *
 * @param db - Runtime database client
 * @returns Function that performs the cascade delete
 */
declare const cascadeDeleteByTool: (db: AgentsRunDatabaseClient) => (params: {
  toolId: string;
  tenantId: string;
  projectId: string;
}) => Promise<ToolCascadeDeleteResult>;
/**
 * Result of a project cascade delete operation (GitHub access only)
 */
type ProjectGitHubAccessCascadeDeleteResult = {
  projectRepositoryAccessDeleted: number;
  projectAccessModeDeleted: boolean;
  mcpToolRepositoryAccessDeleted: number;
  mcpToolAccessModesDeleted: number;
};
/**
 * Delete all GitHub access runtime entities for a specific project.
 * Called when a project is deleted from the manage database.
 *
 * Cleans up:
 * - workAppGitHubProjectRepositoryAccess entries
 * - workAppGitHubProjectAccessMode entry
 * - workAppGitHubMcpToolRepositoryAccess entries (for tools in this project)
 * - workAppGitHubMcpToolAccessMode entries (for tools in this project)
 *
 * @param db - Runtime database client
 * @returns Function that performs the cascade delete
 */
declare const cascadeDeleteGitHubAccessByProject: (db: AgentsRunDatabaseClient) => (params: {
  tenantId: string;
  projectId: string;
}) => Promise<ProjectGitHubAccessCascadeDeleteResult>;
//#endregion
export { CascadeDeleteResult, ProjectGitHubAccessCascadeDeleteResult, ToolCascadeDeleteResult, cascadeDeleteByAgent, cascadeDeleteByBranch, cascadeDeleteByContextConfig, cascadeDeleteByProject, cascadeDeleteBySubAgent, cascadeDeleteByTool, cascadeDeleteGitHubAccessByProject };