"""Blueprints: shareable plain-language automations layered on skills + cron.

A blueprint is NOT a new object type: it is an ordinary skill whose frontmatter declares
``metadata.hermes.blueprint`` (``schedule`` required; optional ``deliver`` [default "origin"],
``prompt``, ``no_agent``, ``model``, ``provider``, ``enabled_toolsets``), so it rides the whole
skills-hub pipeline for free. This module only bridges that block to cron ``create_job()``.
"""

from __future__ import annotations

import logging
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Dict, List, Optional

logger = logging.getLogger(__name__)

__all__ = [
    "BlueprintError",
    "BlueprintSpec",
    "blueprint_spec_for_installed",
    "blueprint_to_job_spec",
    "parse_blueprint",
    "register_blueprint_suggestion",
]


class BlueprintError(ValueError):
    """Raised when a blueprint block is present but malformed."""


@dataclass
class BlueprintSpec:
    """Parsed ``metadata.hermes.blueprint`` automation spec for a skill."""

    skill_name: str
    schedule: str
    deliver: str = "origin"
    prompt: Optional[str] = None
    no_agent: bool = False
    model: Optional[str] = None
    provider: Optional[str] = None
    enabled_toolsets: Optional[list[str]] = None
    raw: dict[str, Any] = field(default_factory=dict)


def _split_frontmatter(text: str) -> Optional[dict[str, Any]]:
    """Return the parsed YAML frontmatter mapping, or None if absent/invalid."""
    if not isinstance(text, str):
        return None
    stripped = text.lstrip("\ufeff").lstrip()  # BOM is not whitespace; strip explicitly
    if not stripped.startswith("---") or (end := stripped.find("\n---", 3)) == -1:
        return None
    try:
        import hermes_yaml as yaml

        data = yaml.safe_load(stripped[3:end])
    except Exception as e:  # pragma: no cover - malformed YAML
        logger.debug("blueprint: frontmatter YAML parse failed: %s", e)
        return None
    return data if isinstance(data, dict) else None


def parse_blueprint(skill_md_text: str) -> Optional[BlueprintSpec]:
    """Extract a BlueprintSpec from a SKILL.md string, or None if not a blueprint.

    A skill is a blueprint iff ``metadata.hermes.blueprint`` is a mapping with a
    non-empty ``schedule``. Raises BlueprintError if the block exists but is
    structurally invalid (so a typo surfaces instead of silently no-op'ing).
    """
    fm = _split_frontmatter(skill_md_text)
    if not fm:
        return None

    meta = fm.get("metadata")
    hermes = meta.get("hermes") if isinstance(meta, dict) else None
    blueprint = hermes.get("blueprint") if isinstance(hermes, dict) else None
    if blueprint is None:
        return None
    if not isinstance(blueprint, dict):
        raise BlueprintError("metadata.hermes.blueprint must be a mapping")

    schedule = str(blueprint.get("schedule", "")).strip()
    if not schedule:
        raise BlueprintError("blueprint.schedule is required and must be non-empty")

    prompt, model, provider = blueprint.get("prompt"), blueprint.get("model"), blueprint.get("provider")
    toolsets = blueprint.get("enabled_toolsets")
    if toolsets is not None and not isinstance(toolsets, list):
        raise BlueprintError("blueprint.enabled_toolsets must be a list when present")

    return BlueprintSpec(
        skill_name=str(fm.get("name", "")).strip(), schedule=schedule,
        deliver=str(blueprint.get("deliver", "origin")).strip() or "origin",
        prompt=str(prompt) if prompt is not None else None,
        no_agent=bool(blueprint.get("no_agent", False)),
        model=str(model).strip() if model else None,
        provider=str(provider).strip() if provider else None,
        enabled_toolsets=[str(t) for t in toolsets] if toolsets else None,
        raw=blueprint,
    )


def blueprint_spec_for_installed(skill_name: str) -> Optional[BlueprintSpec]:
    """Find ``<skill_name>/SKILL.md`` anywhere in the skills tree and parse its
    blueprint block; None when not found or not a blueprint."""
    try:
        from tools.skills_hub import SKILLS_DIR
    except Exception:  # pragma: no cover - import guard
        return None
    # Skills live at skills/<category>/<name>/SKILL.md or skills/<name>/SKILL.md.
    for path in Path(SKILLS_DIR).glob(f"**/{skill_name}/SKILL.md"):
        try:
            spec = parse_blueprint(path.read_text(encoding="utf-8-sig"))
        except OSError:
            continue
        if spec is not None:
            spec.skill_name = spec.skill_name or skill_name  # frontmatter name wins over dir name
            return spec
    return None


def blueprint_to_job_spec(spec: BlueprintSpec, *, name: Optional[str] = None) -> dict[str, Any]:
    """``cron.jobs.create_job`` kwargs for a spec."""
    return {
        "prompt": spec.prompt, "schedule": spec.schedule, "name": name or f"blueprint:{spec.skill_name}",
        "deliver": spec.deliver, "skills": [spec.skill_name] if spec.skill_name else None,
        "model": spec.model, "provider": spec.provider, "enabled_toolsets": spec.enabled_toolsets,
        "no_agent": spec.no_agent,
    }


def register_blueprint_suggestion(spec: BlueprintSpec) -> Optional[dict[str, Any]]:
    """Register an installed blueprint as a Suggested Cron Job (never auto-scheduled;
    the user accepts or dismisses it). None when skipped (seen/dismissed/backlog full)."""
    if not spec.skill_name:
        return None
    try:
        from cron.suggestions import add_suggestion
    except Exception:  # pragma: no cover - import guard
        return None

    deliver = f", delivering to {spec.deliver}" if spec.deliver and spec.deliver != "origin" else ""
    return add_suggestion(
        title=f"Schedule '{spec.skill_name}'",
        description=f"The '{spec.skill_name}' blueprint runs on schedule {spec.schedule}{deliver}.",
        source="blueprint",
        job_spec=blueprint_to_job_spec(spec),
        dedup_key=f"blueprint:{spec.skill_name}:{spec.schedule}",
    )
