"""Skill injection for Boris — matches task text to relevant skills from boris-tui archive.

Thin wrapper that imports from boris_tui if available, otherwise uses a built-in fallback.
"""

import sys
import os


def _try_import_from_boris_tui():
    """Try to import the full skill matcher from boris-tui package."""
    try:
        from boris_tui.skill_matcher import match_skills, format_skills_block, get_skills_summary
        return match_skills, format_skills_block, get_skills_summary
    except ImportError:
        return None, None, None


# Try importing from boris_tui first
_match, _format, _summary = _try_import_from_boris_tui()

if _match is None:
    # boris-tui not installed — add its parent to sys.path and retry
    _alt_path = os.path.normpath(os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        "..",  # All Folders
        "boris-tui",
    ))
    if os.path.isdir(_alt_path) and _alt_path not in sys.path:
        sys.path.insert(0, _alt_path)
        _match, _format, _summary = _try_import_from_boris_tui()


def inject_skills(task_text: str, agent: str = "boris", token_budget: int = 2000) -> str:
    """Match skills to task text and return formatted skill block for prompt injection.

    Returns empty string if no skills match or if the skills archive is unavailable.
    """
    if _match is None or _format is None:
        return ""

    try:
        skills = _match(task_text, token_budget=token_budget, agent=agent)
        return _format(skills)
    except Exception:
        return ""


def get_matched_skill_names(task_text: str, agent: str = "boris") -> str:
    """Get comma-separated list of matched skill names for display."""
    if _match is None or _summary is None:
        return ""

    try:
        skills = _match(task_text, agent=agent)
        return _summary(skills)
    except Exception:
        return ""
