"""Runtime adjuster — threshold/weight adjustment and feedback recording.

Reuses:
    - ``databridge.detection.record_feedback()``
    - ``src.detection.rule_optimizer.suggest_weight_updates()``
    - ``src.evaluation.feedback_loop.run_cycle()``
"""

from __future__ import annotations

from typing import Any, Dict, List, Optional

from databridge_core.sdk.core.context import SDKContext
from databridge_core.sdk.core.exceptions import NotImplementedStubError


class RuntimeAdjuster:
    """Adjust deployed solution parameters at runtime.

    Allows enterprise clients to tune detection thresholds,
    record feedback, and trigger self-learning cycles without
    redeploying.

    Reuses:
        - ``databridge.detection.record_feedback(finding_id, confirmed, ...)``
        - ``src.detection.rule_optimizer.suggest_weight_updates()``
        - ``src.detection.rule_optimizer.apply_updates()``
        - ``src.evaluation.feedback_loop.run_cycle()``

    Usage::

        adjuster = RuntimeAdjuster(ctx=ctx)
        adjuster.record_feedback(finding_id="f_123", confirmed=True)
        suggestions = adjuster.suggest_threshold_updates()
        adjuster.trigger_learning_cycle()
    """

    def __init__(self, ctx: SDKContext):
        self._ctx = ctx

    def record_feedback(
        self,
        finding_id: str,
        confirmed: bool,
        notes: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Record feedback on a detection finding.

        Reuse: ``databridge.detection.record_feedback(
            finding_id, confirmed, notes, feedback_path
        )``
        """
        raise NotImplementedStubError(
            "RuntimeAdjuster",
            "record_feedback",
            "databridge.detection.record_feedback(finding_id, confirmed, notes)",
        )

    def suggest_threshold_updates(
        self,
        min_samples: int = 10,
    ) -> Dict[str, Any]:
        """Suggest detection weight/threshold updates based on feedback.

        Reuse: ``src.detection.rule_optimizer.suggest_weight_updates(
            feedback_path, min_samples
        )``
        """
        raise NotImplementedStubError(
            "RuntimeAdjuster",
            "suggest_threshold_updates",
            "src.detection.rule_optimizer.suggest_weight_updates(feedback_path, min_samples)",
        )

    def apply_threshold_updates(
        self,
        updates: Dict[str, float],
    ) -> Dict[str, Any]:
        """Apply suggested threshold updates.

        Reuse: ``src.detection.rule_optimizer.apply_updates(updates, knowledge_dir)``
        """
        raise NotImplementedStubError(
            "RuntimeAdjuster",
            "apply_threshold_updates",
            "src.detection.rule_optimizer.apply_updates(updates, knowledge_dir)",
        )

    def trigger_learning_cycle(
        self,
        max_iterations: int = 3,
    ) -> Dict[str, Any]:
        """Trigger a self-learning cycle.

        Runs the 7-step feedback loop: collect -> analyze -> tune ->
        validate -> calibrate -> save -> report.

        Reuse: ``src.evaluation.feedback_loop.run_cycle(CycleConfig(...))``
        """
        raise NotImplementedStubError(
            "RuntimeAdjuster",
            "trigger_learning_cycle",
            "src.evaluation.feedback_loop.run_cycle(CycleConfig(max_iterations))",
        )

    def memorize_weights(
        self,
        weights: Dict[str, float],
        reason: str = "",
    ) -> Dict[str, Any]:
        """Memorize detection weights to the knowledge base.

        Reuse: ``databridge.detection.memorize_weights(weights, reason, knowledge_dir)``
        """
        raise NotImplementedStubError(
            "RuntimeAdjuster",
            "memorize_weights",
            "Via MCP tool memorize_detection_weights(weights, reason)",
        )
