"""Offline checks: no API key, no network. Run inside the triage-lab image."""
import json
from alert_triage_copilot.db import SessionLocal, init_db
from alert_triage_copilot.feedback_loop import FeedbackStore
from alert_triage_copilot.agent import mock_classify, _parse_llm_response
from alert_triage_copilot.mock_data import get_mock_alerts
from alert_triage_copilot.schemas import RawAlert, Classification

init_db(); s = SessionLocal(); store = FeedbackStore(s)
scan03 = next(a for a in get_mock_alerts() if a.alert_id == "ALERT-SCAN03")

print("== 1. Demo classifier: one correction, then a different Port Scan")
rogue = RawAlert(**json.load(open("/lab/alerts/scan-rogue.json")))
print("before feedback :", mock_classify(rogue, store).classification.value, mock_classify(rogue, store).confidence)
store.submit_feedback(scan03, Classification.TRUE_POSITIVE, Classification.FALSE_POSITIVE,
                      "soc-analyst-42", "Registered Nessus scanner, approved change ticket CHG-2026-04521.")
d = mock_classify(rogue, store)
print("after feedback  :", d.classification.value, d.confidence, "|", d.reasoning[:150])

print("\n== 2. Parser: a model reply that says TP but lists four remediation steps")
reply = json.dumps({"classification": "TP", "confidence": 0.97, "reasoning": "847 failed SSH logins from a Tor exit node.",
  "recommended_action": "Block and escalate.", "mitre_tactics": ["TA0006"], "mitre_technique_id": "T1110",
  "severity_justification": "High-volume credential attack.",
  "remediation_steps": ["Block the IP.", "Check for successful logins.", "Rotate exposed credentials.", "Escalate to IR."]})
d = _parse_llm_response("PARSE-TEST", reply)
print("stored as       :", d.classification.value, d.confidence, "|", d.reasoning.splitlines()[1].strip()[:90])
