package srequest

import (
	"github.com/tech/sendico/pkg/merrors"
	paymenttypes "github.com/tech/sendico/pkg/payments/types"
)

type PaymentIntent struct {
	Kind           PaymentKind         `json:"kind,omitempty"`
	Source         *Endpoint           `json:"source,omitempty"`
	Destination    *Endpoint           `json:"destination,omitempty"`
	Amount         *paymenttypes.Money `json:"amount,omitempty"`
	FX             *FXIntent           `json:"fx,omitempty"`
	SettlementMode SettlementMode      `json:"settlement_mode,omitempty"`
	FeeTreatment   FeeTreatment        `json:"fee_treatment,omitempty"`
	Attributes     map[string]string   `json:"attributes,omitempty"`
	Comment        string              `json:"comment,omitempty"`
	Customer       *Customer           `json:"customer,omitempty"`
}

type AssetResolverStub struct{}

func (a *AssetResolverStub) IsSupported(_ string) bool {
	return true
}

func (p *PaymentIntent) Validate() error {
	// Kind must be set (non-zero)
	var zeroKind PaymentKind
	if p.Kind == zeroKind {
		return merrors.InvalidArgument("kind is required", "intent.kind")
	}

	if p.Source == nil {
		return merrors.InvalidArgument("source is required", "intent.source")
	}

	if p.Destination == nil {
		return merrors.InvalidArgument("destination is required", "intent.destination")
	}

	if p.Amount == nil {
		return merrors.InvalidArgument("amount is required", "intent.amount")
	}
	//TODO: collect supported currencies and validate against them
	if err := ValidateMoney(p.Amount, &AssetResolverStub{}); err != nil {
		return err
	}

	if p.FX != nil {
		if err := p.FX.Validate(); err != nil {
			return err
		}
	}

	return nil
}
