"""Smart Onboarding Advisor — acceptance tests (the 7 criteria).

A synthetic multi-certified company (ISO 9001 + ISO 27001 + ISO 14001 + TISAX) onboards toward the CRA.
The Advisor orchestrates the existing engines and must satisfy: multi-cert works; ISO 14001 is not
falsely relevant; certs reduce questions but satisfy nothing automatically (Welt-1); <=5 self-explaining
next-best questions; answers update the profile (delta shrinks); sales selects/interprets nothing.
"""

from __future__ import annotations

import os

import yaml

from compliance.onboarding import OnboardingInput, advisor_start, apply_answer
from compliance.transition_reasoning import TargetRequirement

_CRA = yaml.safe_load(open(os.path.join(
    os.path.dirname(__file__), "..", "knowledge", "transition_patterns",
    "transition_pattern_iso27001_to_cra_maschinenvo_v1.yaml"), encoding="utf-8"))
_INFOSEC = [a["capability"] for a in _CRA["likely_covered"]]
_REQ = [TargetRequirement(capability_id=a["capability"]) for a in _CRA["likely_covered"]]
_REQ += [TargetRequirement(capability_id=d["capability"], question_intent=d.get("needed_information", "verify_existence"),
                           expected_evidence=d.get("expected_evidence", []))
         for d in _CRA["delta_requirements"]]
_COVERS = {d["capability"]: d.get("covers_targets", []) for d in _CRA["delta_requirements"]}

_HYP = {
    "ISO27001": _INFOSEC,
    "TISAX": _INFOSEC,
    "ISO9001": ["ce_conformity_assessment_and_technical_documentation"],   # a CRA delta cap (relevant)
    "ISO14001": ["environmental_management_documentation"],                # NOT in the CRA required set
}
_INPUT = OnboardingInput(
    company="synthetic", industry="machine_builder", products=["parking payment system"],
    markets=["EU"], certifications=["ISO9001", "ISO27001", "ISO14001", "TISAX"],
    known_evidence=["CE process"], target=["CRA"])


def _run(inp=_INPUT, hyp=_HYP):
    return advisor_start(inp, hyp, _REQ, target_id="CRA", covers_targets=_COVERS,
                         corpus_status={"CRA": "validated"})


def test_1_multi_certification_works():
    res = _run()
    certs = {a.certification for a in res.inferred_assumptions}
    assert {"ISO27001", "ISO9001"} <= certs            # several certs contribute inferred capabilities


def test_2_iso14001_not_falsely_relevant_for_cra():
    res = _run()
    assert any(r.certification == "ISO14001" for r in res.rejected_assumptions)
    assert all(a.certification != "ISO14001" for a in res.inferred_assumptions)


def test_3_certs_reduce_questions_but_satisfy_nothing_automatically():
    res = _run()
    for a in res.inferred_assumptions:
        assert a.verification_required is True
        assert "nicht automatisch erfüllt" in a.statement


def test_4_at_most_five_next_best_questions():
    res = _run()
    assert 0 < len(res.next_best_questions) <= 5


def test_5_every_question_explains_why():
    res = _run()
    assert all(q.why.strip() for q in res.next_best_questions)


def test_6_each_answer_updates_the_profile():
    res = _run()
    open_cap = res.capability_delta[0]
    # the answer "confirmed" adds the capability; re-running shrinks the delta
    confirmed = apply_answer([], open_cap, "confirmed")
    assert confirmed == [open_cap]
    hyp2 = {**_HYP, "ANSWERED": confirmed}
    inp2 = _INPUT.model_copy(update={"certifications": _INPUT.certifications + ["ANSWERED"]})
    res2 = advisor_start(inp2, hyp2, _REQ, target_id="CRA", covers_targets=_COVERS, corpus_status={"CRA": "validated"})
    assert len(res2.capability_delta) < len(res.capability_delta)


def test_7_sales_selects_nothing_engine_produces_everything():
    res = _run()
    # from plain inputs the engine produced the whole advisory payload
    assert res.headline and res.capability_delta and res.top_measures and res.evidence_requests
    assert res.completeness_summary
