package handler

import (
	"net/http/httputil"
	"net/url"
	"path/filepath"

	"u-desk/internal/agent/config"
	"u-desk/internal/filesystem"

	"github.com/labstack/echo/v4"
)

type Handler struct {
	fsSvc     *filesystem.FileSystemService
	cfg       *config.Config
	fileProxy *httputil.ReverseProxy
}

func New(fsSvc *filesystem.FileSystemService, cfg *config.Config) *Handler {
	fileTarget, _ := url.Parse(cfg.FileServerAddr() + "/localfs/")
	return &Handler{
		fsSvc:     fsSvc,
		cfg:       cfg,
		fileProxy: httputil.NewSingleHostReverseProxy(fileTarget),
	}
}

// getPath 从 query 参数提取并规范化文件路径
func getPath(c echo.Context) string {
	raw := c.QueryParam("path")
	if raw == "" {
		return ""
	}
	// URL 已被 Echo 自动 decode，只需转换路径分隔符
	return filepath.FromSlash(raw)
}
