-- 168_fdw_osm_poi_ekb.sql
-- FDW foreign table bridging to gendesign's v_tradein_osm_poi_ekb view for
-- location-coef / POI scoring in LocationDrawer (#2045, BE-3 final PR).
--
-- Mirrors 060_postgres_fdw_extension.sql conventions: uses the SAME SERVER
-- gendesign_remote (already created there) — this migration does NOT create a new
-- SERVER / USER MAPPING.
--
-- DEPENDENCIES:
--   - 060_postgres_fdw_extension.sql (SERVER gendesign_remote + USER MAPPING, managed by
--     backend startup with env password).
--   - gendesign: view public.v_tradein_osm_poi_ekb + GRANT SELECT to tradein_fdw_reader
--     (PR-A of #2045, already merged + deployed on gendesign).
--
-- Idempotent: DROP FOREIGN TABLE IF EXISTS + re-CREATE.

BEGIN;

DROP FOREIGN TABLE IF EXISTS gendesign_osm_poi_ekb;

CREATE FOREIGN TABLE gendesign_osm_poi_ekb (
    category text,
    name     text,
    lat      double precision,
    lon      double precision
)
SERVER gendesign_remote
OPTIONS (schema_name 'public', table_name 'v_tradein_osm_poi_ekb');

COMMENT ON FOREIGN TABLE gendesign_osm_poi_ekb IS
    'Live view of gendesign.osm_poi_ekb (Site Finder OSM POI, EKB). USER MAPPING managed by '
    'backend startup (see 060). Materialized locally by the osm_poi_ekb_refresh scheduler job '
    'into osm_poi_ekb_local (migration 169) — see app/tasks/osm_poi_ekb_refresh.py. '
    'Feeds app/services/location_coef.py (GET /api/v1/trade-in/location-coef).';

COMMIT;
