package agent

import (
	"net/http"

	"qnc-server/app/main/api/internal/logic/agent"
	"qnc-server/app/main/api/internal/svc"
	"qnc-server/app/main/api/internal/types"
	"qnc-server/common/result"
	"qnc-server/pkg/lzkit/validator"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func GetAgentPromotionQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.GetAgentPromotionQrcodeReq
		if err := httpx.Parse(r, &req); err != nil {
			result.ParamErrorResult(r, w, err)
			return
		}
		if err := validator.Validate(req); err != nil {
			result.ParamValidateErrorResult(r, w, err)
			return
		}

		// 注意：这里传入了 ResponseWriter，用于直接写入图片数据
		l := agent.NewGetAgentPromotionQrcodeLogic(r.Context(), svcCtx, w)
		err := l.GetAgentPromotionQrcode(&req)
		if err != nil {
			// 如果处理过程中出错，返回JSON错误响应
			result.HttpResult(r, w, nil, err)
		}
		// 成功时，图片数据已经通过logic直接写入ResponseWriter，不需要额外处理
	}
}
