#!/bin/bash
# ============================================
# OSM PostGIS Import Script for GeoEdu Service
# ============================================
#
# Importiert OSM PBF-Daten in PostgreSQL/PostGIS
# Verwendet osm2pgsql fuer den Import
#
# Voraussetzungen:
# - PostgreSQL mit PostGIS Extension
# - osm2pgsql installiert
# - OSM PBF Datei heruntergeladen
#
# Nutzung:
#   ./import_osm.sh [--slim] [--drop]
#
# ============================================

set -e

# Configuration
DATA_DIR="${OSM_DATA_DIR:-/app/data/osm}"
PBF_FILE="${DATA_DIR}/germany-latest.osm.pbf"
DATABASE_URL="${DATABASE_URL:-postgresql://breakpilot:breakpilot123@postgres:5432/breakpilot_db}"

# Parse DATABASE_URL
DB_USER=$(echo $DATABASE_URL | sed -n 's|.*://\([^:]*\):.*|\1|p')
DB_PASS=$(echo $DATABASE_URL | sed -n 's|.*://[^:]*:\([^@]*\)@.*|\1|p')
DB_HOST=$(echo $DATABASE_URL | sed -n 's|.*@\([^:]*\):.*|\1|p')
DB_PORT=$(echo $DATABASE_URL | sed -n 's|.*:\([0-9]*\)/.*|\1|p')
DB_NAME=$(echo $DATABASE_URL | sed -n 's|.*/\([^?]*\).*|\1|p')

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${YELLOW}============================================${NC}"
echo -e "${YELLOW}GeoEdu Service - OSM PostGIS Import${NC}"
echo -e "${YELLOW}============================================${NC}"
echo ""

# Check if PBF file exists
if [ ! -f "$PBF_FILE" ]; then
    echo -e "${RED}Fehler: OSM PBF Datei nicht gefunden!${NC}"
    echo "Erwartet: $PBF_FILE"
    echo ""
    echo "Bitte zuerst download_osm.sh ausfuehren."
    exit 1
fi

echo "PBF Datei: $PBF_FILE"
echo "Groesse: $(du -h "$PBF_FILE" | cut -f1)"
echo ""
echo "Datenbank: $DB_NAME @ $DB_HOST:$DB_PORT"
echo ""

# Check for osm2pgsql
if ! command -v osm2pgsql &> /dev/null; then
    echo -e "${RED}Fehler: osm2pgsql nicht installiert!${NC}"
    echo ""
    echo "Installation:"
    echo "  apt-get install osm2pgsql  # Debian/Ubuntu"
    echo "  brew install osm2pgsql     # macOS"
    exit 1
fi

OSM2PGSQL_VERSION=$(osm2pgsql --version 2>&1 | head -1)
echo "osm2pgsql Version: $OSM2PGSQL_VERSION"
echo ""

# Parse arguments
SLIM_MODE=""
DROP_MODE=""
while [[ $# -gt 0 ]]; do
    case $1 in
        --slim)
            SLIM_MODE="--slim"
            echo "Modus: Slim (fuer Updates)"
            shift
            ;;
        --drop)
            DROP_MODE="--drop"
            echo "Modus: Drop (bestehende Tabellen loeschen)"
            shift
            ;;
        *)
            echo "Unbekannte Option: $1"
            exit 1
            ;;
    esac
done

# Estimate time
PBF_SIZE=$(stat -f%z "$PBF_FILE" 2>/dev/null || stat -c%s "$PBF_FILE")
ESTIMATED_HOURS=$((PBF_SIZE / 1000000000 / 2))  # Rough estimate: ~2GB per hour

echo ""
echo -e "${YELLOW}ACHTUNG: Import kann 2-4 Stunden dauern!${NC}"
echo "Geschaetzte Dauer: ~${ESTIMATED_HOURS}-$((ESTIMATED_HOURS * 2)) Stunden"
echo ""
read -p "Import starten? (j/N) " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[JjYy]$ ]]; then
    echo "Import abgebrochen."
    exit 1
fi

# Create style file
STYLE_FILE="${DATA_DIR}/osm2pgsql.style"

cat > "$STYLE_FILE" << 'EOF'
# osm2pgsql style file for GeoEdu Service
# Optimized for educational geography content

