"""Test rapide du filter Qualys — exécuter depuis C:\\patchcenter."""
import sys
sys.path.insert(0, ".")
import requests
import urllib3

from app.database import SessionLocal
from app.services.secrets_service import get_secret

urllib3.disable_warnings()

db = SessionLocal()
url = get_secret(db, "qualys_url").strip()
user = get_secret(db, "qualys_user").strip()
pwd = get_secret(db, "qualys_pass").strip()
db.close()

ep = url.rstrip("/") + "/qps/rest/2.0/search/am/hostasset"
hdr = {"X-Requested-With": "PC", "Content-Type": "text/xml"}

# Variante 1 : Criteria direct sous <filters>
body1 = (
    '<ServiceRequest>'
    '<preferences><limitResults>10</limitResults></preferences>'
    '<filters>'
    '<Criteria field="name" operator="CONTAINS">vpburadps1</Criteria>'
    '</filters>'
    '</ServiceRequest>'
)
# Variante 2 : Criteria sous <filters><list>
body2 = (
    '<ServiceRequest>'
    '<preferences><limitResults>10</limitResults></preferences>'
    '<filters>'
    '<list>'
    '<Criteria field="name" operator="CONTAINS">vpburadps1</Criteria>'
    '</list>'
    '</filters>'
    '</ServiceRequest>'
)

for label, body in [("V1 sans <list>", body1), ("V2 avec <list>", body2)]:
    print(f"\n=== {label} ===")
    print("BODY:", body)
    r = requests.post(ep, auth=(user, pwd), headers=hdr, data=body, verify=False, timeout=10)
    print("STATUS:", r.status_code)
    print(r.text[:800])
