"""Steering method definitions: TETNO, GROM, PRZELOM."""

from wisent.core.constants import (
    TIKHONOV_REG,
    TETNO_CONDITION_THRESHOLD, TETNO_GATE_TEMPERATURE,
    TETNO_ENTROPY_FLOOR, TETNO_ENTROPY_CEILING, TETNO_MAX_ALPHA,
    TETNO_OPTIMIZATION_STEPS, TETNO_LEARNING_RATE,
    GROM_NUM_DIRECTIONS, GROM_OPTIMIZATION_STEPS, GROM_LEARNING_RATE,
    GROM_BEHAVIOR_WEIGHT, GROM_RETAIN_WEIGHT, GROM_SPARSE_WEIGHT,
    GROM_MAX_ALPHA,
    PRZELOM_EPSILON, PRZELOM_INFERENCE_K,
    DEFAULT_STRENGTH,
)
from wisent.core.steering_methods.registry import (
    SteeringMethodDefinition,
    SteeringMethodParameter,
    SteeringMethodType,
)


TETNO_DEFINITION = SteeringMethodDefinition(
    name="tetno",
    method_type=SteeringMethodType.TETNO,
    description="TETNO - Probabilistic Uncertainty-guided Layer Steering Engine. Layer-adaptive conditional steering with uncertainty-guided intensity.",
    method_class_path="wisent.core.steering_methods.methods.tetno.TETNOMethod",
    parameters=[
        SteeringMethodParameter(
            name="sensor_layer",
            type=int,
            default=None,
            help="Layer index where condition gating is computed (auto-computed if not set)",
            cli_flag="--tetno-sensor-layer",
        ),
        SteeringMethodParameter(
            name="steering_layers",
            type=str,
            default=None,
            help="Comma-separated layer indices where steering is applied (auto-computed if not set)",
            cli_flag="--tetno-steering-layers",
        ),
        SteeringMethodParameter(
            name="per_layer_scaling",
            type=bool,
            default=True,
            help="Learn different scaling per layer",
            action="store_true",
            cli_flag="--tetno-per-layer-scaling",
        ),
        SteeringMethodParameter(
            name="condition_threshold",
            type=float,
            default=TETNO_CONDITION_THRESHOLD,
            help="Threshold for condition activation (0-1)",
            cli_flag="--tetno-condition-threshold",
        ),
        SteeringMethodParameter(
            name="gate_temperature",
            type=float,
            default=TETNO_GATE_TEMPERATURE,
            help="Temperature for sigmoid gating (lower = sharper)",
            cli_flag="--tetno-gate-temperature",
        ),
        SteeringMethodParameter(
            name="learn_threshold",
            type=bool,
            default=True,
            help="Learn optimal threshold via grid search",
            action="store_true",
            cli_flag="--tetno-learn-threshold",
        ),
        SteeringMethodParameter(
            name="use_entropy_scaling",
            type=bool,
            default=True,
            help="Enable entropy-based intensity modulation",
            action="store_true",
            cli_flag="--tetno-use-entropy-scaling",
        ),
        SteeringMethodParameter(
            name="entropy_floor",
            type=float,
            default=TETNO_ENTROPY_FLOOR,
            help="Minimum entropy to trigger scaling",
            cli_flag="--tetno-entropy-floor",
        ),
        SteeringMethodParameter(
            name="entropy_ceiling",
            type=float,
            default=TETNO_ENTROPY_CEILING,
            help="Entropy at which max_alpha is reached",
            cli_flag="--tetno-entropy-ceiling",
        ),
        SteeringMethodParameter(
            name="max_alpha",
            type=float,
            default=TETNO_MAX_ALPHA,
            help="Maximum steering strength",
            cli_flag="--tetno-max-alpha",
        ),
        SteeringMethodParameter(
            name="optimization_steps",
            type=int,
            default=TETNO_OPTIMIZATION_STEPS,
            help="Steps for condition vector optimization",
            cli_flag="--tetno-optimization-steps",
        ),
        SteeringMethodParameter(
            name="learning_rate",
            type=float,
            default=TETNO_LEARNING_RATE,
            help="Learning rate for optimization",
            cli_flag="--tetno-learning-rate",
        ),
        SteeringMethodParameter(
            name="normalize",
            type=bool,
            default=True,
            help="L2-normalize vectors",
            action="store_true",
            cli_flag="--tetno-normalize",
        ),
    ],
    optimization_config={
        "strength_search_range": (0.1, 3.0),
        "default_strength": DEFAULT_STRENGTH,
        "threshold_search_range": (0.0, 1.0),
    },
    default_strength=DEFAULT_STRENGTH,
    strength_range=(0.1, 3.0),
)


