/**
 * INTENT Escalation Service
 *
 * Manages human-in-the-loop approval workflows for high-risk intents.
 * Uses PostgreSQL as primary storage with Redis for caching and fast lookups.
 * Supports escalation creation, acknowledgment, approval/rejection, and timeout handling.
 */
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
import type { Redis } from 'ioredis';
import { type Config } from '../common/config.js';
import type { ID } from '../common/types.js';
import type { TenantContext } from '../common/tenant-context.js';
/**
 * Dependencies for EscalationService
 *
 * Use these to inject dependencies for testing or custom configurations.
 * If not provided, defaults to global singletons for backward compatibility.
 */
export interface EscalationServiceDependencies {
    /** Drizzle database instance */
    database?: NodePgDatabase;
    /** Redis client instance */
    redis?: Redis;
    /** Application configuration */
    config?: Config;
}
export type EscalationStatus = 'pending' | 'acknowledged' | 'approved' | 'rejected' | 'timeout' | 'cancelled';
export interface EscalationRecord {
    id: ID;
    intentId: ID;
    tenantId: ID;
    reason: string;
    reasonCategory: 'trust_insufficient' | 'high_risk' | 'policy_violation' | 'manual_review' | 'constraint_escalate';
    escalatedTo: string;
    escalatedBy?: string;
    status: EscalationStatus;
    resolution?: {
        resolvedBy: string;
        resolvedAt: string;
        notes?: string;
    };
    timeout: string;
    timeoutAt: string;
    acknowledgedAt?: string;
    slaBreached: boolean;
    context?: Record<string, unknown>;
    metadata?: Record<string, unknown>;
    createdAt: string;
    updatedAt: string;
}
/**
 * Options for creating an escalation
 *
 * SECURITY: tenantId is now obtained from TenantContext in the create() method,
 * not from user input. This prevents tenant ID injection attacks.
 */
export interface CreateEscalationOptions {
    intentId: ID;
    reason: string;
    reasonCategory: EscalationRecord['reasonCategory'];
    escalatedTo: string;
    escalatedBy?: string;
    timeout?: string;
    context?: Record<string, unknown>;
    metadata?: Record<string, unknown>;
}
export interface ResolveEscalationOptions {
    resolvedBy: string;
    notes?: string;
}
/**
 * Filters for listing escalations
 *
 * SECURITY: tenantId is obtained from TenantContext in the list() method.
 */
export interface ListEscalationFilters {
    status?: EscalationStatus | EscalationStatus[];
    intentId?: ID;
    escalatedTo?: string;
    limit?: number;
    cursor?: ID;
    includeSlaBreached?: boolean;
}
/**
 * Escalation Service with PostgreSQL persistence and Redis caching
 *
 * SECURITY: All methods now accept TenantContext instead of raw tenantId.
 * TenantContext can only be created from validated JWT tokens, preventing
 * tenant ID injection attacks.
 *
 * @see TenantContext in ../common/tenant-context.ts
 */
