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_v498_recent_window_uses_human_last_n_days():
    sync = read("app/external_reconciliation_sync.py")
    service = read("app/reconciliation_service.py")

    assert "_recent_window_start" in sync
    assert "days=days - 1" in sync
    assert "recent_window_start" in service
    assert "days=days - 1" in service


def test_v498_reconciliation_page_recent_window_and_search_action():
    page = read("app/admin_ui/pages/reconciliation.py")

    assert "list_reconciliation_items(status=status, external_type=item_type, limit=100, days=recent_days)" in page
    assert "reconciliation_summary(days=recent_days)" in page
    assert "Pesquisar oportunidade" in page
    assert "Escolher outra oportunidade" in page


def test_v498_odoo_sync_enriches_partner_for_matching():
    sync = read("app/external_reconciliation_sync.py")

    assert 'fields=["id", "vat", "email", "name"]' in sync
    assert 'record["partner_vat"]' in sync
    assert 'record["partner_email"]' in sync
    assert '"customer_email": _customer_email(record)' in sync


def test_v498_opportunity_board_hides_noise_and_uses_action_column():
    page = read("app/admin_ui/pages/opportunities.py")

    assert "is_noise_operation_item" in page
    assert "_is_noise_opportunity" in page
    assert "_opportunity_board_column_for_opportunity" in page
    assert "SEND_INVOICE" in page and "payment" in page
