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_task_service_uses_production_schema_local_customer_id_only():
    source = read("app/task_service.py")
    assert "LEFT JOIN customers cu ON cu.id = o.local_customer_id" in source
    assert "COALESCE(o.fiscal_customer_id, o.local_customer_id)" not in source
    assert "o.fiscal_customer_id::text" not in source
    assert "o.local_customer_id::text AS opportunity_fiscal_customer_id" in source


def test_next_action_service_maps_local_customer_id_to_engine_fiscal_alias():
    source = read("app/opportunity_next_action_service.py")
    assert "local_customer_id::text AS fiscal_customer_id" in source
    assert "local_customer_id::text AS customer_id" in source
    assert "street_name AS address" in source
    assert "postal_zone AS postal_code" in source
    assert "city_name AS city" in source
    assert "SELECT id::text, stage, status, title, fiscal_customer_id::text" not in source
