-- 05_inc_geography.sql — fleettickets · add a geography column for routing/distance
-- ─────────────────────────────────────────────────────────────────────────────
-- `geom` is geometry(Point,4326) — great for display, but distance on it is in
-- planar degrees. For real-world routing ("nearest vehicle", radius search) we want
-- geography, whose ST_Distance / ST_DWithin work in metres on the spheroid.
--
-- `geog` is a STORED generated column = geom::geography (the cast is IMMUTABLE), so
-- it tracks geom automatically (incl. after geocode passes). GiST index supports
-- ST_DWithin(geog, vehicle::geography, metres) and KNN nearest-neighbour ordering.
-- Idempotent.
-- ─────────────────────────────────────────────────────────────────────────────

SET search_path = tickets, public;

ALTER TABLE tickets.inc
  ADD COLUMN IF NOT EXISTS geog geography(Point, 4326) GENERATED ALWAYS AS (geom::geography) STORED;

CREATE INDEX IF NOT EXISTS ix_inc_geog ON tickets.inc USING gist (geog);
