/**
 * Decision Repository
 *
 * Persistence layer for decisions, constraints, refinements, and workflows.
 * Provides CRUD operations with proper transaction support.
 *
 * @packageDocumentation
 */
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { type NewDecisionConstraintsRow, type NewRefinementOptionRow, type NewWorkflowInstanceRow } from './schema.js';
import type { ID, Timestamp } from '../common/types.js';
export interface Decision {
    id: ID;
    tenantId: ID;
    intentId: ID;
    agentId: ID;
    correlationId: ID;
    permitted: boolean;
    tier: 'GREEN' | 'YELLOW' | 'RED';
    trustBand: string;
    trustScore: number;
    policySetId?: string | null;
    reasoning: string[];
    denialReason?: string | null;
    hardDenial?: boolean;
    violatedPolicies?: Array<{
        policyId: string;
        policyName: string;
        severity: 'warning' | 'error' | 'critical';
    }> | null;
    refinementDeadline?: Timestamp | null;
    maxRefinementAttempts?: number;
    refinementAttempt: number;
    originalDecisionId?: ID | null;
    appliedRefinements?: Array<{
        refinementId: string;
        appliedAt: string;
    }> | null;
    latencyMs: number;
    version: number;
    decidedAt: Timestamp;
    expiresAt: Timestamp;
    createdAt: Timestamp;
    constraints?: DecisionConstraints | null;
    refinementOptions?: RefinementOption[];
}
export interface DecisionConstraints {
    id: ID;
    decisionId: ID;
    tenantId: ID;
    allowedTools: string[];
    dataScopes: string[];
    rateLimits: Array<{
        resource: string;
        limit: number;
        windowSeconds: number;
    }>;
    requiredApprovals: Array<{
        type: string;
        approver: string;
        timeoutMs?: number;
        reason: string;
    }>;
    reversibilityRequired: boolean;
    maxExecutionTimeMs?: number | null;
    maxRetries?: number;
    resourceQuotas?: Record<string, number> | null;
    createdAt: Timestamp;
}
export type RefinementAction = 'PROVIDE_CONTEXT' | 'REDUCE_SCOPE' | 'ADD_CONSTRAINTS' | 'REQUEST_APPROVAL' | 'DECOMPOSE' | 'WAIT_FOR_TRUST';
export interface RefinementOption {
    id: ID;
    decisionId: ID;
    tenantId: ID;
    action: RefinementAction;
    description: string;
    successProbability: number;
    effort: 'low' | 'medium' | 'high';
    parameters?: Record<string, unknown> | null;
    resultingConstraints?: Record<string, unknown> | null;
    selected: boolean;
    appliedAt?: Timestamp | null;
    createdAt: Timestamp;
}
export interface WorkflowInstance {
    id: ID;
    tenantId: ID;
    intentId: ID;
    agentId: ID;
    correlationId: ID;
    state: string;
    currentDecisionId?: ID | null;
    stateHistory: Array<{
        from: string;
        to: string;
        reason: string;
        timestamp: string;
    }>;
    execution?: {
        executionId: string;
        startedAt: string;
        completedAt?: string;
        status: 'running' | 'completed' | 'failed';
        result?: unknown;
        error?: string;
    } | null;
    createdAt: Timestamp;
    updatedAt: Timestamp;
    expiresAt: Timestamp;
}
export interface CreateDecisionInput {
    tenantId: ID;
    intentId: ID;
    agentId: ID;
    correlationId: ID;
    permitted: boolean;
    tier: 'GREEN' | 'YELLOW' | 'RED';
    trustBand: string;
    trustScore: number;
    policySetId?: string;
    reasoning: string[];
    denialReason?: string;
    hardDenial?: boolean;
    violatedPolicies?: Array<{
        policyId: string;
        policyName: string;
        severity: 'warning' | 'error' | 'critical';
    }>;
    refinementDeadline?: Date;
    maxRefinementAttempts?: number;
    refinementAttempt?: number;
    originalDecisionId?: ID;
    latencyMs: number;
    expiresAt: Date;
    constraints?: Omit<NewDecisionConstraintsRow, 'id' | 'decisionId' | 'tenantId' | 'createdAt'>;
    refinementOptions?: Array<Omit<NewRefinementOptionRow, 'id' | 'decisionId' | 'tenantId' | 'createdAt'>>;
}
export interface PaginatedResult<T> {
    data: T[];
    total: number;
    hasMore: boolean;
    cursor?: string;
}
export declare const DEFAULT_PAGE_SIZE = 50;
export declare const MAX_PAGE_SIZE = 100;
export declare class DecisionRepository {
    private db;
    constructor(db: PostgresJsDatabase);
    /**
     * Create a decision with optional constraints and refinement options
     */
    createDecision(input: CreateDecisionInput): Promise<Decision>;
    /**
     * Get a decision by ID with constraints and refinement options
     */
    getDecision(id: ID, tenantId: ID): Promise<Decision | null>;
    /**
     * Get the latest decision for an intent
     */
    getLatestDecisionForIntent(intentId: ID, tenantId: ID): Promise<Decision | null>;
    /**
     * List decisions for an agent
     */
    listDecisionsByAgent(agentId: ID, tenantId: ID, options?: {
        limit?: number;
        offset?: number;
    }): Promise<PaginatedResult<Decision>>;
    /**
     * Get decisions pending refinement (YELLOW tier with upcoming deadline)
     */
    getPendingRefinements(tenantId: ID, options?: {
        limit?: number;
    }): Promise<Decision[]>;
    /**
     * Apply a refinement to a YELLOW decision
     */
    applyRefinement(decisionId: ID, refinementId: ID, tenantId: ID): Promise<RefinementOption | null>;
    /**
     * Create a workflow instance
     */
    createWorkflow(input: Omit<NewWorkflowInstanceRow, 'id' | 'createdAt' | 'updatedAt'>): Promise<WorkflowInstance>;
    /**
     * Get workflow by intent ID
     */
    getWorkflowByIntent(intentId: ID, tenantId: ID): Promise<WorkflowInstance | null>;
    /**
     * Update workflow state
     */
    updateWorkflowState(workflowId: ID, tenantId: ID, newState: string, reason: string, currentDecisionId?: ID): Promise<WorkflowInstance | null>;
    /**
     * Update workflow execution status
     */
    updateWorkflowExecution(workflowId: ID, tenantId: ID, execution: WorkflowInstance['execution']): Promise<WorkflowInstance | null>;
    /**
     * Get expired workflows for cleanup
     */
    getExpiredWorkflows(limit?: number): Promise<WorkflowInstance[]>;
}
/**
 * Create a decision repository instance
 */
export declare function createDecisionRepository(db: PostgresJsDatabase): DecisionRepository;
//# sourceMappingURL=repository.d.ts.map