from typing import List
from .playbooks.store import playbook_store, PlaybookItem


class PlaybookManager:
    @classmethod
    def _all(cls) -> List[PlaybookItem]:
        """Return all playbooks sorted by order (position 0 is always main)."""
        return playbook_store.all_playbooks()

    @classmethod
    def get_main_playbook(cls) -> PlaybookItem | None:
        playbooks = cls._all()
        return playbooks[0] if playbooks else None

    @classmethod
    def get_context_playbooks(cls) -> List[PlaybookItem]:
        """All playbooks after the first — injected as reference context."""
        playbooks = cls._all()
        return playbooks[1:] if len(playbooks) > 1 else []

    @classmethod
    def get_system_prompt(cls) -> str:
        playbook = cls.get_main_playbook()
        if not playbook:
            return ""
        goal = (getattr(playbook, "goal", "") or "").strip()
        instructions = (getattr(playbook, "instructions", "") or "").strip()
        if goal and instructions:
            return f"{goal}\n\n{instructions}"
        return goal or instructions