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_v482_guided_operations_helpers_exist():
    guidance = read("app/admin_ui/guidance.py")
    assert "def fiscal_contact_panel_html" in guidance
    assert "Cliente fiscal e contacto Chatwoot" in guidance
    assert "def readiness_checklist_html" in guidance
    assert "Prontidão" in guidance
    assert "def outbox_operator_message" in guidance
    assert "Motivo provável" in read("app/admin_ui/pages/outbox.py")


def test_v482_operations_and_opportunities_show_fiscal_context_and_blockers():
    ops = read("app/admin_ui/pages/operations.py")
    opps = read("app/admin_ui/pages/opportunities.py")
    vm = read("app/admin_ui/view_models/operations.py")
    assert "work_item_blockers" in ops
    assert "Associações por confirmar" in vm
    assert "Bloqueios atuais" in opps
    assert "Prontidão para documentos" in opps
    assert "Prontidão para envio" in opps
    assert "Associação fiscal" in opps


def test_v482_task_detail_separates_fiscal_customer_and_contact():
    tasks = read("app/admin_ui/pages/tasks.py")
    assert "fiscal_contact_panel_html" in tasks
    assert "Prontidão fiscal da tarefa" in tasks
    assert "Prontidão mínima antes de documento/envio" in tasks
    assert "Corrigir estes dados antes de emitir documento" in tasks


def test_v482_system_health_has_production_state_and_data_quality_metrics():
    service = read("app/operations_service.py")
    system = read("app/admin_ui/pages/system.py")
    assert "open_opportunities_without_fiscal_customer" in service
    assert "active_incomplete_fiscal_customers" in service
    assert "products_missing_external_code" in service
    assert "seconds_since_last_chatwoot_webhook" in service
    assert "Estado para operação" in system
    assert "Oportunidades sem cliente fiscal" in system
    assert "Clientes fiscais incompletos" in system
    assert "Último webhook Chatwoot" in system


def test_v482_documentation_exists():
    doc = read("docs/CLIENTFLOW_V482_PRODUCTION_STABILIZATION.md")
    assert "Production Stabilization" in doc
    assert "Cliente fiscal" in doc
    assert "Outbox em linguagem operacional" in doc
    assert "Sem migração" in doc
