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

var aiModelService = service.ServiceGroupApp.AppServiceGroup.AiModelService

// CreateAiModel 创建模型
// @Tags AiModel
// @Summary 创建模型
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiModel true "模型信息"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /aiModel/createAiModel [post]
func (a *AiModelApi) CreateAiModel(c *gin.Context) {
	var model app.AiModel
	err := c.ShouldBindJSON(&model)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	model.UserID = utils.GetUserID(c)

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

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

// UpdateAiModel 更新模型
// @Tags AiModel
// @Summary 更新模型
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body app.AiModel true "模型信息"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /aiModel/updateAiModel [put]
func (a *AiModelApi) UpdateAiModel(c *gin.Context) {
	var model app.AiModel
	err := c.ShouldBindJSON(&model)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	userID := utils.GetUserID(c)

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

// FindAiModel 查询模型
// @Tags AiModel
// @Summary 查询模型
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.GetById true "ID"
// @Success 200 {object} response.Response{data=app.AiModel,msg=string} "查询成功"
// @Router /aiModel/findAiModel [get]
func (a *AiModelApi) FindAiModel(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 model, err := aiModelService.GetAiModel(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(model, c)
	}
}

// GetAiModelList 获取模型列表
// @Tags AiModel
// @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 /aiModel/getAiModelList [get]
func (a *AiModelApi) GetAiModelList(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 := aiModelService.GetAiModelList(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)
	}
}

// SyncProviderModels 同步提供商模型
// @Tags AiModel
// @Summary 同步提供商模型
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "提供商ID"
// @Success 200 {object} response.Response{msg=string} "同步成功"
// @Router /aiModel/syncProviderModels [post]
func (a *AiModelApi) SyncProviderModels(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 := aiModelService.SyncProviderModels(reqId.Uint(), userID); err != nil {
		global.GVA_LOG.Error("同步失败!", zap.Error(err))
		response.FailWithMessage("同步失败: "+err.Error(), c)
	} else {
		response.OkWithMessage("同步成功", c)
	}
}
