// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: queries.sql

package sqlc

import (
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

const getBars = `-- 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
`

type GetBarsParams struct {
	InstrumentID string
	Timeframe    string
	Timestamp    pgtype.Timestamptz
	Timestamp_2  pgtype.Timestamptz
}

func (q *Queries) GetBars(ctx context.Context, arg GetBarsParams) ([]Bar, error) {
	rows, err := q.db.Query(ctx, getBars,
		arg.InstrumentID,
		arg.Timeframe,
		arg.Timestamp,
		arg.Timestamp_2,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Bar
	for rows.Next() {
		var i Bar
		if err := rows.Scan(
			&i.InstrumentID,
			&i.Timeframe,
			&i.Timestamp,
			&i.Open,
			&i.High,
			&i.Low,
			&i.Close,
			&i.Volume,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

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

func (q *Queries) GetInstrument(ctx context.Context, id string) (Instrument, error) {
	row := q.db.QueryRow(ctx, getInstrument, id)
	var i Instrument
	err := row.Scan(
		&i.ID,
		&i.Market,
		&i.Venue,
		&i.Symbol,
		&i.Name,
		&i.Currency,
		&i.ProviderSymbols,
		&i.AssetType,
	)
	return i, err
}

const getResult = `-- 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
`

func (q *Queries) GetResult(ctx context.Context, runID string) (BacktestResult, error) {
	row := q.db.QueryRow(ctx, getResult, runID)
	var i BacktestResult
	err := row.Scan(
		&i.RunID,
		&i.StartingCashCurrency,
		&i.StartingCashAmount,
		&i.EndingEquityCurrency,
		&i.EndingEquityAmount,
		&i.Trades,
		&i.Positions,
		&i.TotalReturn,
		&i.TradeCount,
		&i.EquityCurve,
	)
	return i, err
}

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

func (q *Queries) GetRun(ctx context.Context, id string) (BacktestRun, error) {
	row := q.db.QueryRow(ctx, getRun, id)
	var i BacktestRun
	err := row.Scan(
		&i.ID,
		&i.StrategyID,
		&i.Market,
		&i.Timeframe,
		&i.FromTime,
		&i.ToTime,
		&i.Status,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listInstruments = `-- name: ListInstruments :many
SELECT id, market, venue, symbol, name, currency, provider_symbols, asset_type
FROM instruments
`

func (q *Queries) ListInstruments(ctx context.Context) ([]Instrument, error) {
	rows, err := q.db.Query(ctx, listInstruments)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Instrument
	for rows.Next() {
		var i Instrument
		if err := rows.Scan(
			&i.ID,
			&i.Market,
			&i.Venue,
			&i.Symbol,
			&i.Name,
			&i.Currency,
			&i.ProviderSymbols,
			&i.AssetType,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

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

func (q *Queries) ListRuns(ctx context.Context, status string) ([]BacktestRun, error) {
	rows, err := q.db.Query(ctx, listRuns, status)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BacktestRun
	for rows.Next() {
		var i BacktestRun
		if err := rows.Scan(
			&i.ID,
			&i.StrategyID,
			&i.Market,
			&i.Timeframe,
			&i.FromTime,
			&i.ToTime,
			&i.Status,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertBar = `-- 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
`

type UpsertBarParams struct {
	InstrumentID string
	Timeframe    string
	Timestamp    pgtype.Timestamptz
	Open         pgtype.Numeric
	High         pgtype.Numeric
	Low          pgtype.Numeric
	Close        pgtype.Numeric
	Volume       pgtype.Numeric
}

func (q *Queries) UpsertBar(ctx context.Context, arg UpsertBarParams) error {
	_, err := q.db.Exec(ctx, upsertBar,
		arg.InstrumentID,
		arg.Timeframe,
		arg.Timestamp,
		arg.Open,
		arg.High,
		arg.Low,
		arg.Close,
		arg.Volume,
	)
	return err
}

const upsertInstrument = `-- 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
`

type UpsertInstrumentParams struct {
	ID              string
	Market          string
	Venue           string
	Symbol          string
	Name            string
	Currency        string
	ProviderSymbols []byte
	AssetType       string
}

func (q *Queries) UpsertInstrument(ctx context.Context, arg UpsertInstrumentParams) error {
	_, err := q.db.Exec(ctx, upsertInstrument,
		arg.ID,
		arg.Market,
		arg.Venue,
		arg.Symbol,
		arg.Name,
		arg.Currency,
		arg.ProviderSymbols,
		arg.AssetType,
	)
	return err
}

const upsertResult = `-- 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
`

type UpsertResultParams struct {
	RunID                string
	StartingCashCurrency string
	StartingCashAmount   pgtype.Numeric
	EndingEquityCurrency string
	EndingEquityAmount   pgtype.Numeric
	Trades               []byte
	Positions            []byte
	TotalReturn          pgtype.Numeric
	TradeCount           int32
	EquityCurve          []byte
}

func (q *Queries) UpsertResult(ctx context.Context, arg UpsertResultParams) error {
	_, err := q.db.Exec(ctx, upsertResult,
		arg.RunID,
		arg.StartingCashCurrency,
		arg.StartingCashAmount,
		arg.EndingEquityCurrency,
		arg.EndingEquityAmount,
		arg.Trades,
		arg.Positions,
		arg.TotalReturn,
		arg.TradeCount,
		arg.EquityCurve,
	)
	return err
}

const upsertRun = `-- 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
`

type UpsertRunParams struct {
	ID         string
	StrategyID string
	Market     string
	Timeframe  string
	FromTime   pgtype.Timestamptz
	ToTime     pgtype.Timestamptz
	Status     string
	CreatedAt  pgtype.Timestamptz
	UpdatedAt  pgtype.Timestamptz
}

func (q *Queries) UpsertRun(ctx context.Context, arg UpsertRunParams) error {
	_, err := q.db.Exec(ctx, upsertRun,
		arg.ID,
		arg.StrategyID,
		arg.Market,
		arg.Timeframe,
		arg.FromTime,
		arg.ToTime,
		arg.Status,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	return err
}