# Common tags
node,way   access        text         linear
node,way   addr:housename text        linear
node,way   addr:housenumber text      linear
node,way   addr:interpolation text    linear
node,way   admin_level   text         linear
node,way   aerialway     text         linear
node,way   amenity       text         polygon
node,way   area          text         linear
node,way   barrier       text         linear
node,way   bicycle       text         linear
node,way   boundary      text         linear
node,way   bridge        text         linear
node,way   building      text         polygon
node,way   construction  text         linear
node,way   covered       text         linear
node,way   foot          text         linear
node,way   highway       text         linear
node,way   historic      text         polygon
node,way   junction      text         linear
node,way   landuse       text         polygon
node,way   layer         text         linear
node,way   leisure       text         polygon
node,way   man_made      text         polygon
node,way   military      text         polygon
node,way   name          text         linear
node,way   natural       text         polygon
node,way   oneway        text         linear
node,way   place         text         polygon
node,way   power         text         polygon
node,way   railway       text         linear
node,way   ref           text         linear
node,way   religion      text         linear
node,way   route         text         linear
node,way   service       text         linear
node,way   shop          text         polygon
node,way   sport         text         polygon
node,way   surface       text         linear
node,way   tourism       text         polygon
node,way   tracktype     text         linear
node,way   tunnel        text         linear
node,way   water         text         polygon
node,way   waterway      text         linear
node,way   wetland       text         polygon
node,way   wood          text         polygon
node,way   z_order       int4         linear

# Elevation data from OSM
node,way   ele           text         linear

# Name translations
node,way   name:de       text         linear
node,way   name:en       text         linear

# Population for place rendering
node,way   population    text         linear

# Way area for polygon ordering
way        way_area      real         linear
EOF

echo ""
echo -e "${GREEN}[1/3] PostGIS Extension aktivieren...${NC}"

PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "CREATE EXTENSION IF NOT EXISTS postgis;"
PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "CREATE EXTENSION IF NOT EXISTS hstore;"

echo ""
echo -e "${GREEN}[2/3] OSM Daten importieren...${NC}"
echo "       Dies dauert mehrere Stunden!"
echo ""

# Run osm2pgsql
PGPASSWORD="$DB_PASS" osm2pgsql \
    -H "$DB_HOST" \
    -P "$DB_PORT" \
    -U "$DB_USER" \
    -d "$DB_NAME" \
    -S "$STYLE_FILE" \
    --cache 4000 \
    --number-processes 4 \
    $SLIM_MODE \
    $DROP_MODE \
    "$PBF_FILE"

echo ""
echo -e "${GREEN}[3/3] Indizes erstellen...${NC}"

PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" << 'EOSQL'
-- Spatial indexes (if not created by osm2pgsql)
CREATE INDEX IF NOT EXISTS planet_osm_point_way_idx ON planet_osm_point USING GIST (way);
CREATE INDEX IF NOT EXISTS planet_osm_line_way_idx ON planet_osm_line USING GIST (way);
CREATE INDEX IF NOT EXISTS planet_osm_polygon_way_idx ON planet_osm_polygon USING GIST (way);
CREATE INDEX IF NOT EXISTS planet_osm_roads_way_idx ON planet_osm_roads USING GIST (way);

-- Name indexes for search
CREATE INDEX IF NOT EXISTS planet_osm_point_name_idx ON planet_osm_point (name);
CREATE INDEX IF NOT EXISTS planet_osm_polygon_name_idx ON planet_osm_polygon (name);

-- Vacuum analyze for query optimization
VACUUM ANALYZE planet_osm_point;
VACUUM ANALYZE planet_osm_line;
VACUUM ANALYZE planet_osm_polygon;
VACUUM ANALYZE planet_osm_roads;
EOSQL

echo ""
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}Import abgeschlossen!${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""
echo "Tabellen erstellt:"
echo "  - planet_osm_point"
echo "  - planet_osm_line"
echo "  - planet_osm_polygon"
echo "  - planet_osm_roads"
echo ""
echo -e "${YELLOW}Naechster Schritt:${NC}"
echo "  ./generate_tiles.sh  # PMTiles generieren"
