-- name: UpsertInstrument :exec
INSERT INTO instruments (id, market, venue, symbol, name, currency, provider_symbols, asset_type)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (id) DO UPDATE SET
    market = EXCLUDED.market,
    venue = EXCLUDED.venue,
    symbol = EXCLUDED.symbol,
    name = EXCLUDED.name,
    currency = EXCLUDED.currency,
    provider_symbols = EXCLUDED.provider_symbols,
    asset_type = EXCLUDED.asset_type;

-- name: GetInstrument :one
SELECT id, market, venue, symbol, name, currency, provider_symbols, asset_type
FROM instruments
WHERE id = $1;

-- name: ListInstruments :many
SELECT id, market, venue, symbol, name, currency, provider_symbols, asset_type
FROM instruments;

-- name: UpsertBar :exec
INSERT INTO bars (instrument_id, timeframe, timestamp, open, high, low, close, volume)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (instrument_id, timeframe, timestamp) DO UPDATE SET
    open = EXCLUDED.open,
    high = EXCLUDED.high,
    low = EXCLUDED.low,
    close = EXCLUDED.close,
    volume = EXCLUDED.volume;

-- name: GetBars :many
SELECT instrument_id, timeframe, timestamp, open, high, low, close, volume
FROM bars
WHERE instrument_id = $1 AND timeframe = $2 AND timestamp >= $3 AND timestamp <= $4
ORDER BY timestamp ASC;

-- name: UpsertRun :exec
INSERT INTO backtest_runs (id, strategy_id, market, timeframe, from_time, to_time, status, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (id) DO UPDATE SET
    strategy_id = EXCLUDED.strategy_id,
    market = EXCLUDED.market,
    timeframe = EXCLUDED.timeframe,
    from_time = EXCLUDED.from_time,
    to_time = EXCLUDED.to_time,
    status = EXCLUDED.status,
    created_at = EXCLUDED.created_at,
    updated_at = EXCLUDED.updated_at;

-- name: GetRun :one
SELECT id, strategy_id, market, timeframe, from_time, to_time, status, created_at, updated_at
FROM backtest_runs
WHERE id = $1;

-- name: ListRuns :many
SELECT id, strategy_id, market, timeframe, from_time, to_time, status, created_at, updated_at
FROM backtest_runs
WHERE (@status::text = '' OR status = @status::text)
ORDER BY created_at DESC, id ASC;

-- name: UpsertResult :exec
INSERT INTO backtest_results (run_id, starting_cash_currency, starting_cash_amount, ending_equity_currency, ending_equity_amount, trades, positions, total_return, trade_count, equity_curve)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ON CONFLICT (run_id) DO UPDATE SET
    starting_cash_currency = EXCLUDED.starting_cash_currency,
    starting_cash_amount = EXCLUDED.starting_cash_amount,
    ending_equity_currency = EXCLUDED.ending_equity_currency,
    ending_equity_amount = EXCLUDED.ending_equity_amount,
    trades = EXCLUDED.trades,
    positions = EXCLUDED.positions,
    total_return = EXCLUDED.total_return,
    trade_count = EXCLUDED.trade_count,
    equity_curve = EXCLUDED.equity_curve;

-- name: GetResult :one
SELECT run_id, starting_cash_currency, starting_cash_amount, ending_equity_currency, ending_equity_amount, trades, positions, total_return, trade_count, equity_curve
FROM backtest_results
WHERE run_id = $1;
