/**
 * Types for the general operation management system
 */

import type { Logger, MongoDBClient } from '@stock-bot/types';

/**
 * Configuration for a single operation
 */
export interface OperationConfig {
  /** Unique name for the operation within a provider */
  name: string;
  /** Type of operation determining tracking behavior */
  type: 'standard' | 'incremental' | 'crawl' | 'snapshot' | 'intraday_crawl';
  /** Human-readable description */
  description?: string;
  /** Default hours before operation is considered stale */
  defaultStaleHours?: number;
  /** Priority for scheduling (higher = more important) */
  priority?: number;
  /** Whether operation requires a finished flag (for crawl operations) */
  requiresFinishedFlag?: boolean;
  /** Custom metadata for provider-specific needs */
  metadata?: Record<string, any>;
}

/**
 * Status of an operation for a specific symbol
 */
export interface OperationStatus {
  /** Last time the operation was run (regardless of success) */
  lastRunAt: Date;
  /** Last time the operation completed successfully */
  lastSuccessAt?: Date;
  /** Current status of the operation */
  status: 'success' | 'failure' | 'partial' | 'running';
  /** Number of records processed in last run */
  recordCount?: number;
  /** Date of the most recent record */
  lastRecordDate?: Date;
  /** Error message if status is failure */
  error?: string;
  /** Provider-specific metadata */
  metadata?: Record<string, any>;
  
  // Crawl-specific fields (used for crawl and intraday_crawl operations)
  /** Whether the crawl has completed */
  finished?: boolean;
  /** Oldest date reached during crawl */
  oldestDateReached?: Date;
  /** Newest date reached during crawl */
  newestDateReached?: Date;
  /** Last date that was processed (for resumption) */
  lastProcessedDate?: Date;
  /** Total days processed so far */
  totalDaysProcessed?: number;
  /** Direction of last crawl */
  lastCrawlDirection?: 'forward' | 'backward';
  /** Target oldest date to reach */
  targetOldestDate?: Date;
}

// CrawlState interface removed - fields moved directly to OperationStatus

/**
 * Configuration for a data provider
 */
export interface ProviderConfig {
  /** Unique name for the provider (e.g., 'qm', 'ceo') */
  name: string;
  /** MongoDB collection name for symbols */
  collectionName: string;
  /** Field name used as symbol identifier (e.g., 'qmSearchCode', 'symbol') */
  symbolField: string;
  /** Optional description */
  description?: string;
}

/**
 * Data for updating an operation status
 */
export interface OperationUpdate {
  /** Status of the operation */
  status: 'success' | 'failure' | 'partial';
  /** Date of the most recent record */
  lastRecordDate?: Date;
  /** Number of records processed */
  recordCount?: number;
  /** Error message for failures */
  error?: string;
  /** Additional metadata */
  metadata?: Record<string, any>;
  
  // Crawl-specific update fields
  /** Whether the crawl has completed */
  finished?: boolean;
  /** Oldest date reached during crawl */
  oldestDateReached?: Date;
  /** Newest date reached during crawl */
  newestDateReached?: Date;
  /** Last date that was processed (for resumption) */
  lastProcessedDate?: Date;
  /** Total days processed so far */
  totalDaysProcessed?: number;
  /** Direction of last crawl */
  lastCrawlDirection?: 'forward' | 'backward';
  /** Target oldest date to reach */
  targetOldestDate?: Date;
}

/**
 * Options for querying stale symbols
 */
export interface StaleSymbolOptions {
  /** Symbols not run since this date */
  notRunSince?: Date;
  /** Minimum hours since last successful run */
  minHoursSinceRun?: number;
  /** Maximum number of symbols to return */
  limit?: number;
  /** Symbols to exclude from results */
  excludeSymbols?: string[];
  /** Only include active symbols */
  activeOnly?: boolean;
  /** Filter for specific symbols */
  symbolFilter?: { symbol?: string };
}

/**
 * Options for bulk operations
 */
export interface BulkOperationUpdate {
  /** Symbol identifier */
  symbol: string;
  /** Operation name */
  operation: string;
  /** Update data */
  data: OperationUpdate;
}

/**
 * Statistics for an operation
 */
export interface OperationStats {
  /** Total symbols in collection */
  totalSymbols: number;
  /** Symbols that have been processed at least once */
  processedSymbols: number;
  /** Symbols that need processing based on stale hours */
  staleSymbols: number;
  /** Symbols with successful last run */
  successfulSymbols: number;
  /** Symbols with failed last run */
  failedSymbols: number;
  /** For crawl operations: number of finished crawls */
  finishedCrawls?: number;
  /** Average records per symbol */
  avgRecordsPerSymbol?: number;
  /** Additional provider-specific stats */
  customStats?: Record<string, any>;
}

/**
 * Symbol with operation data
 */
export interface SymbolWithOperations {
  /** Symbol identifier value */
  symbol: string;
  /** Last record date for the operation */
  lastRecordDate?: Date;
  /** Full operation status */
  operationStatus?: OperationStatus;
  /** Additional symbol data */
  metadata?: Record<string, any>;
}

/**
 * Constructor options for operation components
 */
export interface OperationComponentOptions {
  mongodb: MongoDBClient;
  logger: Logger;
}