from __future__ import annotations

import argparse
import subprocess
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
DEFAULT_INDEX = ROOT / "plugins" / "1c" / "datasets" / "prepared" / "rag_index.json"

REQUIRED_PHRASES = [
    "Не выдумывай метаданные 1С",
    "запроси метаданные через инструмент",
    "источники",
    "Какие реквизиты есть у справочника Номенклатура?",
]


def main() -> int:
    parser = argparse.ArgumentParser(description="Check that the 1C RAG prompt contains safety-critical instructions.")
    parser.add_argument("--index", type=Path, default=DEFAULT_INDEX)
    args = parser.parse_args()

    command = [
        sys.executable,
        "scripts/ask_1c_rag.py",
        "Какие реквизиты есть у справочника Номенклатура?",
        "--index",
        str(args.index),
        "--print-prompt",
    ]
    result = subprocess.run(
        command,
        cwd=ROOT,
        check=False,
        text=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    if result.returncode != 0:
        print(result.stderr, file=sys.stderr)
        return result.returncode

    missing = [phrase for phrase in REQUIRED_PHRASES if phrase not in result.stdout]
    if missing:
        print("1C RAG prompt check failed. Missing phrases:", file=sys.stderr)
        for phrase in missing:
            print(f"- {phrase}", file=sys.stderr)
        return 1

    print("1C RAG prompt check passed.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
