# Address-util SQL commands
# Run from apps/backend.
#
# Note: run the delete file last.
# Do NOT run address_util/address-bound-updates.sql (old artifact).

cd apps/backend

# 1. Admin coords (idempotent)
wrangler d1 execute freshyo-dev --file=../../address_util/backfill-admin-coords.sql --config wrangler.prod.toml --remote

# 2. Computed bounds for the 466 non-vague addresses
wrangler d1 execute freshyo-dev --file=../../address_util/computed-bounds-466.sql --config wrangler.prod.toml --remote

# 3. Manual bounds + coords for the 416 vague addresses
wrangler d1 execute freshyo-dev --file=../../address_util/address-bound-coord-updates.sql --config wrangler.prod.toml --remote

# 4. Delete the 23 selected addresses (run last)
wrangler d1 execute freshyo-dev --file=../../address_util/delete-outside-bound-addresses.sql --config wrangler.prod.toml --remote

# Optional verification
wrangler d1 execute freshyo-backend-dev --config wrangler.dev.toml --command="SELECT bound_id, COUNT(*) AS n FROM addresses GROUP BY bound_id ORDER BY bound_id" --remote
wrangler d1 execute freshyo-backend-dev --config wrangler.dev.toml --command="SELECT COUNT(*) AS vague FROM addresses WHERE vague_backfilled_coords = 1" --remote