export declare class EscalationService {
    private config;
    private redis;
    private db;
    private readonly cachePrefix;
    private readonly indexPrefix;
    private readonly cacheTtlSeconds;
    /**
     * Create a new EscalationService instance.
     *
     * @param deps - Optional dependencies for dependency injection.
     *               If not provided, uses global singletons (backward compatible).
     *
     * @example
     * // Default usage (backward compatible)
     * const service = new EscalationService();
     *
     * @example
     * // With dependency injection (for testing)
     * const service = new EscalationService({
     *   database: mockDb,
     *   redis: mockRedis,
     *   config: testConfig,
     * });
     */
    constructor(deps?: EscalationServiceDependencies);
    /**
     * Create a new escalation for an intent
     *
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param options - Escalation creation options
     * @returns Created escalation record
     */
    create(ctx: TenantContext, options: CreateEscalationOptions): Promise<EscalationRecord>;
    /**
     * Get an escalation by ID
     *
     * @param id - Escalation ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @returns Escalation record or null if not found
     */
    get(id: ID, ctx: TenantContext): Promise<EscalationRecord | null>;
    /**
     * Get escalation by intent ID (most recent)
     *
     * @param intentId - Intent ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @returns Most recent escalation for the intent or null if not found
     */
    getByIntentId(intentId: ID, ctx: TenantContext): Promise<EscalationRecord | null>;
    /**
     * List escalations with filters
     *
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param filters - Optional filters for status, intentId, etc.
     * @returns Array of escalation records
     */
    list(ctx: TenantContext, filters?: ListEscalationFilters): Promise<EscalationRecord[]>;
    /**
     * List pending escalations for a tenant (optimized with Redis index)
     *
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param options - Pagination options
     * @returns Array of pending escalation records
     */
    listPending(ctx: TenantContext, options?: {
        limit?: number;
        offset?: number;
    }): Promise<EscalationRecord[]>;
    /**
     * Acknowledge an escalation (SLA tracking)
     *
     * @param id - Escalation ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @returns Acknowledged escalation record or null if not found
     */
    acknowledge(id: ID, ctx: TenantContext): Promise<EscalationRecord | null>;
    /**
     * Approve an escalation
     *
     * @param id - Escalation ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param options - Resolution options (notes, etc.)
     * @returns Approved escalation record or null if not found
     */
    approve(id: ID, ctx: TenantContext, options?: Omit<ResolveEscalationOptions, 'resolvedBy'>): Promise<EscalationRecord | null>;
    /**
     * Reject an escalation
     *
     * @param id - Escalation ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param options - Resolution options (notes, etc.)
     * @returns Rejected escalation record or null if not found
     */
    reject(id: ID, ctx: TenantContext, options?: Omit<ResolveEscalationOptions, 'resolvedBy'>): Promise<EscalationRecord | null>;
    /**
     * Cancel an escalation
     *
     * @param id - Escalation ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param options - Resolution options (notes, etc.)
     * @returns Cancelled escalation record or null if not found
     */
    cancel(id: ID, ctx: TenantContext, options?: Omit<ResolveEscalationOptions, 'resolvedBy'>): Promise<EscalationRecord | null>;
    /**
     * Resolve an escalation (internal)
     *
     * @param id - Escalation ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param status - Resolution status
     * @param options - Resolution options
     * @returns Resolved escalation record or null if not found
     */
    private resolve;
    /**
     * Update SLA breach rate and approval rate gauges for a tenant
     */
    private updateRateGauges;
    /**
     * Process timed out escalations
     */
    processTimeouts(): Promise<number>;
    /**
     * Get escalation history for an intent
     *
     * @param intentId - Intent ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @returns Array of escalation records for the intent
     */
    getHistory(intentId: ID, ctx: TenantContext): Promise<EscalationRecord[]>;
    /**
     * Check if an intent has a pending escalation
     *
     * @param intentId - Intent ID
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @returns True if there is a pending or acknowledged escalation
     */
    hasPendingEscalation(intentId: ID, ctx: TenantContext): Promise<boolean>;
    /**
     * Get SLA breach statistics for a tenant
     *
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @param since - Optional start date for statistics
     * @returns SLA statistics
     */
    getSlaStats(ctx: TenantContext, since?: Date): Promise<{
        total: number;
        breached: number;
        breachRate: number;
        avgResolutionTime: number;
    }>;
    /**
     * Update Redis indexes for fast lookups
     */
    private updateRedisIndexes;
    /**
     * Invalidate cache for an escalation
     */
    private invalidateCache;
    /**
     * Rebuild Redis indexes from PostgreSQL (for recovery/consistency)
     *
     * @param ctx - Validated tenant context from JWT (REQUIRED for security)
     * @returns Number of records indexed
     */
    rebuildIndexes(ctx: TenantContext): Promise<{
        indexed: number;
    }>;
}
/**
 * Create a new escalation service instance with dependency injection.
 *
 * This is the preferred way to create services in production code
 * as it makes dependencies explicit and testable.
 *
 * @param deps - Optional dependencies. If not provided, uses global singletons.
 * @returns Configured EscalationService instance
 *
 * @example
 * // Default usage (backward compatible)
 * const service = createEscalationService();
 *
 * @example
 * // With custom dependencies
 * const service = createEscalationService({
 *   database: customDb,
 *   redis: customRedis,
 *   config: customConfig,
 * });
 */
export declare function createEscalationService(deps?: EscalationServiceDependencies): EscalationService;
//# sourceMappingURL=escalation.d.ts.map