#!/usr/bin/env python3
"""Small, local-contract teacher -> SFT dataset helper.

Default mode is offline validation. Network access exists only behind the
explicit ``--generate`` flag, and only to the user-supplied OpenAI-compatible
base URL. The script stores the teacher's visible answer, never hidden
reasoning or the raw response.
"""
from __future__ import annotations

import argparse
import hashlib
import json
import os
import sys
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime, timezone
from pathlib import Path
from typing import Any

MAX_SAMPLES_LIMIT = 8
MAX_TOKENS_LIMIT = 512


def fail(message: str) -> None:
    raise SystemExit("FAIL-CLOSED: " + message)


def read_rows(path: Path) -> list[dict[str, Any]]:
    if not path.is_file():
        fail(f"missing input JSONL: {path}")
    rows: list[dict[str, Any]] = []
    for line_no, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1):
        if not line.strip():
            continue
        try:
            row = json.loads(line)
        except json.JSONDecodeError as exc:
            fail(f"{path}:{line_no}: invalid JSON: {exc.msg}")
        if not isinstance(row, dict) or not isinstance(row.get("messages"), list):
            fail(f"{path}:{line_no}: expected an object with messages")
        if not row.get("id") or not isinstance(row["id"], str):
            fail(f"{path}:{line_no}: missing string id")
        if not isinstance(row.get("expected"), str) or not row["expected"]:
            fail(f"{path}:{line_no}: expected literal is required for filtering")
        for message in row["messages"]:
            if not isinstance(message, dict) or message.get("role") not in {"system", "user"}:
                fail(f"{path}:{line_no}: teacher prompts may contain only system/user messages")
            if not isinstance(message.get("content"), str):
                fail(f"{path}:{line_no}: message content must be text")
        rows.append(row)
    if not rows:
        fail("input JSONL is empty")
    return rows


def check_answer(row: dict[str, Any], answer: str, finish_reason: str) -> tuple[bool, str]:
    if finish_reason != "stop":
        return False, f"finish_reason={finish_reason!r}, expected 'stop'"
    if not isinstance(answer, str) or not answer.strip():
        return False, "teacher returned empty content"
    if answer != row["expected"]:
        return False, "answer did not match the literal expected checker"
    return True, "accepted"


def input_hash(row: dict[str, Any]) -> str:
    canonical = json.dumps(row["messages"], ensure_ascii=False, sort_keys=True, separators=(",", ":"))
    return hashlib.sha256(canonical.encode("utf-8")).hexdigest()


def validate(rows: list[dict[str, Any]], max_samples: int) -> dict[str, Any]:
    checked = rows[:max_samples]
    accepted = 0
    rejected: list[dict[str, str]] = []
    for row in checked:
        ok, reason = check_answer(row, row.get("candidate", ""), row.get("finish_reason", "stop"))
        if ok:
            accepted += 1
        else:
            rejected.append({"id": row["id"], "reason": reason})
    return {"mode": "validate", "checked": len(checked), "accepted": accepted, "rejected": rejected}


def endpoint(base: str) -> str:
    parsed=urllib.parse.urlsplit(base)
    if parsed.scheme not in ('http','https') or not parsed.hostname:
        fail("--base must be an explicit http(s) OpenAI-compatible endpoint")
    if parsed.username or parsed.password or parsed.query or parsed.fragment:
        fail("Do not put credentials, queries or fragments in --base; use ATLAS_TEACHER_KEY")
    if parsed.scheme=='http' and parsed.hostname not in ('localhost','127.0.0.1','::1'):
        fail("Use HTTPS remotely or a localhost SSH tunnel; never transmit the key over remote plain HTTP")
    return base.rstrip("/") + "/chat/completions" if not base.rstrip("/").endswith("/chat/completions") else base.rstrip("/")


