"""Review-only company questionnaire CLI; it never imports or writes Firebird.""" import argparse import csv import json from pathlib import Path from central.app.ai.questionnaire import build_questionnaire, group_questions, render_csv, render_json, validate_answers def _load(path): if Path(path).suffix.lower() == ".json": return json.loads(Path(path).read_text(encoding="utf-8"))["questions"] with Path(path).open(newline="", encoding="utf-8-sig") as stream: return list(csv.DictReader(stream)) def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument("--questions", default="docs/LOCAL_FISCAL_CONTEXT_QUESTIONS_V2.json") parser.add_argument("--answers", help="JSON object keyed by QUESTION_ID; omit for guided mode") parser.add_argument("--output", default="docs/OPERATOR_CONTEXT_ANSWERS_V3.json") parser.add_argument("--format", choices=("json", "csv"), default="json") parser.add_argument("--confirm", action="store_true", help="write the reviewed artifact") args = parser.parse_args(argv) rows = _load(args.questions) databases = sorted({str(q.get("DATABASE") or "").strip() for q in rows if q.get("DATABASE")}) existing = {q.get("DATABASE") for q in rows if q.get("KEY") == "store_ipi_profile"} rows += [{"QUESTION_ID": f"QIPI-{name}", "DATABASE": name, "SCOPE": "COMPANY", "KEY": "store_ipi_profile", "VALID_OPTIONS": ["POPULATE_REFERENCE_RATE", "LEAVE_NOT_APPLICABLE"], "QUESTION": "Guardar a aliquota TIPI como referencia no cadastro?", "PRODUCTS_AFFECTED": 0, "CELLS": 0} for name in databases if name not in existing] questions = build_questionnaire([q for q in rows if str(q.get("CATEGORY", "")).upper() == "C" or q.get("KEY") == "store_ipi_profile"]) groups = group_questions(questions) if args.answers: answers = json.loads(Path(args.answers).read_text(encoding="utf-8")) else: answers = {} for group in groups: print(f"\nEMPRESA: {group['database']}\nPERGUNTA: {group['question']}\nPOR QUE PRECISA: {group['consequence']}\nPRODUTOS AFETADOS: {group['affected']['products']}\nCÉLULAS AFETADAS: {group['affected']['cells']}\nOPÇÕES: {', '.join(group['options'])}") value = input("RESPOSTA (ou CANCELAR): ").strip() if value.upper() == "CANCELAR": print("cancelado; nada foi persistido") return 0 for question_id in group["question_ids"]: answers[question_id] = value result = validate_answers(questions, answers) print(json.dumps({"questions": len(questions), "groups": len(groups), "entries": len(result["entries"]), "rollback_plan": result["rollback_plan"]}, ensure_ascii=False)) if not args.confirm: print("review only; nothing was persisted") return 0 Path(args.output).write_text(render_csv(result) if args.format == "csv" else render_json(result), encoding="utf-8") return 0 if __name__ == "__main__": raise SystemExit(main())