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 AiProviderApi struct{}

var aiProviderService = service.ServiceGroupApp.AppServiceGroup.AiProviderService

// CreateAiProvider 创建提供商
// @Tags AiProvider
// @Summary 创建提供商
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiProvider true "提供商信息"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /aiProvider/createAiProvider [post]
func (a *AiProviderApi) CreateAiProvider(c *gin.Context) {
	var provider app.AiProvider
	err := c.ShouldBindJSON(&provider)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	provider.UserID = utils.GetUserID(c)

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

// DeleteAiProvider 删除提供商
// @Tags AiProvider
// @Summary 删除提供商
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "ID"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /aiProvider/deleteAiProvider [delete]
func (a *AiProviderApi) DeleteAiProvider(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 := aiProviderService.DeleteAiProvider(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// UpdateAiProvider 更新提供商
// @Tags AiProvider
// @Summary 更新提供商
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiProvider true "提供商信息"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /aiProvider/updateAiProvider [put]
func (a *AiProviderApi) UpdateAiProvider(c *gin.Context) {
	var provider app.AiProvider
	err := c.ShouldBindJSON(&provider)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)

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

// FindAiProvider 查询提供商
// @Tags AiProvider
// @Summary 查询提供商
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.GetById true "ID"
// @Success 200 {object} response.Response{data=app.AiProvider,msg=string} "查询成功"
// @Router /aiProvider/findAiProvider [get]
func (a *AiProviderApi) FindAiProvider(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 provider, err := aiProviderService.GetAiProvider(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(provider, c)
	}
}

// GetAiProviderList 获取提供商列表
// @Tags AiProvider
// @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 /aiProvider/getAiProviderList [get]
func (a *AiProviderApi) GetAiProviderList(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 := aiProviderService.GetAiProviderList(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)
	}
}
