/**
 * API Key Storage Layer
 *
 * Provides both in-memory and database-backed storage implementations
 * for API key management. Use the factory function to get the appropriate
 * store based on environment configuration.
 *
 * Features:
 * - CRUD operations for API keys
 * - Lookup by prefix (first 8 chars)
 * - Rate limit state tracking
 * - Singleton pattern with reset for testing
 * - Environment-based store selection (memory for testing, database for production)
 * - Redis integration for high-performance rate limiting
 *
 * Store Selection:
 * - VORION_API_KEY_STORE=database: Use PostgreSQL database (production)
 * - VORION_API_KEY_STORE=memory: Use in-memory store (testing)
 * - Default: memory in development, database in production/staging
 *
 * @packageDocumentation
 */
import type { Redis } from 'ioredis';
import type { Database } from '../../common/db.js';
import type { ApiKey, ApiKeyStatus, ApiKeyRateLimitState, ApiKeyListFilters } from './types.js';
/**
 * Storage interface for API keys
 */
export interface IApiKeyStore {
    /**
     * 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)
     */
    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
     */
    getRateLimitState(keyId: string): Promise<ApiKeyRateLimitState | null>;
    /**
     * Set rate limit state for a key
     */
    setRateLimitState(state: ApiKeyRateLimitState): Promise<void>;
    /**
     * Atomically increment rate limit counters using Redis INCR
     * This is the preferred method for distributed rate limiting
     */
    incrementRateLimitCounters?(keyId: string): Promise<{
        second: number;
        minute: number;
        hour: number;
    }>;
    /**
     * Update last used timestamp
     */
    updateLastUsed(id: string): Promise<void>;
    /**
     * Reset the store (for testing)
     */
    reset(): void;
    /**
     * Stop the store (cleanup intervals, connections, etc.)
     */
    stop(): void;
}
/**
 * In-memory implementation of API key store.
 *
 * NOTE: This is suitable for development and testing only.
 * Production deployments should use Redis or a database.
 */
export declare class InMemoryApiKeyStore implements IApiKeyStore {
    /** API keys indexed by ID */
    private keysById;
    /** API keys indexed by prefix for fast lookup */
    private keysByPrefix;
    /** Rate limit states indexed by key ID */
    private rateLimitStates;
    /** Cleanup interval handle */
    private cleanupInterval;
    constructor();
    /**
     * 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
     */
    getRateLimitState(keyId: string): Promise<ApiKeyRateLimitState | null>;
    /**
     * Set rate limit state for a key
     */
    setRateLimitState(state: ApiKeyRateLimitState): Promise<void>;
    /**
     * Update last used timestamp
     */
    updateLastUsed(id: string): Promise<void>;
    /**
     * Reset the store (for testing)
     */
    reset(): void;
    /**
     * Start periodic cleanup of stale rate limit states
     */
    private startCleanup;
    /**
     * Stop the cleanup interval
     */
    stop(): void;
    /**
     * Get store statistics
     */
    getStats(): {
        totalKeys: number;
        byStatus: Record<ApiKeyStatus, number>;
        byTenant: Record<string, number>;
        rateLimitStates: number;
    };
}
/**
 * Get the API key store singleton
 */
export declare function getApiKeyStore(): InMemoryApiKeyStore;
/**
 * Create a new API key store instance (for testing)
 */
export declare function createApiKeyStore(): InMemoryApiKeyStore;
/**
 * Reset the API key store singleton (for testing)
 */
export declare function resetApiKeyStore(): void;
/**
 * Store type configuration
 */
export type ApiKeyStoreType = 'memory' | 'database';
/**
 * Configuration options for API key store factory
 */
export interface ApiKeyStoreFactoryOptions {
    /** Override store type (default: from environment) */
    storeType?: ApiKeyStoreType;
    /** Redis client for rate limiting (optional) */
    redis?: Redis | null;
    /** Database instance (optional, for testing) */
    database?: Database;
}
/**
 * Determine the store type based on environment configuration
 */
export declare function getStoreType(): ApiKeyStoreType;
/**
 * Factory function to create the appropriate API key store.
 *
 * In production/staging environments, this returns a database-backed store.
 * In development/test environments, this returns an in-memory store.
 *
 * The store type can be explicitly set via VORION_API_KEY_STORE environment variable:
 * - 'database': Use PostgreSQL database
 * - 'memory': Use in-memory store
 *
 * @param options - Configuration options
 * @returns Promise resolving to an API key store instance
 *
 * @example
 * ```typescript
 * // Get default store based on environment
 * const store = await createApiKeyStoreFactory();
 *
 * // Force database store with Redis for rate limiting
 * const store = await createApiKeyStoreFactory({
 *   storeType: 'database',
 *   redis: redisClient,
 * });
 *
 * // Force in-memory store for testing
 * const store = await createApiKeyStoreFactory({
 *   storeType: 'memory',
 * });
 * ```
 */
export declare function createApiKeyStoreFactory(options?: ApiKeyStoreFactoryOptions): Promise<IApiKeyStore>;
/**
 * Get the API key store singleton using the factory pattern.
 *
 * This is the recommended way to get the API key store in production code.
 * It automatically selects the appropriate store type based on environment.
 *
 * @param options - Configuration options (only used on first call)
 * @returns Promise resolving to the API key store singleton
 */
export declare function getApiKeyStoreFactory(options?: ApiKeyStoreFactoryOptions): Promise<IApiKeyStore>;
/**
 * Reset the factory-created API key store singleton (for testing)
 */
export declare function resetApiKeyStoreFactory(): void;
//# sourceMappingURL=store.d.ts.map