"""Parameter flow management — stub for trial compatibility."""

import json
from pathlib import Path
from typing import Any, Dict, Optional

import config_manager

SUGGESTION_FILE: Path = config_manager.get_config_dir() / "params_suggestion.json"


def extract_param_map(payload: Dict[str, Any]) -> Dict[str, Any]:
    """Extract a flat parameter map from a structured payload."""
    if not isinstance(payload, dict):
        return {}
    suggestions = payload.get("suggestions", [])
    if not isinstance(suggestions, list):
        return {}
    out: Dict[str, Any] = {}
    for s in suggestions:
        if isinstance(s, dict) and "param" in s:
            out[s["param"]] = s.get("applied_value")
    return out


def get_effective_rollback_state() -> Dict[str, Any]:
    """Return the rollback state for current effective params."""
    return {"available": False, "reason": "no previous snapshot"}


def complete_effective_review() -> None:
    """Mark the effective params review as complete."""


def rollback_effective_params() -> Dict[str, Any]:
    """Rollback effective params to previous snapshot."""
    return {"rolled_back": False, "reason": "no previous snapshot available"}


def build_shadow_payload(*args: Any, **kwargs: Any) -> Dict[str, Any]:
    """Build a shadow payload for parameter evaluation."""
    return {}


def write_json(payload: Any, path: Path) -> None:
    """Write a JSON payload to disk atomically."""
    p = Path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    p.write_text(json.dumps(payload, indent=2, ensure_ascii=False), encoding="utf-8")


def promote_suggestion_to_effective(suggestion_file: Optional[Path] = None) -> Dict[str, Any]:
    """Promote a params suggestion to the effective params payload."""
    return {"promoted": True, "file": str(suggestion_file or SUGGESTION_FILE)}


def reject_current_suggestion() -> None:
    """Reject and archive the current params suggestion."""
