#!/bin/bash

# Configuration
CONTAINER_NAME="line-of-sight-db"
DB_USER="line_of_sight"
DB_NAME="line_of_sight"
DATA_FILE="./docker/data/cities.csv.gz"

# Check if docker is available using the known path
DOCKER_BIN="/usr/local/bin/docker"
if [ ! -f "$DOCKER_BIN" ]; then
    DOCKER_BIN=$(which docker)
fi

if [ -z "$DOCKER_BIN" ]; then
    echo "Error: docker command not found."
    exit 1
fi

# Check if data file exists
if [ ! -f "$DATA_FILE" ]; then
    echo "Error: Data file $DATA_FILE not found."
    exit 1
fi

# Check if container is running
if ! "$DOCKER_BIN" ps | grep -q "$CONTAINER_NAME"; then
    echo "Error: Container $CONTAINER_NAME is not running."
    echo "Please run 'docker-compose up -d' first."
    exit 1
fi

echo "🚀 Starting database initialization..."

# Use a HEREDOC to execute multi-line SQL
# We load into a temp table first to handle the WKT -> Geography conversion
IMPORT_SQL=$(cat <<EOF
CREATE TEMP TABLE tmp_cities (
    name VARCHAR(255),
    population INTEGER,
    country VARCHAR(100),
    geom_wkt TEXT
);

COPY tmp_cities FROM STDIN WITH CSV HEADER;

TRUNCATE cities;

INSERT INTO cities (name, population, country, geom)
SELECT 
    name, 
    population, 
    country, 
    ST_GeomFromText(geom_wkt, 4326)::geography
FROM tmp_cities;

DROP TABLE tmp_cities;

SELECT count(*) || ' cities successfully imported.' as result FROM cities;
EOF
)

# Stream the gzipped data into psql
gunzip -c "$DATA_FILE" | "$DOCKER_BIN" exec -i -e PGPASSWORD=line_of_sight_pass "$CONTAINER_NAME" psql -U "$DB_USER" -d "$DB_NAME" -h 127.0.0.1 -c "$IMPORT_SQL"

echo "✅ Initialization complete."
