package agent

import (
	"net/http"

	"jnc-server/app/main/api/internal/logic/agent"
	"jnc-server/app/main/api/internal/svc"

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

func ShortLinkRedirectHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		// 从URL.Path解析shortCode
		// 路径格式：/s/{shortCode}
		path := r.URL.Path
		var shortCode string
		if len(path) >= 3 && path[:3] == "/s/" {
			shortCode = path[3:]
		}
		
		l := agent.NewShortLinkRedirectLogic(r.Context(), svcCtx)
		err := l.ShortLinkRedirect(shortCode, r, w)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		}
	}
}

