package models

import (
	database "WorldEpcho/src/datasource"
	"encoding/json"
	"fmt"
	"log"
)

// EmbuTestResult 定义了存储到数据库中的测试结果的数据结构
type EmbuTestResult struct {
	Id             int64  `xorm:"pk autoincr 'id'"` // 主键，自增
	AppId          int64  `xorm:"'appId'"`
	Uid            int64  `xorm:"'uid'"`
	TestData       string `xorm:"'testData' json"`       // 这里假设存储为JSON字符串
	EvaluateResult string `xorm:"'EvaluateResult' json"` // 结果同样存为JSON字符串
	EvaluateTime   int64  `xorm:" 'EvaluateTime'"`
	ShareType      string `json:"shareType" xorm:" 'ShareType' "` //分享类型, 使用逗号分割的字符串, 如: A,B,C,D

}

// TestResultDetail 定义了测试结果的详细数据结构
type FatherTestResultDetail struct {
	Emotional      string `json:"Emotional"`      //情感温暖理解
	Overreach      string `json:"Overreach"`      //过分干涉
	RejectDeny     string `json:"RejectDeny"`     //拒绝、否认
	Punishment     string `json:"Punishment"`     //惩罚
	Preferred      string `json:"Preferred"`      //偏爱被试
	OverProtection string `json:"OverProtection"` //过度保护 【注意】FartherAverage中特有的字段
}

type MotherTestResultDetail struct {
	Emotional  string `json:"Emotional"`  //情感温暖理解
	Overreach  string `json:"Overreach"`  //过分干涉
	RejectDeny string `json:"RejectDeny"` //拒绝、否认
	Punishment string `json:"Punishment"` //惩罚
	Preferred  string `json:"Preferred"`  //偏爱被试
}

// 测试数据结构体
type TestResultData struct {
	FatherAverage FatherTestResultDetail `json:"FatherAverage"`
	MotherAverage MotherTestResultDetail `json:"MotherAverage"`
}

// 数据库插入一条记录
func InsertTestResult(testResult *EmbuTestResult) error {
	_, err := database.Engine.Insert(testResult)
	if err != nil {
		return fmt.Errorf("插入数据失败: %v", err)
	}
	fmt.Println("数据插入成功")
	return nil
}

//查询汇总评估
// QueryLatestSummary 根据appId和uid查询最新的EvaluateResult中的Summary
/*
func QueryLatestSummary1(appId int64, uid int64) (*EvaluationSummary, *EvaluateResultVal, error) {
	var result EmbuTestResult
	has, err := database.Engine.Where("appId = ? AND uid = ?", appId, uid).Desc("EvaluateTime").Get(&result)
	if err != nil {
		return nil, nil, err
	}
	if !has {
		log.Printf("No records found for appId: %s, uid: %s", appId, uid)
		return nil, nil, nil // 或者返回特定的错误，表明没有找到记录
	}

	var evalResult struct {
		Summary EvaluationSummary `json:"Summary"`
	}
	if err := json.Unmarshal([]byte(result.EvaluateResult), &evalResult); err != nil {
		log.Printf("Error parsing evaluate result for ID %d: %v", result.Id, err)
		return nil, nil, err
	}
	return &evalResult.Summary, &result.EvaluateResult, nil
}
*/

// QueryLatestSummary 根据appId和uid查询最新的EvaluateResult中的Summary
func QueryLatestSummary(appId int64, uid int64) (*EvaluationSummary, *EvaluateResultVal, error) {
	var result EmbuTestResult
	has, err := database.Engine.Where("appId = ? AND uid = ?", appId, uid).Desc("EvaluateTime").Get(&result)
	if err != nil {
		return nil, nil, err
	}
	if !has {
		log.Printf("No records found for appId: %v, uid: %v", appId, uid)
		return nil, nil, nil // 或者返回特定的错误，表明没有找到记录
	}

	var evalResult EvaluateResultVal
	if err := json.Unmarshal([]byte(result.EvaluateResult), &evalResult); err != nil {
		log.Printf("Error parsing evaluate result for ID %v: %v", result.Id, err)
		return nil, nil, err
	}
	// 这里假设EvaluateResultVal中已经包含了Summary，所以直接返回evalResult.Summary
	return &evalResult.Summary, &evalResult, nil
}
