"""Control handles — proxy objects for Action → ActionSet/AgentQueue access.

Actions interact with their parent containers through these handles
instead of direct references, enforcing read/write permission boundaries.
See design doc §3.4.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from oedix.core.action import Action
    from oedix.core.action_set import ActionSet
    from oedix.core.context import Context


class ActionSetHandle:
    """Proxy giving an Action limited control over its owning ActionSet.

    Read: subject, approach, history, remaining_actions.
    Write: set_approach, set_weight, enqueue, enqueue_many.
    """

    def __init__(self, action_set: ActionSet) -> None:
        self._set = action_set

    # -- Read --

    def get_subject(self) -> str:
        return self._set.subject

    def get_title(self) -> str:
        return self._set.title

    def get_approach(self) -> str:
        return self._set.approach

    def get_history(self) -> list[Action]:
        return list(self._set.history)

    def remaining_actions(self) -> int:
        return self._set.remaining_actions()

    # -- Write --

    def set_approach(self, text: str) -> None:
        self._set.set_approach(text)

    def set_title(self, text: str) -> None:
        self._set.set_title(text)

    def set_weight(self, value: float) -> None:
        self._set.set_weight(value)

    def enqueue(self, action: Action) -> None:
        self._set.enqueue(action)

    def enqueue_many(self, actions: list[Action]) -> None:
        self._set.enqueue_many(actions)

    def set_batch_remaining(self, count: int) -> None:
        self._set._batch_remaining = count


class AgentQueueHandle:
    """Proxy giving an Action limited control over the owning AgentQueue.

    Read: query, context, history (read-only), queue (read-only),
          remaining_sets.
    Write: set_context, append_context, enqueue, insert, remove,
           replace_queue.
    """

    def __init__(self, agent_queue) -> None:
        self._q = agent_queue

    # -- Read --

    def get_query(self) -> str:
        return self._q.query

    def get_context(self) -> list[Context]:
        return self._q.get_context()

    def get_history(self) -> list:
        return list(self._q.history)

    def get_queue(self) -> list:
        return list(self._q.queue)

    def remaining_sets(self) -> int:
        return self._q.remaining_sets()

    # -- Write --

    def set_context(self, contexts: list[Context]) -> None:
        self._q.set_context(contexts)

    def append_context(self, context: Context) -> None:
        self._q.append_context(context)

    def enqueue(self, action_set: ActionSet) -> None:
        self._q.enqueue(action_set)

    def insert(self, index: int, action_set: ActionSet) -> None:
        self._q.insert(index, action_set)

    def remove(self, index: int) -> None:
        self._q.remove(index)

    def replace_queue(self, action_sets: list[ActionSet]) -> None:
        self._q.replace_queue(action_sets)
