package storage

import (
	"context"
	"errors"
	"time"

	"github.com/tech/sendico/fx/storage/model"
)

var (
	ErrQuoteExpired         = errors.New("fx.storage: quote expired")
	ErrQuoteConsumed        = errors.New("fx.storage: quote consumed")
	ErrQuoteNotFirm         = errors.New("fx.storage: quote is not firm")
	ErrQuoteConsumptionRace = errors.New("fx.storage: quote consumption collision")
)

type Repository interface {
	Ping(ctx context.Context) error
	Rates() RatesStore
	Quotes() QuotesStore
	Pairs() PairStore
	Currencies() CurrencyStore
}

type RatesStore interface {
	UpsertSnapshot(ctx context.Context, snapshot *model.RateSnapshot) error
	LatestSnapshot(ctx context.Context, pair model.CurrencyPair, provider string) (*model.RateSnapshot, error)
}

type QuotesStore interface {
	Issue(ctx context.Context, quote *model.Quote) error
	GetByRef(ctx context.Context, quoteRef string) (*model.Quote, error)
	Consume(ctx context.Context, quoteRef, ledgerTxnRef string, when time.Time) (*model.Quote, error)
	ExpireIssuedBefore(ctx context.Context, cutoff time.Time) (int, error)
}

type PairStore interface {
	ListEnabled(ctx context.Context) ([]*model.Pair, error)
	Get(ctx context.Context, pair model.CurrencyPair) (*model.Pair, error)
	Upsert(ctx context.Context, p *model.Pair) error
}

type CurrencyStore interface {
	Get(ctx context.Context, code string) (*model.Currency, error)
	List(ctx context.Context, codes ...string) ([]*model.Currency, error)
	Upsert(ctx context.Context, currency *model.Currency) error
}
