package models

import (
	"WorldEpcho/src/datasource"
	"errors"
	"github.com/go-xorm/xorm"
	"log"
)

// SoulAsset 资产表结构
type SoulAsset struct {
	Id              int64  `xorm:"pk autoincr" json:"asset_id"`
	DigitalId       int64  `xorm:"notnull" json:"digital_id"`
	ModelDriverType string `json:"model_driver_type"`
	ModelPath       string `json:"model_path"` //模型资产路径
	VoiceType       string `json:"voice_type"` //人物声线类型
}

/*
   定义一个结构体类型DigitalPersonInfo用于保存数字人信息和数字人资产信息
*/
type DigitalPersonInfo struct {
	DigitalPerson *DigitalPerson `json:"digital_person"`
	SoulAsset     *SoulAsset     `json:"soul_asset"`
}

// ModelDriver 模型驱动枚举类型
type ModelDriver string

const (
	ModelDriver2D   ModelDriver = "2d"
	ModelDriver3D   ModelDriver = "3d"
	ModelDriverLive ModelDriver = "live2d"
)

// getAssets 查询所有资产
func GetAssets() ([]SoulAsset, error) {
	var assets []SoulAsset
	err := datasource.Engine.Find(&assets)
	if err != nil {
		return nil, err
	}
	return assets, nil
}

// createAsset 创建新的数字人资产
func CreateAsset(asset *SoulAsset, session *xorm.Session) error {
	_, err := session.Insert(asset)
	if err != nil {
		return errors.New("创建资产对象失败! ")
	}
	return nil
}

// deleteAsset 删除资产
func DeleteAsset(id string) error {
	_, err := datasource.Engine.Where("id = ?", id).Delete(&SoulAsset{})
	if err != nil {
		return err
	}
	return nil
}

// updateAsset 更新数字人资产
func UpdateAsset(digital_id int64, asset *SoulAsset, session *xorm.Session) error {
	_, err := session.Where("digital_id = ?", digital_id).Update(asset)
	if err != nil {
		return err
	}
	log.Println("UpdateAsset ...after...  asset", asset)
	return nil
}

// 从资产表中查询资产对象
func GetSoulAsset(id int64, session *xorm.Session) (SoulAsset, error) {
	var asset SoulAsset
	has, err := session.ID(id).Get(&asset)
	if err != nil {
		return asset, err
	}
	if !has {
		return asset, errors.New("Asset not found")
	}
	return asset, nil
}
