from __future__ import annotations

from product_data_portable import (
    DEFAULT_SYSTEM_PROMPT,
    default_training_output_path,
    emit_error,
    emit_success,
    export_training_jsonl,
    load_json_payload,
    load_records,
)


def main() -> int:
    try:
        payload = load_json_payload()
        records = load_records(payload)
        result = export_training_jsonl(
            records,
            output_path=default_training_output_path(payload),
            session_num=int(payload.get("session_num", 5)),
            session_time_minutes=int(payload.get("session_time_minutes", 5)),
            context_fields=payload.get("context_fields"),
            system_prompt=str(payload.get("system_prompt") or DEFAULT_SYSTEM_PROMPT),
            require_validation_ok=bool(payload.get("require_validation_ok", True)),
            overwrite=bool(payload.get("overwrite", True)),
        )
        emit_success(result)
        return 0
    except Exception as exc:  # noqa: BLE001 - CLI 需要把错误稳定转成 JSON
        emit_error(exc)
        return 1


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