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/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type AiApiKeyApi struct{}

// CreateAiApiKey 创建API密钥
// @Tags AiApiKey
// @Summary 创建API密钥
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiApiKey true "API密钥信息"
// @Success 200 {object} response.Response{data=app.AiApiKey,msg=string} "创建成功"
// @Router /aiApiKey/createAiApiKey [post]
func (a *AiApiKeyApi) CreateAiApiKey(c *gin.Context) {
	var apiKey app.AiApiKey
	err := c.ShouldBindJSON(&apiKey)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	apiKey.UserID = utils.GetUserID(c)

	if err := aiApiKeyService.CreateAiApiKey(&apiKey); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithData(apiKey, c)
	}
}

// DeleteAiApiKey 删除API密钥
// @Tags AiApiKey
// @Summary 删除API密钥
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "ID"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /aiApiKey/deleteAiApiKey [delete]
func (a *AiApiKeyApi) DeleteAiApiKey(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 := aiApiKeyService.DeleteAiApiKey(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// UpdateAiApiKey 更新API密钥
// @Tags AiApiKey
// @Summary 更新API密钥
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiApiKey true "API密钥信息"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /aiApiKey/updateAiApiKey [put]
func (a *AiApiKeyApi) UpdateAiApiKey(c *gin.Context) {
	var apiKey app.AiApiKey
	err := c.ShouldBindJSON(&apiKey)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)

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

// FindAiApiKey 查询API密钥
// @Tags AiApiKey
// @Summary 查询API密钥
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.GetById true "ID"
// @Success 200 {object} response.Response{data=app.AiApiKey,msg=string} "查询成功"
// @Router /aiApiKey/findAiApiKey [get]
func (a *AiApiKeyApi) FindAiApiKey(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 apiKey, err := aiApiKeyService.GetAiApiKey(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(apiKey, c)
	}
}

// GetAiApiKeyList 获取API密钥列表
// @Tags AiApiKey
// @Summary 获取API密钥列表
// @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 /aiApiKey/getAiApiKeyList [get]
func (a *AiApiKeyApi) GetAiApiKeyList(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 := aiApiKeyService.GetAiApiKeyList(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)
	}
}
