package app

import (
	"net/http"
	"strings"

	"bindbox-game/configs"
	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/jwtoken"
)

type addressShareSubmitRequest struct {
	ShareToken string `json:"share_token"`
	Name       string `json:"name"`
	Mobile     string `json:"mobile"`
	Province   string `json:"province"`
	City       string `json:"city"`
	District   string `json:"district"`
	Address    string `json:"address"`
}

type addressShareSubmitResponse struct {
	AddressID int64 `json:"address_id"`
}

// SubmitAddressShare 提交共享地址（公域）
// @Summary 提交共享地址
// @Description 被邀请者使用分享令牌提交收件信息；服务端校验令牌有效并创建待发货记录（极简方案）
// @Tags APP端.用户
// @Accept json
// @Produce json
// @Param RequestBody body addressShareSubmitRequest true "请求参数：令牌与地址信息"
// @Success 200 {object} addressShareSubmitResponse
// @Failure 400 {object} code.Failure "令牌无效/已处理/资产不可用"
// @Router /api/app/address-share/submit [post]
func (h *handler) SubmitAddressShare() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(addressShareSubmitRequest)
		rsp := new(addressShareSubmitResponse)
		if err := ctx.ShouldBindJSON(req); err != nil || req.ShareToken == "" || req.Name == "" || req.Mobile == "" || req.Province == "" || req.City == "" || req.District == "" || req.Address == "" {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "参数错误"))
			return
		}

		// 尝试获取登录用户信息 (可选)
		var submitUserID *int64
		authHeader := ctx.GetHeader("Authorization")
		if authHeader != "" {
			// 如果有 Authorization 尝试解析
			if claims, err := jwtoken.New(configs.Get().JWT.PatientSecret).Parse(authHeader); err == nil {
				uid := int64(claims.SessionUserInfo.Id)
				submitUserID = &uid
			}
		}

		ip := ctx.Request().RemoteAddr
		// 统一使用 ctx.RequestContext() 包含 context 内容
		addrID, err := h.user.SubmitAddressShare(ctx.RequestContext(), req.ShareToken, req.Name, req.Mobile, req.Province, req.City, req.District, req.Address, submitUserID, &ip)
		if err != nil {
			// 处理业务错误，映射到具体代码
			msg := err.Error()
			errorCode := 10024
			if strings.Contains(msg, "invalid_or_expired_token") {
				errorCode = 10025
			} else if strings.Contains(msg, "already_processed") {
				errorCode = 10026
			}
			ctx.AbortWithError(core.Error(http.StatusBadRequest, errorCode, msg))
			return
		}
		rsp.AddressID = addrID
		ctx.Payload(rsp)
	}
}
