package app

import (
	"strconv"

	"git.echol.cn/loser/st/server/global"
	"git.echol.cn/loser/st/server/model/app/request"
	commonResponse "git.echol.cn/loser/st/server/model/common/response"
	"git.echol.cn/loser/st/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AIConfigApi struct{}

// CreateAIConfig
// @Tags     AppAIConfig
// @Summary  创建AI配置
// @Produce  application/json
// @Param    data  body      request.CreateAIConfigRequest                          true  "AI配置信息"
// @Success  200   {object}  commonResponse.Response{data=response.AIConfigResponse}  "创建成功"
// @Router   /app/ai-config [post]
// @Security ApiKeyAuth
func (a *AIConfigApi) CreateAIConfig(c *gin.Context) {
	var req request.CreateAIConfigRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.CreateAIConfig(&req)
	if err != nil {
		global.GVA_LOG.Error("创建AI配置失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}

// GetAIConfigList
// @Tags     AppAIConfig
// @Summary  获取AI配置列表
// @Produce  application/json
// @Success  200  {object}  commonResponse.Response{data=response.AIConfigListResponse}  "获取成功"
// @Router   /app/ai-config [get]
// @Security ApiKeyAuth
func (a *AIConfigApi) GetAIConfigList(c *gin.Context) {
	resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.GetAIConfigList()
	if err != nil {
		global.GVA_LOG.Error("获取AI配置列表失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithDetailed(commonResponse.PageResult{
		List:     resp.List,
		Total:    resp.Total,
		Page:     0,
		PageSize: 0,
	}, "获取成功", c)
}

// UpdateAIConfig
// @Tags     AppAIConfig
// @Summary  更新AI配置
// @Produce  application/json
// @Param    id    path      int                             true  "配置ID"
// @Param    data  body      request.UpdateAIConfigRequest   true  "AI配置信息"
// @Success  200   {object}  commonResponse.Response{msg=string}  "更新成功"
// @Router   /app/ai-config/:id [put]
// @Security ApiKeyAuth
func (a *AIConfigApi) UpdateAIConfig(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的配置ID", c)
		return
	}

	var req request.UpdateAIConfigRequest
	err = c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	err = service.ServiceGroupApp.AppServiceGroup.AIConfigService.UpdateAIConfig(uint(id), &req)
	if err != nil {
		global.GVA_LOG.Error("更新AI配置失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithMessage("更新成功", c)
}

// DeleteAIConfig
// @Tags     AppAIConfig
// @Summary  删除AI配置
// @Produce  application/json
// @Param    id   path      int  true  "配置ID"
// @Success  200  {object}  commonResponse.Response{msg=string}  "删除成功"
// @Router   /app/ai-config/:id [delete]
// @Security ApiKeyAuth
func (a *AIConfigApi) DeleteAIConfig(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的配置ID", c)
		return
	}

	err = service.ServiceGroupApp.AppServiceGroup.AIConfigService.DeleteAIConfig(uint(id))
	if err != nil {
		global.GVA_LOG.Error("删除AI配置失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithMessage("删除成功", c)
}

// GetModels
// @Tags     AppAIConfig
// @Summary  获取可用模型列表
// @Produce  application/json
// @Param    data  body      request.GetModelsRequest                          true  "获取模型请求"
// @Success  200   {object}  commonResponse.Response{data=response.GetModelsResponse}  "获取成功"
// @Router   /app/ai-config/models [post]
// @Security ApiKeyAuth
func (a *AIConfigApi) GetModels(c *gin.Context) {
	var req request.GetModelsRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.GetModels(&req)
	if err != nil {
		global.GVA_LOG.Error("获取模型列表失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}

// GetModelsByID
// @Tags     AppAIConfig
// @Summary  通过配置ID获取可用模型列表
// @Produce  application/json
// @Param    id   path      int                                                      true  "配置ID"
// @Success  200  {object}  commonResponse.Response{data=response.GetModelsResponse}  "获取成功"
// @Router   /app/ai-config/:id/models [get]
// @Security ApiKeyAuth
func (a *AIConfigApi) GetModelsByID(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的配置ID", c)
		return
	}

	resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.GetModelsByID(uint(id))
	if err != nil {
		global.GVA_LOG.Error("获取模型列表失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}

// TestAIConfig
// @Tags     AppAIConfig
// @Summary  测试AI配置
// @Produce  application/json
// @Param    data  body      request.TestAIConfigRequest                          true  "测试配置请求"
// @Success  200   {object}  commonResponse.Response{data=response.TestAIConfigResponse}  "测试完成"
// @Router   /app/ai-config/test [post]
// @Security ApiKeyAuth
func (a *AIConfigApi) TestAIConfig(c *gin.Context) {
	var req request.TestAIConfigRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.TestAIConfig(&req)
	if err != nil {
		global.GVA_LOG.Error("测试AI配置失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}

// TestAIConfigByID
// @Tags     AppAIConfig
// @Summary  通过ID测试AI配置
// @Produce  application/json
// @Param    id   path      int                                                      true  "配置ID"
// @Success  200  {object}  commonResponse.Response{data=response.TestAIConfigResponse}  "测试完成"
// @Router   /app/ai-config/:id/test [post]
// @Security ApiKeyAuth
func (a *AIConfigApi) TestAIConfigByID(c *gin.Context) {
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的配置ID", c)
		return
	}

	resp, err := service.ServiceGroupApp.AppServiceGroup.AIConfigService.TestAIConfigByID(uint(id))
	if err != nil {
		global.GVA_LOG.Error("测试AI配置失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithData(resp, c)
}
