<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.27.xsd">

  <!--
    Backfill geometry для existing записей где data содержит lat/lon
    (или latitude/longitude или lng), но geometry IS NULL. Match'ит
    backend auto-derive в DictionaryRecordService.resolveGeometry +
    CuodBundleImporter.deriveLatLonGeometry — bundle import skip'ит
    existing records, поэтому уже-загруженные записи остались без
    geometry. Без этой миграции AOI spatial filter возвращает 0 records
    на ground_station и similar dictionaries.

    Idempotent: только WHERE geometry IS NULL. Permission to fail per row
    ON_ERROR=CONTINUE — некоторые записи могут иметь lat/lon как string
    или вне диапазона; пропускаем, не падаем на всю миграцию.

    Rollback: SET geometry = NULL для тех же rows (lossless если
    geometry была null до).
  -->
  <changeSet id="0015-backfill-latlon-geometry-lat-lon" author="ordinis">
    <comment>Backfill geometry из data.lat + data.lon</comment>
    <sql>
      UPDATE dictionary_records
      SET geometry = ST_SetSRID(
        ST_MakePoint(
          (data->>'lon')::double precision,
          (data->>'lat')::double precision),
        4326)
      WHERE geometry IS NULL
        AND jsonb_typeof(data->'lat') = 'number'
        AND jsonb_typeof(data->'lon') = 'number'
        AND (data->>'lat')::double precision BETWEEN -90 AND 90
        AND (data->>'lon')::double precision BETWEEN -180 AND 180;
    </sql>
    <rollback>
      <sql>
        UPDATE dictionary_records
        SET geometry = NULL
        WHERE jsonb_typeof(data->'lat') = 'number'
          AND jsonb_typeof(data->'lon') = 'number';
      </sql>
    </rollback>
  </changeSet>

  <changeSet id="0015-backfill-latlon-geometry-latitude-longitude" author="ordinis">
    <comment>Backfill geometry из data.latitude + data.longitude (alias)</comment>
    <sql>
      UPDATE dictionary_records
      SET geometry = ST_SetSRID(
        ST_MakePoint(
          (data->>'longitude')::double precision,
          (data->>'latitude')::double precision),
        4326)
      WHERE geometry IS NULL
        AND jsonb_typeof(data->'latitude') = 'number'
        AND jsonb_typeof(data->'longitude') = 'number'
        AND (data->>'latitude')::double precision BETWEEN -90 AND 90
        AND (data->>'longitude')::double precision BETWEEN -180 AND 180;
    </sql>
    <rollback>
      <sql>
        UPDATE dictionary_records
        SET geometry = NULL
        WHERE jsonb_typeof(data->'latitude') = 'number'
          AND jsonb_typeof(data->'longitude') = 'number';
      </sql>
    </rollback>
  </changeSet>

  <changeSet id="0015-backfill-latlon-geometry-lat-lng" author="ordinis">
    <comment>Backfill geometry из data.lat + data.lng (alternate alias)</comment>
    <sql>
      UPDATE dictionary_records
      SET geometry = ST_SetSRID(
        ST_MakePoint(
          (data->>'lng')::double precision,
          (data->>'lat')::double precision),
        4326)
      WHERE geometry IS NULL
        AND jsonb_typeof(data->'lat') = 'number'
        AND jsonb_typeof(data->'lng') = 'number'
        AND (data->>'lat')::double precision BETWEEN -90 AND 90
        AND (data->>'lng')::double precision BETWEEN -180 AND 180;
    </sql>
    <rollback>
      <sql>
        UPDATE dictionary_records
        SET geometry = NULL
        WHERE jsonb_typeof(data->'lat') = 'number'
          AND jsonb_typeof(data->'lng') = 'number';
      </sql>
    </rollback>
  </changeSet>

</databaseChangeLog>