def call_teacher(url: str, key: str, model: str, messages: list[dict[str, str]], max_tokens: int) -> tuple[str, str, dict[str, Any]]:
    body = json.dumps({"model": model, "messages": messages, "max_tokens": max_tokens, "stream": False}).encode("utf-8")
    request = urllib.request.Request(url, data=body, method="POST", headers={
        "Content-Type": "application/json", "Authorization": "Bearer " + key,
    })
    try:
        with urllib.request.urlopen(request, timeout=60) as response:
            payload = json.loads(response.read().decode("utf-8"))
    except (urllib.error.URLError, TimeoutError, json.JSONDecodeError) as exc:
        fail(f"teacher endpoint failed: {type(exc).__name__}: {exc}")
    try:
        choice = payload["choices"][0]
        message = choice["message"]
        answer = message["content"]
        finish = choice["finish_reason"]
    except (KeyError, IndexError, TypeError) as exc:
        fail(f"teacher response lacks choices/message/content/finish_reason: {exc}")
    if not isinstance(answer, str):
        fail("teacher content is not text; refusing to store structured/private reasoning")
    usage = payload.get("usage") if isinstance(payload.get("usage"), dict) else {}
    safe_usage = {k: usage[k] for k in ("prompt_tokens", "completion_tokens", "total_tokens") if k in usage and isinstance(usage[k], int)}
    return answer, str(finish), safe_usage


def generate(rows: list[dict[str, Any]], args: argparse.Namespace) -> dict[str, Any]:
    if args.output.exists():
        fail(f"refusing to overwrite existing output: {args.output}")
    key = os.environ.get("ATLAS_TEACHER_KEY")
    if not key:
        fail("--generate requires the key only through ATLAS_TEACHER_KEY")
    url = endpoint(args.base)
    accepted: list[dict[str, Any]] = []
    rejected: list[dict[str, str]] = []
    for row in rows[:args.max_samples]:
        answer, finish, usage = call_teacher(url, key, args.model, row["messages"], args.max_tokens)
        ok, reason = check_answer(row, answer, finish)
        if not ok:
            rejected.append({"id": row["id"], "reason": reason})
            continue
        # Only the visible assistant answer is retained. No raw response or
        # reasoning fields are copied into the SFT row.
        accepted.append({
            "id": row["id"],
            "messages": row["messages"] + [{"role": "assistant", "content": answer}],
            "provenance": {
                "source": "atlas_teacher_endpoint",
                "input_sha256": input_hash(row),
                "teacher_model": args.model,
                "teacher_base": args.base,
                "usage": usage,
                "finish_reason": finish,
            },
        })
    args.output.parent.mkdir(parents=True, exist_ok=True)
    with args.output.open('x',encoding='utf-8') as out:
        out.write("".join(json.dumps(item, ensure_ascii=False) + "\n" for item in accepted))
    return {"mode": "generate", "requested": min(len(rows), args.max_samples), "accepted": len(accepted), "rejected": rejected, "output": str(args.output)}


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--input", type=Path, required=True, help="local JSONL with messages and expected literal")
    parser.add_argument("--base", required=True, help="your own OpenAI-compatible /v1 endpoint base")
    parser.add_argument("--model", required=True, help="teacher model name sent to the endpoint")
    parser.add_argument("--output", type=Path, required=True, help="new JSONL output; existing files are never overwritten")
    parser.add_argument("--validate", action="store_true", help="offline validation (default when --generate is absent)")
    parser.add_argument("--generate", action="store_true", help="explicitly call the supplied endpoint")
    parser.add_argument("--max-samples", type=int, default=4)
    parser.add_argument("--max-tokens", type=int, default=256)
    args = parser.parse_args()
    if args.validate and args.generate:
        fail("choose only --validate or --generate")
    if args.max_samples < 1 or args.max_samples > MAX_SAMPLES_LIMIT:
        fail(f"--max-samples must be between 1 and {MAX_SAMPLES_LIMIT}")
    if args.max_tokens < 1 or args.max_tokens > MAX_TOKENS_LIMIT:
        fail(f"--max-tokens must be between 1 and {MAX_TOKENS_LIMIT}")
    rows = read_rows(args.input)
    result = generate(rows, args) if args.generate else validate(rows, args.max_samples)
    print(json.dumps(result, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
