import type { ObjectId } from 'mongodb';

/**
 * MongoDB Client Configuration
 */
export interface MongoDBClientConfig {
  host: string;
  port: number;
  database: string;
  username?: string;
  password?: string;
  authSource?: string;
  uri?: string;
  poolSettings?: {
    maxPoolSize: number;
    minPoolSize: number;
    maxIdleTime: number;
  };
  timeouts?: {
    connectTimeout: number;
    socketTimeout: number;
    serverSelectionTimeout: number;
  };
  tls?: {
    enabled: boolean;
    insecure: boolean;
    caFile?: string;
  };
  options?: {
    retryWrites: boolean;
    journal: boolean;
    readPreference: 'primary' | 'primaryPreferred' | 'secondary' | 'secondaryPreferred' | 'nearest';
    writeConcern: string;
  };
}

/**
 * MongoDB Connection Options
 */
export interface MongoDBConnectionOptions {
  retryAttempts?: number;
  retryDelay?: number;
  healthCheckInterval?: number;
}

export interface PoolMetrics {
  totalConnections: number;
  activeConnections: number;
  idleConnections: number;
  waitingRequests: number;
  errors: number;
  lastError?: string;
  avgResponseTime?: number;
  created: Date;
  lastUsed?: Date;
}

export interface ConnectionEvents {
  onConnect?: () => void | Promise<void>;
  onDisconnect?: () => void | Promise<void>;
  onError?: (error: Error) => void | Promise<void>;
  onPoolCreated?: () => void | Promise<void>;
}

export interface DynamicPoolConfig {
  enabled: boolean;
  minSize: number;
  maxSize: number;
  scaleUpThreshold: number; // % of pool in use (0-100)
  scaleDownThreshold: number; // % of pool idle (0-100)
  scaleUpIncrement: number; // connections to add
  scaleDownIncrement: number; // connections to remove
  evaluationInterval: number; // ms between checks
}

/**
 * Base Document Interface
 */
export interface DocumentBase {
  _id?: ObjectId;
  created_at: Date;
  updated_at: Date;
  source: string;
  metadata?: Record<string, unknown>;
}

/**
 * Sentiment Data Document
 */
export interface SentimentData extends DocumentBase {
  symbol: string;
  sentiment_score: number;
  sentiment_label: 'positive' | 'negative' | 'neutral';
  confidence: number;
  text: string;
  source_type: 'reddit' | 'twitter' | 'news' | 'forums';
  source_id: string;
  timestamp: Date;
  processed_at: Date;
  language: string;
  keywords: string[];
  entities: Array<{
    name: string;
    type: string;
    confidence: number;
  }>;
}

/**
 * Raw Document
 */
export interface RawDocument extends DocumentBase {
  document_type: 'html' | 'pdf' | 'text' | 'json' | 'xml';
  content: string;
  content_hash: string;
  url?: string;
  title?: string;
  author?: string;
  published_date?: Date;
  extracted_text?: string;
  processing_status: 'pending' | 'processed' | 'failed';
  size_bytes: number;
  language?: string;
}

/**
 * News Article
 */
export interface NewsArticle extends DocumentBase {
  headline: string;
  content: string;
  summary?: string;
  author: string;
  publication: string;
  published_date: Date;
  url: string;
  symbols: string[];
  categories: string[];
  sentiment_score?: number;
  relevance_score?: number;
  image_url?: string;
  tags: string[];
}

/**
 * SEC Filing
 */
export interface SecFiling extends DocumentBase {
  cik: string;
  accession_number: string;
  filing_type: string;
  company_name: string;
  symbols: string[];
  filing_date: Date;
  period_end_date: Date;
  url: string;
  content: string;
  extracted_data?: Record<string, unknown>;
  financial_statements?: Array<{
    statement_type: string;
    data: Record<string, number>;
  }>;
  processing_status: 'pending' | 'processed' | 'failed';
}

/**
 * Earnings Transcript
 */
export interface EarningsTranscript extends DocumentBase {
  symbol: string;
  company_name: string;
  quarter: string;
  year: number;
  call_date: Date;
  transcript: string;
  participants: Array<{
    name: string;
    title: string;
    type: 'executive' | 'analyst';
  }>;
  key_topics: string[];
  sentiment_analysis?: {
    overall_sentiment: number;
    topic_sentiments: Record<string, number>;
  };
  financial_highlights?: Record<string, number>;
}

/**
 * Analyst Report
 */
export interface AnalystReport extends DocumentBase {
  symbol: string;
  analyst_firm: string;
  analyst_name: string;
  report_title: string;
  report_date: Date;
  rating: 'buy' | 'hold' | 'sell' | 'strong_buy' | 'strong_sell';
  price_target?: number;
  previous_rating?: string;
  content: string;
  summary: string;
  key_points: string[];
  financial_projections?: Record<string, number>;
}

/**
 * Exchange-related types
 */
export interface ExchangeSourceMapping {
  id: string;
  name: string;
  code?: string;
  aliases?: string[];
  lastUpdated: Date;
}

export interface MasterExchange extends DocumentBase {
  masterExchangeId: string;
  shortName?: string;
  officialName: string;
  country: string;
  currency: string;
  timezone: string;
  active?: boolean;

  tradingHours?: {
    open: string;
    close: string;
    timezone: string;
  };

  sourceMappings: Record<string, ExchangeSourceMapping>;
  confidence: number;
  verified: boolean;
}
