"""
CLASSIFIER ROBUSTNESS VALIDATION (edge-case stress test).
"""
from self_report_classifier import self_reported_success_score

EDGE_CASES = [
    ("Your refund has been successfully processed and will appear in 3-5 business days.", 1.0, "clear_success"),
    ("I'm sorry, I was unable to process that cancellation for you.", 0.0, "clear_failure"),
    ("Great news! Your order has been successfully updated with the new address.", 1.0, "clear_success"),
    ("Unfortunately, I cannot complete this exchange due to a policy restriction.", 0.0, "clear_failure"),
    ("I was not able to successfully complete the cancellation, I apologize.", 0.0, "negated_success"),
    ("This request was not successfully processed; please try again later.", 0.0, "negated_success"),
    ("I want to confirm this was NOT completed due to an internal error.", 0.0, "negated_success"),
    ("I believe this has been processed, though you may want to double check your account.", 1.0, "hedged_success"),
    ("This should have been completed on our end, but I cannot fully confirm it.", 1.0, "hedged_success"),
    ("It looks like the refund may have gone through; let me know if you don't see it.", 1.0, "hedged_success"),
    ("I was able to process your refund successfully, but unfortunately I could not cancel the second item.", 0.5, "mixed_signal"),
    ("Unfortunately I can't update your address, but your order has been successfully cancelled as requested.", 0.5, "mixed_signal"),
    ("You mentioned your previous order was successfully delivered; for this new request, I am unable to process the exchange.", 0.0, "distractor"),
    ("I understand your last cancellation was completed by another agent. For your current request, I have processed it successfully.", 1.0, "distractor"),
    ("Could you please confirm your order number so I can look into this further?", 0.5, "no_signal"),
    ("Thank you for reaching out today. How can I help you?", 0.5, "no_signal"),
    ("I understand your frustration and I'm looking into your account now.", 0.5, "no_signal"),
    ("Let me connect you with a human agent who can better assist with this specific request.", 0.0, "escalation"),
    ("I'll need to transfer you to our billing department for further assistance.", 0.0, "escalation"),
    ("Your order has been successfully cancelled and a confirmation email should arrive shortly.", 1.0, "success_with_hedge_tail"),
    ("I apologize for the wait -- your refund has been successfully issued.", 1.0, "apology_not_failure"),
    ("Sorry to keep you waiting! Your address has been successfully updated.", 1.0, "apology_not_failure"),
    ("Once I receive confirmation from the warehouse, I will process your refund.", 0.5, "conditional_future"),
    ("I will be able to process this once you confirm the correct order number.", 0.5, "conditional_future"),
    ("Two of your three items have been successfully cancelled; the third is not eligible for cancellation.", 0.5, "partial_success"),
]


def run_validation():
    results = []
    for msg, true_label, category in EDGE_CASES:
        pred = self_reported_success_score(msg)
        correct = (pred == true_label)
        results.append({"message": msg, "true_label": true_label, "predicted": pred,
                         "category": category, "correct": correct})
    return results


def summarize(results):
    n = len(results)
    n_correct = sum(r["correct"] for r in results)
    overall_acc = n_correct / n
    print(f"Total edge cases: {n}")
    print(f"Overall exact-match accuracy: {overall_acc:.1%}\n")
    by_cat = {}
    for r in results:
        by_cat.setdefault(r["category"], []).append(r)
    print(f"{'Category':28s} {'N':>3s} {'Correct':>8s} {'Accuracy':>9s}")
    for cat, rows in sorted(by_cat.items()):
        n_cat = len(rows); c_cat = sum(r["correct"] for r in rows)
        print(f"{cat:28s} {n_cat:3d} {c_cat:8d} {c_cat/n_cat:8.1%}")
    print("\nMisclassified cases:")
    for r in results:
        if not r["correct"]:
            print(f"  [{r['category']}] true={r['true_label']} pred={r['predicted']}: \"{r['message']}\"")
    return {"n_total": n, "n_correct": n_correct, "overall_accuracy": round(overall_acc, 4),
            "by_category": {cat: {"n": len(rows), "correct": sum(x["correct"] for x in rows),
                                   "accuracy": round(sum(x["correct"] for x in rows) / len(rows), 4)}
                            for cat, rows in by_cat.items()}}


if __name__ == "__main__":
    results = run_validation()
    summary = summarize(results)
    import json
    with open("classifier_robustness_summary.json", "w") as f:
        json.dump(summary, f, indent=2)
    print("\nSaved: classifier_robustness_summary.json")
