package model

import (
	"strings"
	"time"

	"github.com/tech/sendico/pkg/db/storable"
	"github.com/tech/sendico/pkg/mservice"
	moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
)

type DepositStatus string

const (
	DepositStatusPending   DepositStatus = "pending"
	DepositStatusConfirmed DepositStatus = "confirmed"
	DepositStatusFailed    DepositStatus = "failed"
)

// Deposit records an inbound transfer observed on-chain.
type Deposit struct {
	storable.Base `bson:",inline" json:",inline"`

	DepositRef      string         `bson:"depositRef" json:"depositRef"`
	WalletRef       string         `bson:"walletRef" json:"walletRef"`
	Network         string         `bson:"network" json:"network"`
	TokenSymbol     string         `bson:"tokenSymbol" json:"tokenSymbol"`
	ContractAddress string         `bson:"contractAddress" json:"contractAddress"`
	Amount          *moneyv1.Money `bson:"amount" json:"amount"`
	SourceAddress   string         `bson:"sourceAddress" json:"sourceAddress"`
	TxHash          string         `bson:"txHash" json:"txHash"`
	BlockID         string         `bson:"blockId,omitempty" json:"blockId,omitempty"`
	Status          DepositStatus  `bson:"status" json:"status"`
	ObservedAt      time.Time      `bson:"observedAt" json:"observedAt"`
	RecordedAt      time.Time      `bson:"recordedAt" json:"recordedAt"`
	LastStatusAt    time.Time      `bson:"lastStatusAt" json:"lastStatusAt"`
}

// Collection implements storable.Storable.
func (*Deposit) Collection() string {
	return mservice.ChainDeposits
}

// Normalize standardizes case-sensitive fields.
func (d *Deposit) Normalize() {
	d.DepositRef = strings.TrimSpace(d.DepositRef)
	d.WalletRef = strings.TrimSpace(d.WalletRef)
	d.Network = strings.TrimSpace(strings.ToLower(d.Network))
	d.TokenSymbol = strings.TrimSpace(strings.ToUpper(d.TokenSymbol))
	d.ContractAddress = strings.TrimSpace(strings.ToLower(d.ContractAddress))
	d.SourceAddress = strings.TrimSpace(strings.ToLower(d.SourceAddress))
	d.TxHash = strings.TrimSpace(strings.ToLower(d.TxHash))
	d.BlockID = strings.TrimSpace(d.BlockID)
}
