package models

import (
	database "WorldEpcho/src/datasource"
	"fmt"
	"github.com/go-xorm/xorm"
)

// UserSummaryInspirationUsageLog 用户灵感消耗汇总
type UserSummaryInspirationUsageLog struct {
	UserId                     int64 `xorm:"pk"`                                                                         // 用户ID
	MonthLimitAmount           int64 `xorm:"month_limit_amount"`                                                         // 月限额
	MonthlyUsage               int64 `xorm:"monthly_usage"`                                                              // 月度使用额
	InitialInspiration         int64 `xorm:"initial_inspiration" json:"initial_inspiration"`                             //初始总额度
	SummaryInspirationConsumed int64 `xorm:"summary_inspiration_consumed default 0" json:"summary_inspiration_consumed"` // 汇总灵感消耗量

}

// 增加记录
func AddUserSummaryInspirationUsageLog(log *UserSummaryInspirationUsageLog) (int64, error) {
	return database.Engine.Insert(log)
}

// 增加用户总的灵感消耗记录
func InsertUserSummaryInspirationUsageLog_Tx(session *xorm.Session, log *UserSummaryInspirationUsageLog) (int64, error) {
	return session.Insert(log)
}

// 更新记录
func UpdateLog(log *UserSummaryInspirationUsageLog) (int64, error) {
	return database.Engine.Where("user_id = ?", log.UserId).Update(log)
}

func UpdateLog_Tx(ession *xorm.Session, log *UserSummaryInspirationUsageLog) (int64, error) {
	return ession.Where("user_id = ?", log.UserId).Update(log)
}

func UpdateUserSummaryInspirationUsageLog(session *xorm.Session, log *UserSummaryInspirationUsageLog) (int64, error) {
	return session.Where("user_id = ?", log.UserId).Update(log)
}

// 查询单个记录
func GetUserSummaryInspirationUsageLog(userId int64) (*UserSummaryInspirationUsageLog, bool, error) {
	var log UserSummaryInspirationUsageLog
	has, err := database.Engine.Where("user_id = ?", userId).Get(&log)
	return &log, has, err
}

// 查询所有记录
func GetAllLogs() ([]UserSummaryInspirationUsageLog, error) {
	var logs []UserSummaryInspirationUsageLog
	err := database.Engine.Find(&logs)
	return logs, err
}

// 删除记录
func DeleteUserSummaryInspirationUsageLog(userId int64) (int64, error) {
	return database.Engine.Where("user_id = ?", userId).Delete(&UserSummaryInspirationUsageLog{})
}

// GetLastChatTime retrieves the last chat time for a given user, app, and dp
func GetLastChatTime(userID, appID, dpID int64) (int64, error) {
	// Define a slice to store the query result
	var logs []UserInspirationUsageLog

	// Perform the query
	err := database.Engine.Where("user_id = ?", userID).
		And("dp_id = ?", dpID).
		And("app_id = ?", appID).
		OrderBy("chat_time desc").
		Limit(1).
		Find(&logs)

	if err != nil {
		return 0, err
	}

	// If there is a result, return the last chat time
	if len(logs) > 0 {
		return logs[0].ChatTime, nil
	}

	// No chat records found for the specified criteria
	return 0, fmt.Errorf("no chat records found for the specified criteria")
}

// 根据用户ID查询用户可用的月限额
func GetUserAvailableMonthlyLimit(userId int64) (int64, bool, error) {
	var userLog UserSummaryInspirationUsageLog
	has, err := database.Engine.Where("user_id = ?", userId).Get(&userLog)
	if err != nil {
		return 0, false, err
	}
	if !has {
		return 0, false, nil
	}

	available := userLog.MonthLimitAmount - userLog.MonthlyUsage
	return available, true, nil
}
