#!/bin/bash
# Direct MySQL to PostgreSQL migration script
# Uses mysqldump and psql for direct data transfer

set -e

# Configuration
MYSQL_HOST="72.60.226.4"
MYSQL_PORT="3306"
MYSQL_USER="root"
MYSQL_PASS="AfcItz2391,."
MYSQL_DB="db_trading_meta"

PG_HOST="localhost"
PG_PORT="5432"
PG_USER="orbiquant_user"
PG_PASS="orbiquant_dev_2025"
PG_DB="orbiquant_trading"

# Ticker mapping: MySQL symbol -> (PG symbol, ticker_id)
declare -A TICKER_MAP=(
    ["X:BTCUSD"]="1"
    ["C:EURUSD"]="2"
    ["C:GBPUSD"]="3"
    ["C:USDJPY"]="4"
    ["C:USDCAD"]="5"
    ["C:AUDUSD"]="6"
    ["C:NZDUSD"]="7"
    ["C:EURGBP"]="8"
    ["C:EURAUD"]="9"
    ["C:EURCHF"]="10"
    ["C:GBPJPY"]="11"
    ["C:GBPAUD"]="12"
    ["C:GBPCAD"]="13"
    ["C:GBPNZD"]="14"
    ["C:AUDCAD"]="15"
    ["C:AUDCHF"]="16"
    ["C:AUDNZD"]="17"
    ["C:XAUUSD"]="18"
)

migrate_ticker() {
    local mysql_ticker=$1
    local pg_ticker_id=$2

    echo "Migrating $mysql_ticker (ticker_id=$pg_ticker_id)..."

    # Export from MySQL and import to PostgreSQL
    mysql -h $MYSQL_HOST -P $MYSQL_PORT -u $MYSQL_USER -p"$MYSQL_PASS" $MYSQL_DB \
        -N -e "SELECT $pg_ticker_id, date_agg, open, high, low, close, volume, vwap, ts FROM tickers_agg_data WHERE ticker='$mysql_ticker' ORDER BY date_agg" \
        | PGPASSWORD=$PG_PASS psql -h $PG_HOST -p $PG_PORT -U $PG_USER -d $PG_DB \
        -c "COPY market_data.ohlcv_5m (ticker_id, timestamp, open, high, low, close, volume, vwap, ts_epoch) FROM STDIN WITH (FORMAT csv, DELIMITER E'\t')"

    echo "Completed $mysql_ticker"
}

# Main
echo "Starting migration..."
echo "MySQL: $MYSQL_HOST:$MYSQL_PORT/$MYSQL_DB"
echo "PostgreSQL: $PG_HOST:$PG_PORT/$PG_DB"
echo ""

# Migrate specific ticker or all
if [ -n "$1" ]; then
    # Migrate specific ticker
    mysql_ticker=$1
    pg_ticker_id=${TICKER_MAP[$mysql_ticker]}
    if [ -z "$pg_ticker_id" ]; then
        echo "Unknown ticker: $mysql_ticker"
        exit 1
    fi
    migrate_ticker "$mysql_ticker" "$pg_ticker_id"
else
    # Migrate all tickers
    for mysql_ticker in "${!TICKER_MAP[@]}"; do
        pg_ticker_id=${TICKER_MAP[$mysql_ticker]}
        migrate_ticker "$mysql_ticker" "$pg_ticker_id"
    done
fi

echo ""
echo "Migration complete!"

# Verify counts
echo ""
echo "Verification:"
PGPASSWORD=$PG_PASS psql -h $PG_HOST -p $PG_PORT -U $PG_USER -d $PG_DB \
    -c "SELECT t.symbol, COUNT(*) as rows FROM market_data.ohlcv_5m o JOIN market_data.tickers t ON t.id = o.ticker_id GROUP BY t.symbol ORDER BY t.symbol"
