import { defineStore } from 'pinia'
import { ref } from 'vue'

export interface CustomIndicatorParam {
  type: 'int' | 'float' | 'bool' | 'string'
  default: any
  description?: string
  min?: number
  max?: number
}

/**
 * Per-series plot configuration.
 * style maps to LineStudyPlotStyle: 0=Line, 1=Histogram, 3=Dots/Cross,
 * 4=Area, 5=Columns, 6=Circles, 9=StepLine.
 */
export interface PlotConfig {
  style: number
  color?: string
  linewidth?: number
  visible?: boolean
}

/** Shaded region between two plots ("plot_plot") or two bands ("hline_hline"). */
export interface FilledAreaConfig {
  id: string
  type: 'plot_plot' | 'hline_hline'
  series1: string
  series2: string
  color?: string
  opacity?: number
}

/** Horizontal reference line (e.g. RSI ob/os). linestyle: 0=solid, 1=dotted, 2=dashed. */
export interface BandConfig {
  id: string
  value: number
  color?: string
  linewidth?: number
  linestyle?: number
  visible?: boolean
}

export interface CustomIndicatorColumn {
  name: string
  display_name?: string
  description?: string
  plot?: PlotConfig
}

export interface CustomIndicatorMetadata {
  display_name: string
  parameters: Record<string, CustomIndicatorParam>
  input_series: string[]
  output_columns: CustomIndicatorColumn[]
  pane: 'price' | 'separate'
  filled_areas?: FilledAreaConfig[]
  bands?: BandConfig[]
}

export interface IndicatorInstance {
  id: string
  pandas_ta_name: string
  instance_name: string
  parameters: Record<string, any>
  tv_study_id?: string
  tv_indicator_name?: string
  tv_inputs?: Record<string, any>
  visible: boolean
  pane: string
  symbol?: string
  created_at?: number
  modified_at?: number
  original_id?: string
  /** Populated for custom_ indicators; drives TV custom study auto-construction. */
  custom_metadata?: CustomIndicatorMetadata
}

export const useIndicatorStore = defineStore('indicators', () => {
  const indicators = ref<Record<string, IndicatorInstance>>({})

  // Helper methods
  const addIndicator = (indicator: IndicatorInstance) => {
    indicators.value[indicator.id] = indicator
  }

  const updateIndicator = (id: string, updates: Partial<IndicatorInstance>) => {
    if (indicators.value[id]) {
      const updated = {
        ...indicators.value[id],
        ...updates,
        modified_at: Math.floor(Date.now() / 1000)
      }
      indicators.value[id] = updated
    }
  }

  const removeIndicator = (id: string) => {
    delete indicators.value[id]
  }

  const getIndicator = (id: string): IndicatorInstance | undefined => {
    return indicators.value[id]
  }

  const getAllIndicators = (): IndicatorInstance[] => {
    return Object.values(indicators.value)
  }

  const getIndicatorsBySymbol = (symbol: string): IndicatorInstance[] => {
    return Object.values(indicators.value).filter(ind => ind.symbol === symbol)
  }

  return {
    indicators,
    addIndicator,
    updateIndicator,
    removeIndicator,
    getIndicator,
    getAllIndicators,
    getIndicatorsBySymbol
  }
})
