import { AgentsRunDatabaseClient } from "../db/runtime/runtime-client.js";
import { PinoLogger } from "../utils/logger.js";
import { AgentsManageDatabaseClient } from "../db/manage/manage-client.js";
import { AgentSelect, ContextConfigSelect, SubAgentSelect, ToolSelect } from "../types/entities.js";

//#region src/data-reconciliation/types.d.ts
type EntityRowByTable = {
  tools: ToolSelect;
  context_configs: ContextConfigSelect;
  agent: AgentSelect;
  sub_agents: SubAgentSelect;
};
type EntityOperation = 'insert' | 'update' | 'delete';
type EntityDiff<TTable extends keyof EntityRowByTable = keyof EntityRowByTable> = {
  table: TTable;
  operation: 'insert';
  primaryKey: Record<string, string>;
  before: null;
  after: EntityRowByTable[TTable];
} | {
  table: TTable;
  operation: 'update';
  primaryKey: Record<string, string>;
  before: EntityRowByTable[TTable];
  after: EntityRowByTable[TTable];
} | {
  table: TTable;
  operation: 'delete';
  primaryKey: Record<string, string>;
  before: EntityRowByTable[TTable];
  after: null;
};
type EntityEffectHandlers<TTable extends keyof EntityRowByTable> = {
  onCreated?: (after: EntityRowByTable[TTable], ctx: ReconcileContext) => Promise<void>;
  onUpdated?: (before: EntityRowByTable[TTable], after: EntityRowByTable[TTable], ctx: ReconcileContext) => Promise<void>;
  onDeleted?: (before: EntityRowByTable[TTable], ctx: ReconcileContext) => Promise<void>;
  check?: (ctx: AuditContext) => Promise<unknown>;
};
type EntityEffectRegistry = { [K in keyof EntityRowByTable]?: EntityEffectHandlers<K> };
declare function defineHandlers<TTable extends keyof EntityRowByTable>(_table: TTable, handlers: EntityEffectHandlers<TTable>): EntityEffectHandlers<TTable>;
type ReconcileContext = {
  manageDb: AgentsManageDatabaseClient;
  runDb: AgentsRunDatabaseClient;
  scopes: {
    tenantId: string;
    projectId: string;
  };
  fullBranchName: string;
  logger: PinoLogger;
};
type AuditContext = ReconcileContext;
type AppliedEffect = {
  table: string;
  operation: EntityOperation;
  primaryKey: Record<string, string>;
};
type FailedEffect = AppliedEffect & {
  error: string;
};
type SkippedDiff = {
  table: string;
  operation: EntityOperation;
  primaryKey: Record<string, string>;
  reason: string;
};
type ReconcileResult = {
  applied: AppliedEffect[];
  skipped: SkippedDiff[];
  failed: FailedEffect[];
};
type EntityAuditEntry<TResult = unknown> = {
  table: string;
  result: TResult;
  error?: string;
};
type AuditReport = {
  entries: EntityAuditEntry[];
  checkedEntities: string[];
  skippedEntities: string[];
};
type OrphanedRuntimeRowsResult = {
  orphanedRows: Array<{
    table: string;
    id: string;
    referencedEntityId: string;
  }>;
};
//#endregion
export { AppliedEffect, AuditContext, AuditReport, EntityAuditEntry, EntityDiff, EntityEffectHandlers, EntityEffectRegistry, EntityOperation, EntityRowByTable, FailedEffect, OrphanedRuntimeRowsResult, ReconcileContext, ReconcileResult, SkippedDiff, defineHandlers };