#!/usr/bin/env bash
# build_all.sh
# ============
# Run all build_* scripts in dependency order to rebuild the full pipeline.
#
# Prerequisites:
#   - Python environment with dependencies installed (pip install -r requirements.txt)
#   - data/2026/securite-info-complements-data.gouv-*.xlsx  (large national file, download manually)
#   - Internet access (downloads DVF data, cadastre, API Geo, IGN WFS, Overpass, GEORISQUES)
#
# Steps:
#   1. Parcel centroid GPS cache from Etalab cadastre (one-time, ~215 MB download)
#   2. DVF 2015-2020 historical data from data.cquest.org (~6 × 200 MB)
#   3. Nord department security scores (requires the large source xlsx)
#   4. Lille métropole security subset (from step 3 output)
#   5. IRIS GeoJSON + security + DVF prices (IGN WFS + steps 3-4 output)
#   6. DVF → IRIS spatial join → mutations + prix_evolution_iris tables
#   7. Backcast missing IRIS price years
#   8. Commune-level price evolution (17 Lille métropole communes)
#   9. GEORISQUES TRI flood zone polygons
#  10. OSM road noise zones (Overpass API, LDEN estimate)
#  11. Overpass POIs (mairie, gare, metro, tram — Nord)
#  12. GEORISQUES Seveso/ICPE hazard sites
#  13. OSM nature/green coverage zones
#  14. SPS/BPS/NPI pressure scores per IRIS
#  15. Radon potential class — all metropolitan France communes (~35 000, ~10–15 min)
#  16. Retrait-Gonflement des Argiles — all metropolitan France communes (~35 000, ~10–15 min) → iris_pression table
#
# Usage:
#   bash build_all.sh            # run all steps (skip if output already exists)
#   bash build_all.sh --force    # pass --force to steps that support it

set -euo pipefail

FORCE=""
if [[ "${1:-}" == "--force" ]]; then
    FORCE="--force"
fi

log() {
    echo ""
    echo "============================================================"
    echo "  STEP $1 — $2"
    echo "============================================================"
}

log 1  "Parcel centroids (Etalab cadastre Nord-59)"
python build_parcel_centroids_59.py ${FORCE:+--force-build}

log 2  "DVF historique 2015-2020 (data.cquest.org)"
python build_dvf_historique.py $FORCE

log 3  "Security scores — département Nord"
python build_securite_nord.py

log 4  "Security scores — Lille métropole subset"
python build_securite_lille.py

log 5  "IRIS GeoJSON + security + DVF prices"
python build_iris_data.py

log 6  "DVF → IRIS spatial join (mutations + prix_evolution_iris)"
python build_mutations_iris.py

log 7  "Backcast missing IRIS price years"
python build_backcast_iris.py

log 8  "Commune-level price evolution (Lille métropole)"
python build_prix_evolution.py

log 9  "Flood zones (GEORISQUES WFS TRI)"
python build_flood_zones.py

log 10 "Noise zones (OSM roads via Overpass, LDEN estimate)"
python build_noise_zones.py

log 11 "POIs (Overpass API — Nord)"
python build_poi.py

log 12 "Seveso/ICPE hazard sites (GEORISQUES)"
python build_seveso.py

log 13 "Nature/green zones (OSM via Overpass)"
python build_nature_zones.py

log 14 "Pressure scores SPS/BPS/NPI per IRIS"
python build_pression_iris.py

log 15 "Radon potential class — all metropolitan France communes"
python build_radon.py

log 16 "Retrait-Gonflement des Argiles — all metropolitan France communes"
python build_rga.py

echo ""
echo "============================================================"
echo "  All build steps completed successfully."
echo "============================================================"
