/**
 * TradingView Charting Library type definitions
 * Based on TradingView DataFeed API
 */

export interface LibrarySymbolInfo {
  name: string
  ticker?: string
  description: string
  type: string
  session: string
  timezone: string
  exchange: string
  minmov: number
  pricescale: number
  has_intraday: boolean
  has_daily: boolean
  has_weekly_and_monthly: boolean
  supported_resolutions: string[]
  volume_precision?: number
  data_status?: 'streaming' | 'endofday' | 'pulsed' | 'delayed_streaming'
  [key: string]: any
}

export interface Bar {
  time: number  // Unix timestamp in milliseconds
  open: number
  high: number
  low: number
  close: number
  volume?: number
}

export interface HistoryMetadata {
  noData: boolean
  nextTime?: number
}

export interface SearchSymbolResultItem {
  symbol: string
  full_name: string
  description: string
  exchange: string
  ticker?: string
  type: string
}

export interface DatafeedConfiguration {
  supported_resolutions: string[]
  supports_search?: boolean
  supports_group_request?: boolean
  supports_marks?: boolean
  supports_timescale_marks?: boolean
  supports_time?: boolean
  exchanges?: Array<{ value: string; name: string; desc: string }>
  symbols_types?: Array<{ name: string; value: string }>
}

export interface IBasicDataFeed {
  onReady(callback: (configuration: DatafeedConfiguration) => void): void

  searchSymbols(
    userInput: string,
    exchange: string,
    symbolType: string,
    onResult: (items: SearchSymbolResultItem[]) => void
  ): void

  resolveSymbol(
    symbolName: string,
    onResolve: (symbolInfo: LibrarySymbolInfo) => void,
    onError: (reason: string) => void
  ): void

  getBars(
    symbolInfo: LibrarySymbolInfo,
    resolution: string,
    periodParams: {
      from: number
      to: number
      firstDataRequest: boolean
      countBack?: number
    },
    onResult: (bars: Bar[], meta: HistoryMetadata) => void,
    onError: (reason: string) => void
  ): void

  subscribeBars(
    symbolInfo: LibrarySymbolInfo,
    resolution: string,
    onTick: (bar: Bar) => void,
    listenerGuid: string,
    onResetCacheNeededCallback: () => void
  ): void

  unsubscribeBars(listenerGuid: string): void
}

export interface ChartingLibraryWidgetOptions {
  symbol: string
  datafeed: IBasicDataFeed
  interval: string
  container: string | HTMLElement
  library_path: string
  locale?: string
  disabled_features?: string[]
  enabled_features?: string[]
  charts_storage_url?: string
  charts_storage_api_version?: string
  client_id?: string
  user_id?: string
  fullscreen?: boolean
  autosize?: boolean
  theme?: 'Light' | 'Dark'
  timezone?: string
  debug?: boolean
  [key: string]: any
}

export interface IChartingLibraryWidget {
  onChartReady(callback: () => void): void
  headerReady(): Promise<void>
  remove(): void
  activeChart(): any
  [key: string]: any
}

// Global TradingView namespace
declare global {
  interface Window {
    TradingView: {
      widget: new (options: ChartingLibraryWidgetOptions) => IChartingLibraryWidget
      version: () => string
    }
  }
}
