// Package meeting 会议模块（Phase 2e-2）
package meeting

import (
	"github.com/echochat/backend/app/meeting/controller"
	"github.com/gin-gonic/gin"
)

// RegisterRoutes 注册 meeting 模块的全部前台路由
// 所有接口统一挂载在 /api/v1/meeting/* 前缀下，均需 JWT 认证
// 设计文档 §6.2 的 12 个接口 Task 4 骨架阶段仅搭路由层，业务实现留待 Task 5/6/7
// Phase B 新增录制三接口（POST/DELETE/GET /rooms/:code/recordings*）
func RegisterRoutes(
	r *gin.Engine,
	ctrl *controller.MeetingController,
	recordingCtrl *controller.MeetingRecordingController,
	jwtAuth gin.HandlerFunc,
) {
	authed := r.Group("/api/v1/meeting")
	authed.Use(jwtAuth)
	{
		// 会议房间生命周期
		authed.POST("/rooms", ctrl.CreateRoom)
		authed.GET("/rooms/mine", ctrl.ListMyMeetings)
		authed.GET("/rooms/:code", ctrl.GetRoom)
		authed.POST("/rooms/:code/join", ctrl.JoinRoom)
		authed.POST("/rooms/:code/leave", ctrl.LeaveRoom)
		authed.POST("/rooms/:code/end", ctrl.EndRoom)

		authed.POST("/rooms/:code/transfer-host", ctrl.TransferHost)
		authed.POST("/rooms/:code/kick", ctrl.KickMember)

		authed.POST("/rooms/:code/invite", ctrl.InviteUsers)
		authed.POST("/invite-tokens/:token/redeem", ctrl.RedeemInvite)

		authed.POST("/rooms/:code/chats", ctrl.SendChat)
		authed.GET("/rooms/:code/chats", ctrl.ListChats)

		// 会议录制（Phase B 引入）
		authed.POST("/rooms/:code/recordings", recordingCtrl.StartRecording)
		authed.DELETE("/rooms/:code/recordings/:id", recordingCtrl.StopRecording)
		authed.GET("/rooms/:code/recordings", recordingCtrl.ListRecordings)
	}

	// 内部 webhook（Phase B watchdog）：media-server 异常退出回调
	// 鉴权由 controller 内部用 X-Internal-Secret header + INTERNAL_WEBHOOK_SECRET 校验
	// 不走 JWT，因为调用方是 media-server 而非用户
	r.POST("/internal/meeting/recordings/failure", recordingCtrl.MediaServerFailureWebhook)

	registerCloudLawRoutes := func(group *gin.RouterGroup) {
		group.POST("/meetings", ctrl.CloudLawCreateMeeting)
		group.GET("/meetings/:code", ctrl.CloudLawGetMeeting)
		group.POST("/auth/session", ctrl.CloudLawSession)
		group.POST("/meetings/:code/join", ctrl.CloudLawJoinMeeting)
		group.POST("/meetings/:code/end", ctrl.CloudLawEndMeeting)
	}
	registerCloudLawRoutes(r.Group("/api/v1/cloud-law"))
}
