"""Тесты parcel_planning_overlaps (#1085 analyze-wiring) — ППТ/ПМТ overlap по участку.

БД не дёргается: фейковая сессия с .execute().mappings().all(). Проверяем graceful на
отсутствие участка/таблицы и проброс строк.
"""

from __future__ import annotations

from typing import Any

from sqlalchemy.exc import ProgrammingError

from app.services.site_finder.planning_lookup import parcel_planning_overlaps


class _Result:
    def __init__(self, rows: list[dict[str, Any]]) -> None:
        self._rows = rows

    def mappings(self) -> _Result:
        return self

    def all(self) -> list[dict[str, Any]]:
        return self._rows


class _DB:
    def __init__(self, rows: list[dict[str, Any]] | Exception) -> None:
        self._rows = rows

    def execute(self, sql: Any, params: dict[str, Any]) -> _Result:
        if isinstance(self._rows, Exception):
            raise self._rows
        return _Result(self._rows)


def test_returns_overlaps() -> None:
    rows = [
        {
            "project_type": "ppt",
            "doc_status_name": "действующий",
            "full_name": "ПАГЕ №1",
            "project_name": "Галактика",
            "dmd_actual_year": 2021,
        },
    ]
    out = parcel_planning_overlaps(_DB(rows), "POINT(60 56)")  # type: ignore[arg-type]
    assert out == rows


def test_empty_wkt_returns_empty() -> None:
    assert parcel_planning_overlaps(_DB([]), None) == []  # type: ignore[arg-type]


def test_missing_table_is_graceful() -> None:
    """planning_projects ещё не задеплоена → ProgrammingError → [] (analyze не падает)."""
    db = _DB(ProgrammingError("stmt", {}, Exception("relation planning_projects does not exist")))
    assert parcel_planning_overlaps(db, "POINT(60 56)") == []  # type: ignore[arg-type]
