// Main type declarations for @wengine-ai/llms

// Re-export plugin declarations (pluginManager, tokenSpeedPlugin, etc.)
export * from "./plugins";

// Health store types
export interface ProviderHealthState {
  provider: string;
  model: string;
  status: 'closed' | 'open' | 'half-open';
  failureCount: number;
  successCount: number;
  lastFailureTime: number;
  lastProbeTime: number;
  lastError?: string;
}

export interface HealthPoolConfig {
  enabled?: boolean;
  failureThreshold?: number;
  probeIntervalMinutes?: number;
  halfOpenSuccessThreshold?: number;
}

export class ProviderHealthStore {
  constructor(config?: HealthPoolConfig);
  recordSuccess(provider: string, model: string): void;
  recordFailure(provider: string, model: string, error?: string): void;
  getState(provider: string, model: string): ProviderHealthState | undefined;
  isAvailable(provider: string, model: string): boolean;
  getPriority(provider: string, model: string): number;
  needsProbe(state: ProviderHealthState): boolean;
  markProbeAttempt(provider: string, model: string): void;
  clear(): void;
  getAllStates(): ProviderHealthState[];
  getFailPoolModels(): string[];
  getHalfOpenModels(): string[];
  stopProbeTimer(): void;
}

export declare function getHealthStore(config?: HealthPoolConfig): ProviderHealthStore;
export declare function resetHealthStore(): void;

// Core exports
export interface RouterScenarioType {
  'default': 'default';
  'background': 'background';
  'think': 'think';
  'longContext': 'longContext';
  'extendedContext': 'extendedContext';
  'webSearch': 'webSearch';
  'modelMapping': 'modelMapping';
  'image': 'image';
}

export interface RouterFallbackConfig {
  default?: string[];
  background?: string[];
  think?: string[];
  longContext?: string[];
  webSearch?: string[];
  modelMapping?: string[];
  image?: string[];
}

export class ConfigService {
  get<T>(key: string): T | undefined;
  getAll(): Record<string, any>;
}

export class ProviderService {
  getProvider(name: string): any | undefined;
}

export class TransformerService {
  getTransformersWithEndpoint(): Array<{ transformer: any }>;
}

export class TokenizerService {
  countTokens(request: any, config?: any): Promise<{ tokenCount: number }>;
}

export function router(req: any, reply: any, context: any): Promise<void>;
export function calculateTokenCount(messages: any[], system: any, tools: any[]): number;
export function searchProjectBySession(sessionId: string): Promise<string | null>;

// Session cache
export const sessionUsageCache: any;

// SSE utilities
export class SSEParserTransform {}
export class SSESerializerTransform {}
export function rewriteStream(stream: any, handler: any): any;

// Rate limit types
export interface RateLimitInfo {
  provider: string;
  remaining: number | null;
  limit: number | null;
  reset: number | null;
  capturedAt: number;
}

export declare function getAllRateLimitInfo(): RateLimitInfo[];
export declare function getRateLimitInfo(providerName: string): RateLimitInfo | undefined;
export declare function initRateLimitPersistence(): void;

// Active probe types
export interface ActiveProbeConfig {
  enabled?: boolean;
  quotaProbeIntervalMinutes?: number;
  probeTimeoutMs?: number;
  initialDelayMs?: number;
  excludeProviders?: string[];
}

export interface LLMProvider {
  name: string;
  baseUrl: string;
  apiKey: string;
  models: string[];
  transformer?: any;
}

export class ActiveProbeService {
  constructor(
    getProviders: () => LLMProvider[],
    config?: ActiveProbeConfig,
    getHttpsProxy?: () => string | undefined,
    logger?: any
  );
  start(): void;
  stop(): void;
  probeProviderManually(providerName: string): Promise<boolean>;
}

export declare function getActiveProbeService(
  getProviders: () => LLMProvider[],
  config?: ActiveProbeConfig,
  getHttpsProxy?: () => string | undefined,
  logger?: any
): ActiveProbeService;

export declare function startActiveProbe(
  getProviders: () => LLMProvider[],
  config?: ActiveProbeConfig,
  getHttpsProxy?: () => string | undefined,
  logger?: any
): ActiveProbeService;

export declare function stopActiveProbe(): void;
export declare function resetActiveProbeService(): void;

// Runtime debug log toggle
export declare function setRuntimeDebugLog(enabled: boolean): void;
export declare function getRuntimeDebugLog(): boolean;

// Quota
export interface QuotaAdapter {
  checkQuota(providerName: string, model: string): Promise<boolean>;
}
export interface ProviderQuotaResult {
  totalBalance?: number;
  usedBalance?: number;
  limitDaily?: number;
  usedDailyBalance?: number;
}
export interface StoredQuotaResult extends ProviderQuotaResult {
  updatedAt: number;
}
export declare function getQuotaAdapter(providerName: string): QuotaAdapter | undefined;
export declare function getAllQuotaResults(): Record<string, StoredQuotaResult>;
export declare function getQuotaResult(providerName: string): StoredQuotaResult | undefined;
export declare function storeQuotaResult(providerName: string, result: ProviderQuotaResult): void;
export declare function initQuotaStorePersistence(): void;
