from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


def test_opportunity_flow_engine_files_exist_and_are_isolated():
    assert (ROOT / "app/domain/opportunity_flow/evidence.py").exists()
    assert (ROOT / "app/domain/opportunity_flow/decision.py").exists()
    assert (ROOT / "app/domain/opportunity_flow/rules.py").exists()
    assert (ROOT / "app/domain/opportunity_flow/engine.py").exists()
    rules = read("app/domain/opportunity_flow/rules.py")
    assert "HTMLResponse" not in rules
    assert "sqlalchemy" not in rules.lower()
    assert "orçamento -> pagamento" not in rules.lower()  # use semantic tests, not HTML copy


def test_company_profile_config_files_exist():
    workflow = read("config/company_profiles/blif/workflow.yaml")
    labels = read("config/company_profiles/blif/labels.yaml")
    assert "payment_terms: before_shipping" in workflow
    assert "delivery_terms: carrier" in workflow
    assert "Orçamento" in workflow
    assert "Fatura" in workflow
    assert "WAIT_PRODUCTION" in labels
    assert "Aguardar produção" in labels


def test_next_action_service_delegates_to_domain_engine():
    service = read("app/opportunity_next_action_service.py")
    assert "build_opportunity_evidence" in service
    assert "decide_opportunity_next_action" in service
    assert "load_company_profile" in service


def test_settings_workflow_page_exists():
    system = read("app/admin_ui/pages/system.py")
    assert "/settings/workflow" in system
    assert "Fluxo operacional" in system
    assert "regras críticas continuam protegidas" in system
