package app

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

type AiPresetBindingApi struct{}

var aiPresetBindingService = service.ServiceGroupApp.AppServiceGroup.AiPresetBindingService

// CreateAiPresetBinding 创建绑定
// @Tags AiPresetBinding
// @Summary 创建绑定
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiPresetBinding true "绑定信息"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /aiPresetBinding/createAiPresetBinding [post]
func (a *AiPresetBindingApi) CreateAiPresetBinding(c *gin.Context) {
	var binding app.AiPresetBinding
	err := c.ShouldBindJSON(&binding)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	binding.UserID = utils.GetUserID(c)

	if err := aiPresetBindingService.CreateAiPresetBinding(&binding); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteAiPresetBinding 删除绑定
// @Tags AiPresetBinding
// @Summary 删除绑定
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "ID"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /aiPresetBinding/deleteAiPresetBinding [delete]
func (a *AiPresetBindingApi) DeleteAiPresetBinding(c *gin.Context) {
	var reqId request.GetById
	err := c.ShouldBindJSON(&reqId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)

	if err := aiPresetBindingService.DeleteAiPresetBinding(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// UpdateAiPresetBinding 更新绑定
// @Tags AiPresetBinding
// @Summary 更新绑定
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiPresetBinding true "绑定信息"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /aiPresetBinding/updateAiPresetBinding [put]
func (a *AiPresetBindingApi) UpdateAiPresetBinding(c *gin.Context) {
	var binding app.AiPresetBinding
	err := c.ShouldBindJSON(&binding)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)

	if err := aiPresetBindingService.UpdateAiPresetBinding(&binding, userID); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindAiPresetBinding 查询绑定
// @Tags AiPresetBinding
// @Summary 查询绑定
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.GetById true "ID"
// @Success 200 {object} response.Response{data=app.AiPresetBinding,msg=string} "查询成功"
// @Router /aiPresetBinding/findAiPresetBinding [get]
func (a *AiPresetBindingApi) FindAiPresetBinding(c *gin.Context) {
	var reqId request.GetById
	err := c.ShouldBindQuery(&reqId)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)

	if binding, err := aiPresetBindingService.GetAiPresetBinding(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(binding, c)
	}
}

// GetAiPresetBindingList 获取绑定列表
// @Tags AiPresetBinding
// @Summary 获取绑定列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.PageInfo true "分页参数"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /aiPresetBinding/getAiPresetBindingList [get]
func (a *AiPresetBindingApi) GetAiPresetBindingList(c *gin.Context) {
	var pageInfo request.PageInfo
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)

	if list, total, err := aiPresetBindingService.GetAiPresetBindingList(pageInfo, userID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
