-- Migration 101: cad_* overlay tables for NSPD layers (epic #263 sub-A)
--
-- Creates 5 new tables for NSPD overlay data:
--   cad_territorial_zones   -- layer 875838 (ПЗЗ территориальные зоны)
--   cad_red_lines           -- layer 879243 (красные линии)
--   cad_engineering_structures -- layer 36328 (инженерные сооружения)
--   cad_risk_zones          -- layers 872xxx (зоны рисков)
--   cad_opportunity_parcels -- layers 37294, 37298, 37299, 36473, 875845
--
-- All tables are idempotent (CREATE TABLE IF NOT EXISTS + CREATE INDEX IF NOT EXISTS).
-- Migration auto-applies via deploy.yml.

BEGIN;

-- ПЗЗ территориальные зоны (NSPD layer 875838)
CREATE TABLE IF NOT EXISTS cad_territorial_zones (
    id BIGSERIAL PRIMARY KEY,
    quarter_cad TEXT NOT NULL,
    zone_id TEXT NOT NULL UNIQUE,
    zone_code TEXT,
    zone_name TEXT,
    permitted_use TEXT,
    raw_props JSONB,
    geom GEOGRAPHY(POLYGON, 4326),
    fetched_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS cad_territorial_zones_geom_gist
    ON cad_territorial_zones USING GIST(geom);
CREATE INDEX IF NOT EXISTS cad_territorial_zones_quarter
    ON cad_territorial_zones (quarter_cad);

-- Red lines (NSPD layer 879243) — placeholder schema
CREATE TABLE IF NOT EXISTS cad_red_lines (
    id BIGSERIAL PRIMARY KEY,
    quarter_cad TEXT NOT NULL,
    feature_id TEXT UNIQUE,
    name TEXT,
    raw_props JSONB,
    geom GEOGRAPHY(GEOMETRY, 4326),
    fetched_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS cad_red_lines_geom_gist
    ON cad_red_lines USING GIST(geom);

-- Engineering structures (NSPD layer 36328) — для #115 точки подключения
CREATE TABLE IF NOT EXISTS cad_engineering_structures (
    id BIGSERIAL PRIMARY KEY,
    quarter_cad TEXT NOT NULL,
    cad_num TEXT,
    name TEXT,
    structure_type TEXT,
    raw_props JSONB,
    geom GEOGRAPHY(GEOMETRY, 4326),
    fetched_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS cad_engineering_structures_geom_gist
    ON cad_engineering_structures USING GIST(geom);
CREATE INDEX IF NOT EXISTS cad_engineering_structures_quarter
    ON cad_engineering_structures (quarter_cad);

-- Risk zones (NSPD layers 872xxx)
CREATE TABLE IF NOT EXISTS cad_risk_zones (
    id BIGSERIAL PRIMARY KEY,
    quarter_cad TEXT NOT NULL,
    feature_id TEXT UNIQUE,
    risk_type TEXT,
    layer_id INT,
    severity TEXT,
    raw_props JSONB,
    geom GEOGRAPHY(GEOMETRY, 4326),
    fetched_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS cad_risk_zones_geom_gist
    ON cad_risk_zones USING GIST(geom);

-- Opportunity parcels (NSPD layers 37294, 37298, 37299, 36473, 875845)
CREATE TABLE IF NOT EXISTS cad_opportunity_parcels (
    id BIGSERIAL PRIMARY KEY,
    quarter_cad TEXT NOT NULL,
    cad_num TEXT,
    opportunity_type TEXT,
    layer_id INT,
    name TEXT,
    raw_props JSONB,
    geom GEOGRAPHY(GEOMETRY, 4326),
    fetched_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS cad_opportunity_parcels_geom_gist
    ON cad_opportunity_parcels USING GIST(geom);

COMMIT;
