// ═══════════════════════════════════════════════════════════════════════
// Strategy Customization — Type Definitions
// ═══════════════════════════════════════════════════════════════════════

/** 参数类型枚举 */
export type ParamType =
  | "float"
  | "int"
  | "bool"
  | "string_choice"
  | "float_range";

/** 风险等级 */
export type RiskLevel = "low" | "medium" | "high" | "critical";

/** 参数 schema — 单个参数的完整元数据 */
export interface ParamDef {
  key: string;
  label: string;
  description: string;
  type: ParamType;
  default: unknown;
  group: string;
  subgroup: string;
  order: number;
  min_val: number | null;
  max_val: number | null;
  step: number | null;
  choices: string[];
  unit: string;
  visible_when: string | null;
  risk_level: RiskLevel;
  requires_paper_mode: boolean;
  advanced: boolean;
}

/** 参数分组元数据 */
export interface ParamGroupMeta {
  key: string;
  label: string;
  icon: string;
  description: string;
  order: number;
}

/** 带参数列表的分组 */
export interface ParamGroup extends ParamGroupMeta {
  params: ParamDef[];
}

/** 参数 schema 响应 */
export interface ParamsSchemaResponse {
  success: boolean;
  schema: ParamDef[];
  groups: Record<string, ParamGroup>;
  error?: string;
}

/** 策略当前参数 */
export interface StrategyParamsResponse {
  success: boolean;
  strategy_id: string;
  params: Record<string, unknown>;
  error?: string;
}

/** 单个参数更新 */
export interface ParamUpdate {
  key: string;
  value: unknown;
}

/** 批量参数更新请求 */
export interface ParamsBatchUpdate {
  strategy_id: string;
  params: ParamUpdate[];
  change_note: string;
  bypass_paper_mode: boolean;
}

/** 参数校验摘要 */
export interface ParamsSummary {
  total_params: number;
  low_risk: number;
  medium_risk: number;
  high_risk: number;
  critical_risk: number;
  groups_touched: string[];
  requires_paper_mode: boolean;
}

/** 参数校验响应 */
export interface ParamsValidateResponse {
  success: boolean;
  errors: Record<string, string[]>;
  summary: ParamsSummary;
  param_count: number;
  error?: string;
}

/** 参数应用响应 */
export interface ParamsApplyResponse {
  success: boolean;
  message: string;
  strategy_id: string;
  new_version: string;
  summary: ParamsSummary;
  requires_paper_mode?: boolean;
  paper_mode_required_for?: string[];
  validation_errors?: Record<string, string[]>;
  error?: string;
}

/** 策略版本历史条目 */
export interface StrategyVersion {
  version: string;
  note: string;
  published_at: string;
  params_changed?: string[];
  summary?: ParamsSummary;
}

/** 策略版本历史响应 */
export interface StrategyHistoryResponse {
  success: boolean;
  strategy_id: string;
  current_version: string;
  versions: StrategyVersion[];
  error?: string;
}

/** 策略对比差异 */
export interface StrategyDiffResponse {
  success: boolean;
  strategy_a: string;
  strategy_b: string;
  diff_count: number;
  diff: Record<string, { a: unknown; b: unknown }>;
  error?: string;
}

/** 策略 Market State 中的单条记录 */
export interface StrategyMarketEntry {
  display_name?: string;
  enabled: boolean;
  is_custom?: boolean;
  is_composite?: boolean;
  base_engine?: string;
  default_mode?: string;
  trust_level?: string;
  current_version: string;
  versions: StrategyVersion[];
  template: Record<string, unknown>;
  components?: string[];
  description?: string;
  created_at?: string;
  capabilities?: string[];
}

/** 运行时实例 */
export interface RuntimeInstance {
  instance_id: string;
  strategy_id: string;
  strategy_name: string;
  strategy_version: string;
  allocation: number;
  status: string;
  role: string;
  config_revision: string;
  param_overrides: Record<string, unknown>;
  template_snapshot: Record<string, unknown>;
  deployed_at?: string;
  updated_at?: string;
  metadata: Record<string, unknown>;
}

/** 策略健康评分 */
export interface InstanceHealth {
  instance_id: string;
  strategy_id: string;
  score: number;
  grade: string;
  win_rate_score: number;
  profit_factor_score: number;
  consistency_score: number;
  regime_fit_score: number;
  recency_score: number;
  total_trades: number;
  win_rate: number;
  profit_factor: number;
  loss_streak: number;
  sharpe_20: number;
  total_pnl: number;
  suggested_action: string;
  suggested_scale: number;
  rehabilitation_progress: number;
  best_regime: string;
  best_regime_win_rate: number;
  current_regime_fitness: number;
  primary_loss_pattern: string;
}

/** 风险预算响应 */
export interface RiskBudgetResponse {
  success: boolean;
  health_scores: Record<string, InstanceHealth>;
  voting_weights: Record<string, number>;
  risk_budget: Record<string, number>;
  error?: string;
}

/** 预设条目 */
export interface StrategyPreset {
  id: string;
  name: string;
  description: string;
  strategy_id: string;
  saved_at: string;
}

/** 预设列表响应 */
export interface PresetsListResponse {
  success: boolean;
  presets: StrategyPreset[];
  error?: string;
}

/** 预设保存请求 */
export interface PresetSaveRequest {
  name: string;
  description: string;
  strategy_id: string;
  params_snapshot?: Record<string, unknown>;
}

/** 策略克隆请求 */
export interface CloneRequest {
  strategy_id_from: string;
  new_name: string;
  change_note?: string;
}

/** 策略工作台响应 */
export interface StrategyWorkbenchResponse {
  success: boolean;
  marketplace: {
    strategyTemplates: StrategyMarketEntry[];
    runtimeInstances: RuntimeInstance[];
    orchestration: Record<string, unknown>;
    current: Record<string, unknown>;
  };
  error?: string;
}

/** 参数变更快照（用于 undo） */
export interface ParamChangeSnapshot {
  prevParams: Record<string, unknown>;
  newParams: Record<string, unknown>;
  timestamp: number;
  changeNote: string;
}
