#!/usr/bin/env bash
#
# Updates regression tests input data
#

set -eu

wait-api-rate-limits() {
    local time=${1:-1}
    echo "Sleeping ${time}s to fit into API rate limits..."
    sleep ${1:-1}
}

investments() {
    local config="testdata/configs/$1"; shift
    echo "$@"
    cargo run --quiet -- --config "$config" "$@" > /dev/null
}

rate-limited-investments() {
    wait-api-rate-limits
    investments "$@"
}

(cd testdata && ./update)

INVESTMENTS_NOW=$(< testdata/time)
export INVESTMENTS_NOW

for broker in bcs firstrade ib iia kate kate-iia open tinkoff; do
    case "$broker" in
        firstrade|ib) cash=10000;;
        *) cash=100000;;
    esac

    investments main sync "$broker"
    investments main cash "$broker" "$cash"

    rate-limited-investments main analyse "$broker"
done

for portfolio in \
    ib-margin-rub ib-complex ib-stock-split ib-reverse-stock-split ib-reverse-stock-split-reverse-order \
    ib-simple-with-lse ib-symbol-with-space ib-tax-remapping tinkoff-complex; do
    investments other tax-statement "$portfolio"
done

wait-api-rate-limits 60
    investments other analyse ib-complex --all
    rate-limited-investments other simulate-sell ib-complex 1 VTRS
wait-api-rate-limits 60

rate-limited-investments other simulate-sell tinkoff-mixed-currency-trade all VTBA all VTBX
rate-limited-investments other simulate-sell ib-stock-split all AAPL
rate-limited-investments other simulate-sell ib-reverse-stock-split all AAPL all VISL