package main

import (
	"fmt"
	"log"

	"github.com/gothinkster/golang-gin-realworld-example-app/common"
	"github.com/gothinkster/golang-gin-realworld-example-app/config"
)

type Asset struct {
	ID              int64  `gorm:"column:id"`
	AssetCode       string `gorm:"column:asset_code"`
	Name            string `gorm:"column:name"`
	ContractAddress string `gorm:"column:contract_address"`
}

func main() {
	// Load configuration
	cfg := config.Load()

	// Initialize database
	if cfg.DBType == "mysql" {
		common.InitMySQL()
	} else {
		common.Init()
	}

	db := common.GetDB()

	// Query YT assets
	var assets []Asset
	err := db.Table("assets").
		Where("asset_code IN (?, ?, ?)", "YT-A", "YT-B", "YT-C").
		Find(&assets).Error
	if err != nil {
		log.Fatalf("Failed to query assets: %v", err)
	}

	fmt.Println("=== 数据库中的 YT 合约地址 ===")
	for _, asset := range assets {
		fmt.Printf("%s: %s\n", asset.AssetCode, asset.ContractAddress)
	}

	// Query YTLPToken from system_contracts
	var ytlp struct {
		Name    string `gorm:"column:name"`
		Address string `gorm:"column:address"`
	}
	err = db.Table("system_contracts").
		Where("name = ? AND is_active = ?", "YTLPToken", 1).
		Select("name, address").
		First(&ytlp).Error
	if err != nil {
		log.Printf("Failed to query YTLPToken: %v", err)
	} else {
		fmt.Println("\n=== ytLP 地址 ===")
		fmt.Printf("%s: %s\n", ytlp.Name, ytlp.Address)
	}

}
