package dao

import (
	"gorm.io/gorm"
)

type EthAddressInfo struct {
	gorm.Model
	Name       string `gorm:"column:name" json:"name"`
	EthIndex   int64  `gorm:"column:eth_index" json:"eth_index"`
	EthAddress string `gorm:"type:varchar(200);column:eth_address;uniqueIndex" json:"eth_address"`
}

type AccountInfo interface {
	GetName() string
	GetIndex() int64
	GetAddr() string
}

func (eth *EthAddressInfo) GetName() string {
	return eth.Name
}
func (eth *EthAddressInfo) GetIndex() int64 {
	return eth.EthIndex
}
func (eth *EthAddressInfo) GetAddr() string {
	return eth.EthAddress
}

func (eth *EthAddressInfo) TableName() string {
	return "key_manager_eth"
}

func (dao *Dao) CreateEth(data *EthAddressInfo) error {
	return dao.db.Create(&data).Error
}

func (dao *Dao) GetEthFromAddress(addr string) (*EthAddressInfo, error) {
	var ethAddressInfo EthAddressInfo
	err := dao.db.Table(new(EthAddressInfo).TableName()).Where("eth_address=?", addr).First(&ethAddressInfo).Error
	return &ethAddressInfo, err
}

func (dao *Dao) GetEthFromIndex(name string, index int64) (*EthAddressInfo, error) {
	var ethAddressInfo EthAddressInfo
	err := dao.db.Table(new(EthAddressInfo).TableName()).Where("name=? AND eth_index=?", name, index).First(&ethAddressInfo).Error
	return &ethAddressInfo, err
}