GROM_DEFINITION = SteeringMethodDefinition(
    name="grom",
    method_type=SteeringMethodType.GROM,
    description="GROM - Total Integrated Targeted Activation Navigation. Joint optimization of manifold, gating, and intensity.",
    method_class_path="wisent.core.steering_methods.methods.grom.GROMMethod",
    parameters=[
        SteeringMethodParameter(
            name="num_directions",
            type=int,
            default=GROM_NUM_DIRECTIONS,
            help="Number of directions per layer in the steering manifold",
            cli_flag="--grom-num-directions",
        ),
        SteeringMethodParameter(
            name="steering_layers",
            type=str,
            default=None,
            help="Comma-separated layer indices for steering (auto-computed if not set)",
            cli_flag="--grom-steering-layers",
        ),
        SteeringMethodParameter(
            name="sensor_layer",
            type=int,
            default=None,
            help="Primary layer for gating decisions (auto-computed if not set)",
            cli_flag="--grom-sensor-layer",
        ),
        SteeringMethodParameter(
            name="gate_hidden_dim",
            type=int,
            default=None,
            help="Hidden dimension for gating network (auto-computed as hidden_dim//16 if not set)",
            cli_flag="--grom-gate-hidden-dim",
        ),
        SteeringMethodParameter(
            name="intensity_hidden_dim",
            type=int,
            default=None,
            help="Hidden dimension for intensity network (auto-computed as hidden_dim//32 if not set)",
            cli_flag="--grom-intensity-hidden-dim",
        ),
        SteeringMethodParameter(
            name="optimization_steps",
            type=int,
            default=GROM_OPTIMIZATION_STEPS,
            help="Total optimization steps",
            cli_flag="--grom-optimization-steps",
        ),
        SteeringMethodParameter(
            name="learning_rate",
            type=float,
            default=GROM_LEARNING_RATE,
            help="Learning rate for all components",
            cli_flag="--grom-learning-rate",
        ),
        SteeringMethodParameter(
            name="behavior_weight",
            type=float,
            default=GROM_BEHAVIOR_WEIGHT,
            help="Weight for behavior effectiveness loss",
            cli_flag="--grom-behavior-weight",
        ),
        SteeringMethodParameter(
            name="retain_weight",
            type=float,
            default=GROM_RETAIN_WEIGHT,
            help="Weight for retain loss (minimize side effects)",
            cli_flag="--grom-retain-weight",
        ),
        SteeringMethodParameter(
            name="sparse_weight",
            type=float,
            default=GROM_SPARSE_WEIGHT,
            help="Weight for sparsity loss",
            cli_flag="--grom-sparse-weight",
        ),
        SteeringMethodParameter(
            name="max_alpha",
            type=float,
            default=GROM_MAX_ALPHA,
            help="Maximum steering intensity",
            cli_flag="--grom-max-alpha",
        ),
        SteeringMethodParameter(
            name="normalize",
            type=bool,
            default=True,
            help="L2-normalize directions",
            action="store_true",
            cli_flag="--grom-normalize",
        ),
    ],
    optimization_config={
        "strength_search_range": (0.1, 5.0),
        "default_strength": 1.0,
        "num_directions_range": (3, 10),
    },
    default_strength=1.0,
    strength_range=(0.1, 5.0),
)


PRZELOM_DEFINITION = SteeringMethodDefinition(
    name="przelom",
    method_type=SteeringMethodType.PRZELOM,
    description="Attention-Transport steering - computes desired transport plan via EOT and inverts through Q-projection pseudoinverse.",
    method_class_path="wisent.core.steering_methods.methods.przelom.PrzelomMethod",
    parameters=[
        SteeringMethodParameter(
            name="epsilon",
            type=float,
            default=1.0,
            help="Entropic regularization (softmax temperature) for transport plan",
            cli_flag="--przelom-epsilon",
        ),
        SteeringMethodParameter(
            name="target_mode",
            type=str,
            default="uniform",
            help="Target transport plan: uniform or nearest",
            cli_flag="--przelom-target-mode",
            choices=["uniform", "nearest"],
        ),
        SteeringMethodParameter(
            name="regularization",
            type=float,
            default=TIKHONOV_REG,
            help="Tikhonov regularization for pseudoinverse",
            cli_flag="--przelom-regularization",
        ),
        SteeringMethodParameter(
            name="inference_k",
            type=int,
            default=5,
            help="Nearest source points for inference interpolation",
            cli_flag="--przelom-inference-k",
        ),
    ],
    optimization_config={
        "strength_search_range": (0.1, 3.0),
        "default_strength": 1.0,
    },
    default_strength=1.0,
    strength_range=(0.1, 3.0),
)
