package app

import (
	"net/http"

	"bindbox-game/configs"
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/miniprogram"
	"bindbox-game/internal/pkg/validation"
	"bindbox-game/internal/pkg/wechat"

	"go.uber.org/zap"
)

type bindPhoneRequest struct {
	Code string `json:"code"`
}

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

// BindPhone 绑定手机号
// @Summary 绑定手机号
// @Description 使用微信手机号 code 换取手机号并绑定到指定用户
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param user_id path integer true "用户ID"
// @Security LoginVerifyToken
// @Param RequestBody body bindPhoneRequest true "请求参数"
// @Success 200 {object} bindPhoneResponse
// @Failure 400 {object} code.Failure
// @Router /api/app/users/{user_id}/phone/bind [post]
func (h *handler) BindPhone() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(bindPhoneRequest)
		rsp := new(bindPhoneResponse)
		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
		}

		cfg := configs.Get()
		var tokenRes struct {
			AccessToken string `json:"access_token"`
		}
		if err := miniprogram.GetAccessToken(cfg.Wechat.AppID, cfg.Wechat.AppSecret, &tokenRes); err != nil || tokenRes.AccessToken == "" {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "获取微信access_token失败"))
			return
		}
		pn, err := wechat.GetPhoneNumber(ctx, tokenRes.AccessToken, req.Code)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
			return
		}
		mobile := pn.PhoneInfo.PurePhoneNumber
		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 {
				h.logger.Warn("手机号绑定冲突", zap.Int64("user_id", userID), zap.Int64("existed_user_id", existedUser.ID), zap.String("mobile", mobile))
				ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "该手机号已被其他账号占用"))
				return
			}
			// 如果是当前用户自己，直接返回成功
			rsp.Success = true
			rsp.Mobile = mobile
			ctx.Payload(rsp)
			return
		}

		h.logger.Info("开始绑定手机号", zap.Int64("user_id", userID), zap.String("mobile", mobile))
		if _, err := h.writeDB.Users.WithContext(ctx.RequestContext()).Where(h.writeDB.Users.ID.Eq(userID)).Updates(map[string]any{"mobile": mobile}); err != nil {
			h.logger.Error("绑定手机号数据库更新失败", zap.Error(err), zap.Int64("user_id", userID), zap.String("mobile", mobile))
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ServerError, err.Error()))
			return
		}
		rsp.Success = true
		rsp.Mobile = mobile
		ctx.Payload(rsp)
	}
}
