"""SQLAlchemy + GeoAlchemy2 ORM models.

Stage 2a: real Parcel model. Geometry stored in WGS84 (EPSG:4326);
project to МСК-66 via pyproj when computing distances/areas.
"""

from datetime import datetime

from geoalchemy2 import Geometry
from sqlalchemy import JSON, DateTime, Float, String
from sqlalchemy.orm import Mapped, mapped_column

from app.core.db import Base


class Parcel(Base):
    __tablename__ = "parcels"

    id: Mapped[str] = mapped_column(String, primary_key=True)
    cadastral_number: Mapped[str] = mapped_column(String, unique=True, index=True)
    vri: Mapped[str] = mapped_column(String, index=True)
    area_sqm: Mapped[float] = mapped_column(Float)
    address: Mapped[str | None] = mapped_column(String, nullable=True)
    geometry: Mapped[object] = mapped_column(Geometry("POLYGON", srid=4326))
    enrichment: Mapped[dict] = mapped_column(JSON, default=dict)
    created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
    updated_at: Mapped[datetime] = mapped_column(
        DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
    )
