package api

import (
	"github.com/gin-gonic/gin"

	"oci-portal/internal/service"
)

// registerAiGateway 挂载 /ai/v1/* 对外网关端点(独立密钥鉴权,不挂 JWT 与系统日志)。
// 挂在全局中间件之后,仍受 IP 限速与 Recovery 保护;高频调用自有 AiCallLog。
func registerAiGateway(r *gin.Engine, aiGateway *service.AiGatewayService) {
	aih := &aiGatewayHandler{gw: aiGateway}
	ai := r.Group("/ai/v1", bodyLimit(10<<20), aih.auth)
	ai.POST("/chat/completions", aih.chatCompletions)
	ai.POST("/responses", aih.responses)
	ai.POST("/messages", aih.messages)
	ai.POST("/embeddings", aih.embeddings)
	ai.POST("/audio/speech", aih.audioSpeech)
	ai.POST("/tts", aih.tts)
	ai.POST("/rerank", aih.rerank)
	ai.POST("/moderations", aih.moderations)
	ai.GET("/models", aih.listModels)
}

// registerAiAdmin 挂载面板管理侧的 AI 密钥/渠道/模型/日志(JWT 组内)。
func registerAiAdmin(secured *gin.RouterGroup, aiGateway *service.AiGatewayService) {
	aiadmin := &aiAdminHandler{gw: aiGateway}
	secured.GET("/ai-keys", aiadmin.listKeys)
	secured.POST("/ai-keys", aiadmin.createKey)
	secured.PUT("/ai-keys/:id", aiadmin.updateKey)
	secured.PUT("/ai-keys/:id/content-log", aiadmin.updateKeyContentLog)
	secured.DELETE("/ai-keys/:id", aiadmin.deleteKey)
	secured.GET("/ai-channels", aiadmin.listChannels)
	secured.POST("/ai-channels", aiadmin.createChannel)
	secured.PUT("/ai-channels/:id", aiadmin.updateChannel)
	secured.DELETE("/ai-channels/:id", aiadmin.deleteChannel)
	secured.POST("/ai-channels/:id/probe", aiadmin.probeChannel)
	secured.POST("/ai-channels/:id/sync-models", aiadmin.syncChannelModels)
	secured.GET("/ai-channels/:id/models", aiadmin.listChannelModels)
	secured.POST("/ai-channels/:id/test-model", aiadmin.testChannelModel)
	secured.GET("/ai-models", aiadmin.gatewayModels)
	secured.GET("/ai-model-catalog", aiadmin.modelCatalog)
	secured.GET("/ai-settings", aiadmin.aiSettings)
	secured.PUT("/ai-settings", aiadmin.updateAiSettings)
	secured.GET("/ai-blacklist", aiadmin.listBlacklist)
	secured.POST("/ai-blacklist", aiadmin.addBlacklist)
	secured.DELETE("/ai-blacklist/:id", aiadmin.removeBlacklist)
	secured.GET("/ai-logs", aiadmin.listLogs)
	secured.GET("/ai-content-logs", aiadmin.listContentLogs)
}
