package app

import (
	"net/http"

	"bindbox-game/configs"
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/douyin"
	"bindbox-game/internal/pkg/validation"
	"bindbox-game/internal/service/sysconfig"
)

type bindDouyinPhoneRequest struct {
	Code string `json:"code"`
	// EncryptedData string `json:"encrypted_data"` // Reserved if needed
	// IV            string `json:"iv"`             // Reserved if needed
}

type bindDouyinPhoneResponse struct {
	Success bool   `json:"success"`
	Mobile  string `json:"mobile"`
}

// DouyinBindPhone 抖音绑定手机号
// @Summary 抖音绑定手机号
// @Description 使用抖音手机号 code 换取手机号并绑定到指定用户
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param user_id path integer true "用户ID"
// @Security LoginVerifyToken
// @Param RequestBody body bindDouyinPhoneRequest true "请求参数"
// @Success 200 {object} bindDouyinPhoneResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/{user_id}/douyin/phone/bind [post]
func (h *handler) DouyinBindPhone() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(bindDouyinPhoneRequest)
		rsp := new(bindDouyinPhoneResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}
		userID := int64(ctx.SessionUserInfo().Id)
		if userID <= 0 || req.Code == "" {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "缺少必要参数"))
			return
		}

		// 获取 Access Token
		accessToken, err := douyin.GetAccessToken(ctx.RequestContext())
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, "获取Access Token失败: "+err.Error()))
			return
		}

		// 获取 AppID
		dynamicCfg := sysconfig.GetGlobalDynamicConfig()
		douyinCfg := dynamicCfg.GetDouyin(ctx.RequestContext())
		appID := douyinCfg.AppID
		if appID == "" {
			// Fallback to static config if dynamic not available or empty (though GetAccessToken checked it)
			appID = configs.Get().Douyin.AppID
		}

		// 获取手机号
		mobile, err := douyin.GetPhoneNumber(ctx.RequestContext(), accessToken, appID, req.Code)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "获取手机号失败: "+err.Error()))
			return
		}

		if mobile == "" {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "获取到的手机号为空"))
			return
		}

		// 检查该手机号是否已被其他账号占用
		existedUser, _ := h.readDB.Users.WithContext(ctx.RequestContext()).Where(h.readDB.Users.Mobile.Eq(mobile)).First()
		if existedUser != nil {
			if existedUser.ID != userID {
				ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "该手机号已被其他账号占用"))
				return
			}
			// 如果是当前用户自己，直接返回成功
			rsp.Success = true
			rsp.Mobile = mobile
			ctx.Payload(rsp)
			return
		}

		// 检查当前用户是否已有手机号
		currentUser, _ := h.readDB.Users.WithContext(ctx.RequestContext()).Where(h.readDB.Users.ID.Eq(userID)).First()
		if currentUser != nil && currentUser.Mobile != "" {
			if currentUser.Mobile == mobile {
				rsp.Success = true
				rsp.Mobile = mobile
				ctx.Payload(rsp)
				return
			}
			// 如果已有手机号且不一致，允许覆盖更新（或者可以根据需求改为提示已绑定过）
		}

		// 更新
		if _, err := h.writeDB.Users.WithContext(ctx.RequestContext()).Where(h.writeDB.Users.ID.Eq(userID)).Updates(map[string]any{"mobile": mobile}); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error()))
			return
		}

		rsp.Success = true
		rsp.Mobile = mobile
		ctx.Payload(rsp)
	}
}
