#!/usr/bin/env bash
# bootstrap_schema_migrations.sh
# Seed _schema_migrations tracking table with all SQL files already applied
# to production BEFORE auto-apply was introduced (PR #150).
#
# Run ONCE manually on prod via SSH tunnel before the first auto-deploy after
# merging feat/auto-apply-migrations-150. After that, deploy.yml handles
# everything automatically.
#
# Usage:
#   ssh gendesign           # open SSH tunnel (LocalForward 15432 → 5432)
#   export POSTGRES_USER=gendesign
#   export POSTGRES_PASSWORD=<from vault meta/00_credentials.md>
#   export POSTGRES_DB=gendesign
#   bash scripts/bootstrap_schema_migrations.sh
#
# The script connects on localhost:15432 (tunnel).
# Files NOT in this seed list will be applied automatically on next deploy.
# Currently pending (will be applied by first auto-deploy):
#   89_drop_dead_brin_rosreestr_deals.sql
#   90_user_weight_profiles.sql
#   91_engineering_networks_191fz.sql

set -euo pipefail

DB_URL="${DB_URL:-postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:15432/${POSTGRES_DB}}"

echo "Connecting to: postgresql://${POSTGRES_USER}:***@localhost:15432/${POSTGRES_DB}"

psql "$DB_URL" -v ON_ERROR_STOP=on <<'SQL'
CREATE TABLE IF NOT EXISTS _schema_migrations (
  filename TEXT PRIMARY KEY,
  applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Seed: all NN_*.sql files known to be applied on prod before PR #150.
-- Excludes 89, 90, 91 — those are pending and will auto-apply on next deploy.
-- Non-SQL files (*.sh, *.py) are never tracked — only .sql applies.
INSERT INTO _schema_migrations (filename) VALUES
  ('01_schema_rosreestr_deals.sql'),
  ('03_materialized_views.sql'),
  ('10_schema_domrf.sql'),
  ('20_schema_domrf_extras.sql'),
  ('31_schema_domrf_full.sql'),
  ('31_schema_domrf_raw.sql'),
  ('33_schema_domrf_lazy.sql'),
  ('35_schema_domrf_stats.sql'),
  ('37_cleanup_redundant.sql'),
  ('38_relations_fix.sql'),
  ('39_territory_aliases.sql'),
  ('40_relations.sql'),
  ('42_schema_breakdown_widen.sql'),
  ('43_anton_import.sql'),
  ('45_relations_v2.sql'),
  ('50_schema_kn_extensions.sql'),
  ('51_schema_kn_extras.sql'),
  ('52_schema_prinzip_crm.sql'),
  ('53_schema_kn_thumbs.sql'),
  ('54_schema_scrape_log.sql'),
  ('55_schema_scrape_resume.sql'),
  ('56_schema_ekb_districts_geom.sql'),
  ('57_kn_objects_district.sql'),
  ('58_schema_cad_quarters.sql'),
  ('59_backfill_obj_cad_quarter.sql'),
  ('60_v_zk_rosreestr_velocity.sql'),
  ('63_schema_nspd_runs.sql'),
  ('64_v_zk_rosreestr_velocity.sql'),
  ('65_partitions_rosreestr_2024_h1.sql'),
  ('66_indexes_recommend.sql'),
  ('67_refresh_ekb_districts_median.sql'),
  ('68_schema_objective.sql'),
  ('72_objective_sync_config.sql'),
  ('73_complexes_master.sql'),
  ('74_dedupe_unified_views.sql'),
  ('75_unification_naming.sql'),
  ('76_complex_connectivity.sql'),
  ('77_nspd_geo_jobs.sql'),
  ('78_drop_use_rosreestr2coord.sql'),
  ('80_complexes_backfill_nulls.sql'),
  ('81_job_settings.sql'),
  ('82_osm_poi_ekb.sql'),
  ('83_cad_parcels_geom.sql'),
  ('84_osm_noise_sources_ekb.sql'),
  ('85_pzz_zones_ekb.sql'),
  ('86_v_bucket_success_score.sql'),
  ('87_on_demand_indexes.sql'),
  ('88_nspd_quarter_dumps.sql')
ON CONFLICT (filename) DO NOTHING;

SELECT COUNT(*) AS seeded_count FROM _schema_migrations;
SQL

echo ""
echo "Bootstrap complete. Pending migrations (will apply on next deploy):"
echo "  89_drop_dead_brin_rosreestr_deals.sql"
echo "  90_user_weight_profiles.sql"
echo "  91_engineering_networks_191fz.sql"
echo ""
echo "Verify with:"
echo "  psql \$DB_URL -c \"SELECT filename, applied_at FROM _schema_migrations ORDER BY filename;\""
