export interface Subscription {
  id: number;
  instrument_id: number;
  sub_kind: string;
  active: boolean;
  created_at: string;
  updated_at: string;
  exchange: string;
  base_asset: string;
  quote_asset: string;
}

export interface SubscriptionSimple {
  id: number;
  instrument_id: number;
  sub_kind: string;
  active: boolean;
  created_at: string;
  updated_at: string;
}

export interface CreateSubscriptionRequest {
  exchange: string;
  pair: string;
  sub_kinds: string[];
}

export interface InstrumentStats {
  instrument_id: number;
  exchange_name: string;
  base_asset: string;
  quote_asset: string;
  kind: string;
  trade_count_total: number;
  trade_count_1h: number;
  l1_count_total: number;
  l1_count_1h: number;
  latest_trade_time: string | null;
  latest_l1_time: string | null;
  estimated_storage_bytes: number;
}

export interface Exchange {
  name: string;
  supported_sub_kinds: string[];
  supports_instrument_lookup: boolean;
}

export interface InstrumentPair {
  base: string;
  quote: string;
}

export interface IngestConfig {
  id: number;
  instrument_id: number;
  source: string;
  source_type: string;
  retention_days: number;
  /** null = unlimited (no purge, full history backfill) */
  candle_retention_days: number | null;
  active: boolean;
  last_ingested_date: string | null;
  candle_intervals: string[];
  created_at: string;
  updated_at: string;
  exchange: string;
  base_asset: string;
  quote_asset: string;
}

export interface CreateIngestConfigRequest {
  exchange: string;
  pair: string;
  source?: string;
  source_type?: string;
  retention_days?: number;
  /** null = unlimited candle retention */
  candle_retention_days?: number | null;
  candle_intervals?: string[];
}

export interface UpdateIngestConfigRequest {
  active?: boolean;
  retention_days?: number;
  candle_intervals?: string[];
  /** Set a specific candle retention in days */
  candle_retention_days?: number;
  /** Set true to switch candle retention to unlimited (clears the day count) */
  candle_retention_unlimited?: boolean;
}

export interface DataRange {
  start: string;
  end: string;
}

export interface PaperRunPosition {
  id: number;
  run_id: string;
  seq: number;
  side: string;
  price_entry: string;
  price_exit: string;
  quantity: string;
  pnl: string;
  fees_enter: string;
  fees_exit: string;
  time_enter: string;
  time_exit: string;
}

export interface PaperRunPositionsResponse {
  total: number;
  sampled: number;
  positions: PaperRunPosition[];
}

export interface CreatePaperRunRequest {
  mode?: 'live' | 'backtest';
  config: Record<string, unknown>;
  starts_at: string;
  finishes_at: string;
  risk_free_return: number;
  candle_interval?: string | null;
}

export interface PaperRun {
  id: string;
  status: 'queued' | 'running' | 'complete' | 'failed' | 'cancelled';
  mode: 'live' | 'backtest';
  config: unknown;
  starts_at: string;
  finishes_at: string;
  risk_free_return: string;
  candle_interval: string | null;
  result_summary: unknown | null;
  error_message: string | null;
  worker_id: string | null;
  created_at: string;
  started_at: string | null;
  completed_at: string | null;
  strategy_id: string | null;
}

export interface Strategy {
  id: string;
  hash: string;
  config: unknown;
  hash_input: unknown;
  created_at: string;
  name: string | null;
  version: number;
  description: string | null;
}

export interface CreateStrategyRequest {
  config: unknown;
  name?: string;
  description?: string;
}

export interface CandleCoverageEntry {
  interval: string;
  first_open: string;
  last_close: string;
  count: number;
}

export interface BackfillCandlesRequest {
  exchange: string;
  symbol: string;
  interval: string;
  from: string;
  to: string;
}

export interface BackfillCandlesResponse {
  inserted: number;
  interval: string;
  from: string;
  to: string;
}

export interface CandleEntry {
  open_time: string;
  close_time: string;
  open: number;
  high: number;
  low: number;
  close: number;
  volume: number;
}

export interface PaperRunCandlesResponse {
  interval: string;
  total: number;
  candles: CandleEntry[];
}
