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

set -eu

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

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

rate-limited-investments() {
    wait-api-rate-limits 1
    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-external-exchanges ib-stock-split ib-reverse-stock-split \
    ib-reverse-stock-split-reverse-order ib-simple-with-lse ib-symbol-with-space ib-tax-remapping \
    ib-stock-yield-enhancement-program-not-received-yet tinkoff-complex; do
    investments other tax-statement "$portfolio"
done

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

rate-limited-investments other simulate-sell open-first-iia-a
rate-limited-investments other simulate-sell ib-external-exchanges all IBKR
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
rate-limited-investments other simulate-sell tinkoff-mixed-currency-trade all VTBA all VTBX