package models

import (
	"WorldEpcho/src/datasource"
	"fmt"
)

/*
   构建推演结果记录表，当世界推演检测到推演结束时，保存推演结局信息，结局标题，内容。
    世界ID | 用户ID | 世界名称|  结局标题 | 结局内容
*/

type WorldDeductionResult struct {
	Id             int64  `xorm:"pk autoincr"`               // 主键，自增
	WorldId        int64  `xorm:" 'world_id' notnull   " `   // 世界ID
	UserId         int64  `xorm:" 'user_id' notnull  "`      // 用户ID
	WorldName      string `xorm:"'world_name' "`             // 世界名称
	OutcomeTitle   string `xorm:" 'outcome_title' "`         // 结局标题
	OutcomeContent string `xorm:" 'outcome_content' text " ` // 结局内容
	ChatStartTime  int64  `xorm:" 'chat_start_time' "`       //本轮聊天开始时间
	ChatEndTime    int64  `xorm:" 'chat_end_time' "`         //本轮聊天结束时间
	Score          int    `xorm:" 'score' "`                 //得分
	//CreatedAt      int64  `xorm:" 'created_at' "`            // 创建时间
	//UpdatedAt      int64  `xorm:" 'updated_at' "`            // 更新时间
}

// AddWorldDeductionResult 添加一条推演结果记录
func AddWorldDeductionResult(result *WorldDeductionResult) (int64, error) {
	affected, err := datasource.Engine.Insert(result)
	if err != nil {
		return 0, fmt.Errorf("error adding world deduction result: %v", err)
	}
	return affected, nil

}

// GetWorldDeductionResultByID 根据ID查询推演结果记录
func GetWorldDeductionResultByID(id int64) (*WorldDeductionResult, error) {
	var result WorldDeductionResult
	has, err := datasource.Engine.Id(id).Get(&result)
	if err != nil {
		return nil, fmt.Errorf("error retrieving world deduction result: %v", err)
	}
	if !has {
		return nil, fmt.Errorf("no result found with id %d", id)
	}
	return &result, nil
}

// GetWorldDeductionResultsByWorldAndUser 根据世界ID和用户ID查询推演结果
func GetWorldDeductionResultsByWorldAndUser(worldId, userId int64) ([]WorldDeductionResult, error) {
	var results []WorldDeductionResult
	err := datasource.Engine.Where("world_id = ? AND user_id = ?", worldId, userId).Find(&results)
	if err != nil {
		return nil, fmt.Errorf("error retrieving world deduction results: %v", err)
	}
	return results, nil
}

// GetWorldDeductionResultsByWorldAndUser 根据世界ID和用户ID查询最近一次的推演结果
func GetWorldDeductionResultsByWorldNameAndUserId(userId int64) ([]WorldDeductionResult, error) {
	var result []WorldDeductionResult
	err := datasource.Engine.Where("user_id = ?", userId).
		Find(&result)
	if err != nil {
		return nil, fmt.Errorf("error retrieving world deduction results: %v", err)
	}
	return result, nil
}

// GetWorldDeductionResultsByWorldAndUser 根据世界ID和用户ID查询最近一次的推演结果
func GetWorldDeductionResultsByWorldNameAndUserLastTime(worldName string, userId int64) (*WorldDeductionResult, error) {
	var result WorldDeductionResult
	has, err := datasource.Engine.Where("world_name = ? AND user_id = ?", worldName, userId).
		OrderBy("created_at DESC").Get(&result)
	if err != nil {
		return nil, fmt.Errorf("error retrieving world deduction results: %v", err)
	}
	if !has {
		return nil, nil // 如果没有找到结果，返回nil
	}
	return &result, nil
}

// DeleteWorldDeductionResult 删除指定ID的推演结果记录
func DeleteWorldDeductionResult(id int64) (int64, error) {
	affected, err := datasource.Engine.Id(id).Delete(&WorldDeductionResult{})
	if err != nil {
		return 0, fmt.Errorf("error deleting world deduction result: %v", err)
	}
	return affected, nil
}
