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_detail_uses_context_left_operation_right_without_duplicate_tabs():
    opps = read("app/admin_ui/pages/opportunities.py")
    assert "cf-opp-detail-grid" in opps
    assert "grid-template-areas:'context operation'" in opps
    assert "cf-opp-context-column" in opps
    assert "cf-opp-operation-column" in opps
    assert "No duplicated tabs; sections render as vertical cards" in opps
    assert '<nav class="cf-ia-tabs" aria-label="Secções da oportunidade">' not in opps
    assert "cf-opp-context-jump" not in opps


def test_opportunity_detail_keeps_context_sections_as_vertical_cards():
    opps = read("app/admin_ui/pages/opportunities.py")
    for marker in [
        'id="cliente"',
        'id="documentos"',
        'id="produtos"',
        'id="odoo"',
        'id="comunicacoes"',
        'id="timeline"',
        'id="tecnico"',
    ]:
        assert marker in opps
    assert "Documentos Jasmin" in opps
    assert "Produtos" in opps
    assert "Estado Odoo" in opps
    assert "Mensagens Chatwoot" in opps
    assert "Timeline recente" in opps


def test_opportunity_detail_has_fiscal_association_card_in_operation_column():
    opps = read("app/admin_ui/pages/opportunities.py")
    assert "fiscal_association_card_html" in opps
    assert "Associar cliente fiscal" in opps
    assert 'action="/opportunities/{esc(opportunity_id)}/customer"' in opps
    assert "Guardar cliente fiscal" in opps
    assert "Pesquisar clientes" in opps
    assert "Criar cliente" in opps
    assert "{fiscal_association_card_html}" in opps
