# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""new.py — Skill-Generator für /skill-new.

Legt .skills/<name>/ mit SKILL.md, eval/test_basic.md und (optional)
hook.yaml an. Trägt Skill-Eintrag im manifest.json ein.
"""
import argparse
import json
from datetime import date
from pathlib import Path

SKILL_TEMPLATE = """---
name: {name}
description: "TODO: Skill-Beschreibung nach Formel (Was + Inhalt + Anwendung)"
type: skill
agent_ready: true
schema_version: 1
tags:
  - skill
scope: vault
bundle_skill: false
default_active:
  allgemein: true
trigger_words:
{trigger_yaml}
allowed_tools_claude: []
depends_on: []
conflicts_with: []
created: {today}
updated: {today}
---

# {name}

TODO: Fließtext-Absatz unter H1, kein Doppel der Description.

## Workflow

TODO: Schritte des Skills, tool-neutral formuliert.

## Verknüpfungen

- TODO: mind. ein Wikilink (Pflicht)
"""

EVAL_TEMPLATE = """# Eval: Basic Trigger

**Input:** TODO
**Erwartung:** TODO
**Bestanden wenn:** TODO
"""


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--vault", type=Path, default=Path.cwd())
    parser.add_argument("--name", required=True)
    parser.add_argument("--type", choices=["skill", "hook"], default="skill")
    parser.add_argument("--triggers", default="", help="comma-separated")
    args = parser.parse_args()

    skill_dir = args.vault / ".skills" / args.name
    skill_dir.mkdir(parents=True, exist_ok=False)
    (skill_dir / "eval").mkdir()

    triggers = [t.strip() for t in args.triggers.split(",") if t.strip()]
    # YAML-konformes Quoting: doppelte Anführungszeichen, eingebettete " escapen.
    # repr() würde Python-String-Quotes (single) und Backslash-Escape-Sequenzen produzieren,
    # die kein YAML-Parser interpretiert wie ein Python-Interpreter.
    trigger_yaml = (
        "\n".join(f'  - "{t.replace(chr(34), chr(92) + chr(34))}"' for t in triggers)
        if triggers
        else "  []"
    )

    (skill_dir / "SKILL.md").write_text(
        SKILL_TEMPLATE.format(
            name=args.name,
            trigger_yaml=trigger_yaml,
            today=date.today().isoformat(),
        )
    )
    (skill_dir / "eval" / "test_basic.md").write_text(EVAL_TEMPLATE)

    # manifest.json updaten
    manifest_path = args.vault / ".skills" / "manifest.json"
    if manifest_path.exists():
        manifest = json.loads(manifest_path.read_text())
        manifest.setdefault("skills", {})[args.name] = {
            "scope": "vault",
            "bundle_skill": False,
            "default_active": {"allgemein": True},
            "depends_on": [],
            "conflicts_with": [],
        }
        manifest_path.write_text(json.dumps(manifest, indent=2, ensure_ascii=False))

    print(f"Skill-Skelett angelegt: {skill_dir}")
    print("Vergiss nicht: SKILL.md ausformulieren, eval/test_basic.md füllen, manifest.json prüfen")


if __name__ == "__main__":
    main()
