/**
 * Database-backed API Key Store
 *
 * Production-ready implementation of IApiKeyStore using Drizzle ORM
 * with PostgreSQL. Supports optional Redis for high-performance rate limiting.
 *
 * Features:
 * - Full CRUD operations with proper transactions
 * - Efficient indexing (by prefix, tenantId)
 * - Rate limit state with TTL-based cleanup
 * - Redis integration for rate limiting when available
 * - Graceful fallback to database when Redis is unavailable
 *
 * @packageDocumentation
 */
import type { Redis } from 'ioredis';
import { type Database } from '../../common/db.js';
import type { IApiKeyStore } from './store.js';
import type { ApiKey, ApiKeyStatus, ApiKeyRateLimitState, ApiKeyListFilters } from './types.js';
/**
 * Database-backed API key store implementation.
 *
 * Uses PostgreSQL via Drizzle ORM for persistent storage of API keys.
 * Optionally uses Redis for high-performance rate limiting.
 */
export declare class DbApiKeyStore implements IApiKeyStore {
    private db;
    private redis;
    private cleanupInterval;
    constructor(options?: {
        database?: Database;
        redis?: Redis | null;
    });
    /**
     * Create a new API key
     */
    create(apiKey: ApiKey): Promise<ApiKey>;
    /**
     * Get an API key by ID
     */
    getById(id: string): Promise<ApiKey | null>;
    /**
     * Get an API key by prefix (first 8 chars)
     * This is the primary lookup method during validation
     */
    getByPrefix(prefix: string): Promise<ApiKey | null>;
    /**
     * Update an API key
     */
    update(id: string, updates: Partial<ApiKey>): Promise<ApiKey | null>;
    /**
     * Delete an API key
     */
    delete(id: string): Promise<boolean>;
    /**
     * List API keys with filters
     */
    list(filters: ApiKeyListFilters): Promise<{
        keys: ApiKey[];
        total: number;
    }>;
    /**
     * Get rate limit state for a key
     *
     * Uses Redis if available, falls back to database
     */
    getRateLimitState(keyId: string): Promise<ApiKeyRateLimitState | null>;
    /**
     * Set rate limit state for a key
     *
     * Uses Redis if available, always persists to database as backup
     */
    setRateLimitState(state: ApiKeyRateLimitState): Promise<void>;
    /**
     * Atomically increment rate limit counters using Redis INCR
     *
     * This is the preferred method for distributed rate limiting.
     * Uses Redis atomic INCR with EXPIRE for sliding window.
     * Falls back to database-based counting if Redis is unavailable.
     *
     * @param keyId - The API key ID
     * @returns Current counts after incrementing
     */
    incrementRateLimitCounters(keyId: string): Promise<{
        second: number;
        minute: number;
        hour: number;
    }>;
    /**
     * Increment rate limit counters in the database
     * Used as fallback when Redis is unavailable
     */
    private incrementDbRateLimitCounters;
    /**
     * Update last used timestamp
     */
    updateLastUsed(id: string): Promise<void>;
    /**
     * Reset the store (for testing)
     */
    reset(): void;
    /**
     * Stop cleanup interval
     */
    stop(): void;
    private getRedisRateLimitState;
    private setRedisRateLimitState;
    private getDbRateLimitState;
    private setDbRateLimitState;
    /**
     * Start periodic cleanup of stale rate limit records
     */
    private startCleanup;
    /**
     * Remove expired rate limit records
     */
    cleanupStaleRateLimits(): Promise<number>;
    /**
     * Update expired API keys status
     */
    updateExpiredKeys(): Promise<number>;
    private mapRecordToApiKey;
    private mapRateLimitRecordToState;
    private isDuplicateKeyError;
    /**
     * Get store statistics
     */
    getStats(): Promise<{
        totalKeys: number;
        byStatus: Record<ApiKeyStatus, number>;
        byTenant: Record<string, number>;
        rateLimitStates: number;
    }>;
}
/**
 * Get the database API key store singleton
 */
export declare function getDbApiKeyStore(redis?: Redis | null): DbApiKeyStore;
/**
 * Create a new database API key store instance (for testing)
 */
export declare function createDbApiKeyStore(options?: {
    database?: Database;
    redis?: Redis | null;
}): DbApiKeyStore;
/**
 * Reset the database API key store singleton (for testing)
 */
export declare function resetDbApiKeyStore(): void;
//# sourceMappingURL=db-store.d.ts.map