import json
from pathlib import Path


def test_send_quote_template_supports_text_quote_without_losing_quotation_validation():
    templates = Path("app/message_templates.py").read_text()
    assert 'code="SEND_QUOTE"' in templates
    assert 'reply_type="send_document_or_text_quote"' in templates
    assert 'expected_document_kinds=("quotation",)' in templates
    assert 'Segue abaixo a proposta/preços indicativos' in templates


def test_reply_readiness_allows_send_quote_without_document_and_forbids_attachment_claims():
    service = Path("app/reply_assistant_service.py").read_text()
    assert "def _is_send_quote_text_allowed" in service
    assert "Sem documento/anexo selecionado: a resposta será tratada como proposta textual" in service
    assert "O modelo selecionado requer pelo menos um documento/anexo da oportunidade" in service
    assert "sem escrever 'segue em anexo'" in service
    assert '"Segue em anexo"' in service
    assert 'send_document_or_text_quote' in service


def test_explicit_orcamento_prompt_and_intent_keep_send_quote():
    action_prompt = Path("app/action_prompt.py").read_text()
    intent_gate = Path("app/reply_intent_gate.py").read_text()
    assert '"Necessito de um orçamento"' in action_prompt
    assert 'não SEND_INFO' in action_prompt or 'não SEND_INFO' in action_prompt.replace('nao', 'não')
    assert 'quote_terms = ("orcamento", "cotacao", "proposta", "quote", "quotation")' in intent_gate
    assert 'template = "SEND_QUOTE"' in intent_gate
    assert 'reply_type = "send_document_or_text_quote"' in intent_gate


def test_catalog_contains_dual_74_price_315():
    data = json.loads(Path("app/business_knowledge/blif_knowledge.json").read_text())
    assert any(
        item.get("code") == "EV_DUAL_7_4" and item.get("price_without_vat") == 315
        for item in data["products"]
    )


def test_audit_and_repair_scripts_include_quote_misclassification_guard():
    audit = Path("scripts/audit_operator_page_consistency.py").read_text()
    repair = Path("scripts/repair_send_info_quote_requests.py").read_text()
    assert "send_info_task_for_explicit_quote_request" in audit
    assert "EXPLICIT_QUOTE_REQUEST_RE" in audit
    assert "repair_send_info_quote_requests" in repair
    assert "new_action_code=\"SEND_QUOTE\"" in repair
