use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MaParamKind {
    Float,
    Int,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaParamInfo {
    pub key: &'static str,
    pub label: &'static str,
    pub kind: MaParamKind,
    pub default: f64,
    pub min: Option<f64>,
    pub max: Option<f64>,
    pub step: Option<f64>,
    pub notes: Option<&'static str>,
}

pub fn ma_param_schema(ma_type: &str) -> &'static [MaParamInfo] {
    match ma_type.trim().to_ascii_lowercase().as_str() {
        "alma" => &[
            MaParamInfo {
                key: "offset",
                label: "Offset",
                kind: MaParamKind::Float,
                default: 0.85,
                min: Some(0.0),
                max: Some(1.0),
                step: Some(0.01),
                notes: None,
            },
            MaParamInfo {
                key: "sigma",
                label: "Sigma",
                kind: MaParamKind::Float,
                default: 6.0,
                min: Some(0.1),
                max: Some(64.0),
                step: Some(0.1),
                notes: None,
            },
        ],
        "corrected_moving_average" | "cma" => &[],
        "n_order_ema" => &[MaParamInfo {
            key: "order",
            label: "Order",
            kind: MaParamKind::Int,
            default: 1.0,
            min: Some(1.0),
            max: Some(64.0),
            step: Some(1.0),
            notes: Some("Additional enum params: ema_style and iir_style."),
        }],
        "epma" => &[MaParamInfo {
            key: "offset",
            label: "Offset",
            kind: MaParamKind::Int,
            default: 4.0,
            min: Some(0.0),
            max: Some(512.0),
            step: Some(1.0),
            notes: Some("Must be < period."),
        }],
        "sgf" => &[MaParamInfo {
            key: "poly_order",
            label: "Poly Order",
            kind: MaParamKind::Int,
            default: 2.0,
            min: Some(0.0),
            max: Some(16.0),
            step: Some(1.0),
            notes: Some("Must be an integer >= 0 and < effective period."),
        }],
        "gaussian" => &[MaParamInfo {
            key: "poles",
            label: "Poles",
            kind: MaParamKind::Int,
            default: 4.0,
            min: Some(1.0),
            max: Some(4.0),
            step: Some(1.0),
            notes: Some("Valid range: 1..4."),
        }],
        "highpass2" | "highpass_2_pole" => &[MaParamInfo {
            key: "k",
            label: "K",
            kind: MaParamKind::Float,
            default: 0.707,
            min: Some(0.0001),
            max: Some(10.0),
            step: Some(0.001),
            notes: Some("Must be finite and > 0."),
        }],
        "jma" => &[
            MaParamInfo {
                key: "phase",
                label: "Phase",
                kind: MaParamKind::Float,
                default: 50.0,
                min: Some(-100.0),
                max: Some(100.0),
                step: Some(1.0),
                notes: Some("Typical range: -100..100."),
            },
            MaParamInfo {
                key: "power",
                label: "Power",
                kind: MaParamKind::Int,
                default: 2.0,
                min: Some(1.0),
                max: Some(16.0),
                step: Some(1.0),
                notes: Some("Must be an integer >= 1."),
            },
        ],
        "tilson" => &[MaParamInfo {
            key: "volume_factor",
            label: "Volume Factor",
            kind: MaParamKind::Float,
            default: 0.0,
            min: Some(-10.0),
            max: Some(10.0),
            step: Some(0.1),
            notes: Some("Must be finite."),
        }],
        "ema_deviation_corrected_t3" => &[
            MaParamInfo {
                key: "hot",
                label: "Hot",
                kind: MaParamKind::Float,
                default: 0.7,
                min: Some(-16.0),
                max: Some(16.0),
                step: Some(0.01),
                notes: Some("T3 hot coefficient. Must be finite."),
            },
            MaParamInfo {
                key: "t3_mode",
                label: "T3 Mode",
                kind: MaParamKind::Int,
                default: 0.0,
                min: Some(0.0),
                max: Some(1.0),
                step: Some(1.0),
                notes: Some("0 = T3 New, 1 = T3 Original."),
            },
        ],
        "wave_smoother" => &[MaParamInfo {
            key: "phase",
            label: "Phase",
            kind: MaParamKind::Float,
            default: 70.0,
            min: Some(0.0),
            max: Some(119.0),
            step: Some(0.1),
            notes: Some("Wave phase in degrees. Valid range: 0..119."),
        }],
        "maaq" => &[
            MaParamInfo {
                key: "fast_period",
                label: "Fast Period",
                kind: MaParamKind::Int,
                default: 2.0,
                min: Some(1.0),
                max: Some(512.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
            MaParamInfo {
                key: "slow_period",
                label: "Slow Period",
                kind: MaParamKind::Int,
                default: 30.0,
                min: Some(1.0),
                max: Some(2048.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
        ],
        "ehlers_itrend" => &[MaParamInfo {
            key: "warmup_bars",
            label: "Warmup Bars",
            kind: MaParamKind::Int,
            default: 20.0,
            min: Some(1.0),
            max: Some(2048.0),
            step: Some(1.0),
            notes: Some("Must be an integer > 0."),
        }],
        "ehlers_ecema" => &[MaParamInfo {
            key: "gain_limit",
            label: "Gain Limit",
            kind: MaParamKind::Int,
            default: 50.0,
            min: Some(1.0),
            max: Some(2048.0),
            step: Some(1.0),
            notes: Some("Must be an integer > 0."),
        }],
        "ehlers_undersampled_double_moving_average" => &[
            MaParamInfo {
                key: "fast_length",
                label: "Fast Length",
                kind: MaParamKind::Int,
                default: 6.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
            MaParamInfo {
                key: "slow_length",
                label: "Slow Length",
                kind: MaParamKind::Int,
                default: 12.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
            MaParamInfo {
                key: "sample_length",
                label: "Sample Length",
                kind: MaParamKind::Int,
                default: 5.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
        ],
        "elastic_volume_weighted_moving_average" => &[
            MaParamInfo {
                key: "length",
                label: "Length",
                kind: MaParamKind::Int,
                default: 30.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Used when use_volume_sum is 1."),
            },
            MaParamInfo {
                key: "absolute_volume_millions",
                label: "Absolute Volume Millions",
                kind: MaParamKind::Float,
                default: 134.0,
                min: Some(0.000001),
                max: Some(1000000000.0),
                step: Some(0.001),
                notes: Some("Used when use_volume_sum is 0."),
            },
            MaParamInfo {
                key: "use_volume_sum",
                label: "Use Volume Sum",
                kind: MaParamKind::Int,
                default: 0.0,
                min: Some(0.0),
                max: Some(1.0),
                step: Some(1.0),
                notes: Some("0 = absolute volume, 1 = rolling volume sum."),
            },
        ],
        "frama" => &[
            MaParamInfo {
                key: "sc",
                label: "SC",
                kind: MaParamKind::Int,
                default: 300.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Smoothing constant (must be > 0)."),
            },
            MaParamInfo {
                key: "fc",
                label: "FC",
                kind: MaParamKind::Int,
                default: 1.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Fast constant (must be > 0)."),
            },
        ],
        "volatility_adjusted_ma" | "vama" => &[MaParamInfo {
            key: "vol_period",
            label: "Vol Period",
            kind: MaParamKind::Int,
            default: 51.0,
            min: Some(1.0),
            max: Some(4096.0),
            step: Some(1.0),
            notes: Some("Must be an integer > 0."),
        }],
        "sama" => &[
            MaParamInfo {
                key: "maj_length",
                label: "Maj Length",
                kind: MaParamKind::Int,
                default: 14.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
            MaParamInfo {
                key: "min_length",
                label: "Min Length",
                kind: MaParamKind::Int,
                default: 6.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
        ],
        "vpwma" => &[MaParamInfo {
            key: "power",
            label: "Power",
            kind: MaParamKind::Float,
            default: 0.382,
            min: Some(-8.0),
            max: Some(8.0),
            step: Some(0.001),
            notes: Some("Must be finite. Integer 0..8 can be faster."),
        }],
        "dma" => &[
            MaParamInfo {
                key: "ema_length",
                label: "EMA Length",
                kind: MaParamKind::Int,
                default: 20.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
            MaParamInfo {
                key: "ema_gain_limit",
                label: "EMA Gain Limit",
                kind: MaParamKind::Int,
                default: 50.0,
                min: Some(1.0),
                max: Some(4096.0),
                step: Some(1.0),
                notes: Some("Must be an integer > 0."),
            },
        ],
        "cora_wave" => &[
            MaParamInfo {
                key: "r_multi",
                label: "R Multi",
                kind: MaParamKind::Float,
                default: 2.0,
                min: Some(0.0),
                max: Some(16.0),
                step: Some(0.001),
                notes: Some("Must be finite and >= 0."),
            },
            MaParamInfo {
                key: "smooth",
                label: "Smooth",
                kind: MaParamKind::Int,
                default: 1.0,
                min: Some(0.0),
                max: Some(1.0),
                step: Some(1.0),
                notes: Some("0 = off, 1 = on (default)."),
            },
        ],
        _ => &[],
    }
}
