from pathlib import Path


def test_jasmin_reconciliation_imports_documents_and_lines():
    source = Path("app/reconciliation_service.py").read_text()
    assert "def _upsert_jasmin_document_from_item" in source
    assert "commercial_documents" in source
    assert "commercial_document_lines" in source
    assert "def _jasmin_document_lines_from_item" in source


def test_jasmin_reconciliation_imports_opportunity_items_and_value():
    source = Path("app/reconciliation_service.py").read_text()
    assert "def _upsert_opportunity_items_from_jasmin_item" in source
    assert "metadata->>'source_system' = 'jasmin'" in source
    assert "_apply_jasmin_documents_to_opportunity" in source
    assert "jasmin_reconciliation_imported" in source


def test_create_and_link_process_apply_jasmin_import():
    source = Path("app/reconciliation_service.py").read_text()
    assert "_apply_jasmin_documents_to_opportunity(conn, items, opportunity_id, actor=actor)" in source
    assert "_apply_jasmin_documents_to_opportunity(conn, [item], opportunity_id, actor=actor)" in source


def test_jasmin_reimport_updates_existing_opportunity_items():
    source = Path("app/reconciliation_service.py").read_text()
    assert "UPDATE opportunity_items" in source
    assert "unit_price = CAST(:unit_price AS NUMERIC)" in source
    assert "total_price = CAST(:total_price AS NUMERIC)" in source
    assert "price_source" in source
