package rest

import (
	"fmt"

	"github.com/shopspring/decimal"
)

type HTTPError struct {
	Error string
}

type Msg struct {
	Text string
}

type Credentials struct {
	Email    string `json:"email" binding:"required" example:"user1"`
	Password string `json:"password" binding:"required" example:"password1"`
}

type Session struct {
	Email string
}

type QuoteRequest struct {
	ClOrdID   string `json:"cl_ord_id" binding:"required"`
	QuoteID   string `json:"quote_id" binding:"required"`
	Symbol    string `json:"symbol" binding:"required"`
	Currency  string `json:"currency"`
	BidPx     string `json:"bid_px" binding:"required"`
	OfferPx   string `json:"offer_px" binding:"required"`
	BidSize   string `json:"bid_size"`
	OfferSize string `json:"offer_size"`
}

func (r QuoteRequest) toDecimals() (bidPx, offerPx, bidSize, offerSize decimal.Decimal, err error) {
	bidPx, err = decimal.NewFromString(r.BidPx)
	if err != nil {
		return bidPx, offerPx, bidSize, offerSize, fmt.Errorf("invalid bid_px: %w", err)
	}

	offerPx, err = decimal.NewFromString(r.OfferPx)
	if err != nil {
		return bidPx, offerPx, bidSize, offerSize, fmt.Errorf("invalid offer_px: %w", err)
	}

	if r.BidSize != "" {
		bidSize, err = decimal.NewFromString(r.BidSize)
		if err != nil {
			return bidPx, offerPx, bidSize, offerSize, fmt.Errorf("invalid bid_size: %w", err)
		}
	}

	if r.OfferSize != "" {
		offerSize, err = decimal.NewFromString(r.OfferSize)
		if err != nil {
			return bidPx, offerPx, bidSize, offerSize, fmt.Errorf("invalid offer_size: %w", err)
		}
	}

	return bidPx, offerPx, bidSize, offerSize, nil
}
