from __future__ import annotations

from typing import Any

try:
    from .loop_nodes import (
        accumulator_delete_entries,
        accumulator_list_entries,
        accumulator_move_entry,
        accumulator_retake_entry,
        accumulator_save_entries,
    )
    from .prompt_builder import save_character_profile_payload
except ImportError:  # Allows local smoke tests from the repository root.
    from loop_nodes import (
        accumulator_delete_entries,
        accumulator_list_entries,
        accumulator_move_entry,
        accumulator_retake_entry,
        accumulator_save_entries,
    )
    from prompt_builder import save_character_profile_payload


def _payload(payload: Any) -> dict[str, Any]:
    return payload if isinstance(payload, dict) else {}


def profile_save_cached_payload(payload: Any) -> dict[str, Any]:
    data = _payload(payload)
    return save_character_profile_payload(
        profile_name=str(data.get("profile_name") or ""),
        profile_json=data.get("profile_json") or "",
    )


def accumulator_list_payload(payload: Any) -> dict[str, Any]:
    data = _payload(payload)
    return accumulator_list_entries(
        str(data.get("store_key") or ""),
        preview_limit=int(data.get("preview_limit") or 0),
    )


def accumulator_delete_payload(payload: Any) -> dict[str, Any]:
    data = _payload(payload)
    return accumulator_delete_entries(
        store_key=str(data.get("store_key") or ""),
        preview_key=str(data.get("preview_key") or ""),
        entry_id=str(data.get("entry_id") or ""),
        index=int(data.get("index") or 0),
        clear=bool(data.get("clear")),
        preview_limit=int(data.get("preview_limit") or 0),
    )


def accumulator_save_payload(payload: Any) -> dict[str, Any]:
    data = _payload(payload)
    return accumulator_save_entries(
        store_key=str(data.get("store_key") or ""),
        save_path=str(data.get("save_path") or "sxcp_accumulator"),
        filename_prefix=str(data.get("filename_prefix") or "sxcp_accum"),
        clear_after_save=bool(data.get("clear_after_save")),
        preview_limit=int(data.get("preview_limit") or 0),
    )


def accumulator_move_payload(payload: Any) -> dict[str, Any]:
    data = _payload(payload)
    return accumulator_move_entry(
        store_key=str(data.get("store_key") or ""),
        preview_key=str(data.get("preview_key") or ""),
        entry_id=str(data.get("entry_id") or ""),
        index=int(data.get("index") or 0),
        direction=str(data.get("direction") or "up"),
        target_index=int(data.get("target_index") or 0),
        preview_limit=int(data.get("preview_limit") or 0),
    )


def accumulator_retake_payload(payload: Any) -> dict[str, Any]:
    data = _payload(payload)
    return accumulator_retake_entry(
        store_key=str(data.get("store_key") or ""),
        preview_key=str(data.get("preview_key") or ""),
        entry_id=str(data.get("entry_id") or ""),
        index=int(data.get("index") or 0),
    )
