package roomapi

import (
	"net/http"

	"hyapp/pkg/tencentrtc"
	"hyapp/services/gateway-service/internal/client"
	"hyapp/services/gateway-service/internal/transport/http/httproutes"
)

// Handler owns room-facing HTTP endpoints in gateway.
// It keeps gateway as protocol adapter only: room state still belongs to room-service.
type Handler struct {
	roomClient        client.RoomClient
	roomGuardClient   client.RoomGuardClient
	roomQueryClient   client.RoomQueryClient
	userProfileClient client.UserProfileClient
	userCountryClient client.UserCountryQueryClient
	userRegionClient  client.UserRegionClient
	userSocialClient  client.UserSocialClient
	userHostClient    client.UserHostClient
	walletClient      client.WalletClient
	growthLevelClient client.GrowthLevelClient
	achievementClient client.AchievementClient
	rtcTokenConfig    tencentrtc.TokenConfig
}

type Config struct {
	RoomClient        client.RoomClient
	RoomGuardClient   client.RoomGuardClient
	RoomQueryClient   client.RoomQueryClient
	UserProfileClient client.UserProfileClient
	UserCountryClient client.UserCountryQueryClient
	UserRegionClient  client.UserRegionClient
	UserSocialClient  client.UserSocialClient
	UserHostClient    client.UserHostClient
	WalletClient      client.WalletClient
	GrowthLevelClient client.GrowthLevelClient
	AchievementClient client.AchievementClient
	RTCTokenConfig    tencentrtc.TokenConfig
}

func New(config Config) *Handler {
	return &Handler{
		roomClient:        config.RoomClient,
		roomGuardClient:   config.RoomGuardClient,
		roomQueryClient:   config.RoomQueryClient,
		userProfileClient: config.UserProfileClient,
		userCountryClient: config.UserCountryClient,
		userRegionClient:  config.UserRegionClient,
		userSocialClient:  config.UserSocialClient,
		userHostClient:    config.UserHostClient,
		walletClient:      config.WalletClient,
		growthLevelClient: config.GrowthLevelClient,
		achievementClient: config.AchievementClient,
		rtcTokenConfig:    config.RTCTokenConfig,
	}
}

func (h *Handler) RoomHandlers() httproutes.RoomHandlers {
	return httproutes.RoomHandlers{
		GetMyRoom:            h.getMyRoom,
		ListRoomFeeds:        h.listRoomFeeds,
		ListRooms:            h.listRooms,
		GetCurrentRoom:       h.getCurrentRoom,
		GetRoomSnapshot:      h.getRoomSnapshot,
		GetRoomDetail:        h.getRoomDetail,
		ListRoomOnlineUsers:  h.listRoomOnlineUsers,
		ListRoomBannedUsers:  h.listRoomBannedUsers,
		GetRoomGiftPanel:     h.getRoomGiftPanel,
		GetRoomRocket:        h.getRoomRocket,
		FollowRoom:           h.handleRoomFollow,
		CreateRoom:           h.createRoom,
		UpdateRoomProfile:    h.updateRoomProfile,
		SaveRoomBackground:   h.saveRoomBackground,
		ListRoomBackgrounds:  h.listRoomBackgrounds,
		SetRoomBackground:    h.setRoomBackground,
		JoinRoom:             h.joinRoom,
		RoomHeartbeat:        h.roomHeartbeat,
		LeaveRoom:            h.leaveRoom,
		CloseRoom:            h.closeRoom,
		MicUp:                h.micUp,
		MicDown:              h.micDown,
		ChangeMicSeat:        h.changeMicSeat,
		ConfirmMicPublishing: h.confirmMicPublishing,
		MicHeartbeat:         h.micHeartbeat,
		SetMicMute:           h.setMicMute,
		SetMicSeatLock:       h.setMicSeatLock,
		SetChatEnabled:       h.setChatEnabled,
		SetRoomPassword:      h.setRoomPassword,
		SetRoomAdmin:         h.setRoomAdmin,
		MuteUser:             h.muteUser,
		KickUser:             h.kickUser,
		UnbanUser:            h.unbanUser,
		SendGift:             h.sendGift,
	}
}

func (h *Handler) IssueTencentRTCToken(writer http.ResponseWriter, request *http.Request) {
	h.issueTencentRTCToken(writer, request)
}

func (h *Handler) BatchRoomDisplayProfiles(writer http.ResponseWriter, request *http.Request) {
	h.batchRoomDisplayProfiles(writer, request)
}
