package vision

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

var allowedImageExt = map[string]struct{}{
	".png": {}, ".jpg": {}, ".jpeg": {}, ".webp": {}, ".gif": {},
	".bmp": {}, ".tif": {}, ".tiff": {},
}

// ResolveImagePath 解析并校验可读图片路径（支持任意目录；仍校验扩展名与常规文件）。
func ResolveImagePath(path string, cwd string) (string, error) {
	p := strings.TrimSpace(path)
	if p == "" {
		return "", fmt.Errorf("path is empty")
	}
	cwdTrim := strings.TrimSpace(cwd)
	if cwdTrim == "" {
		var err error
		cwdTrim, err = os.Getwd()
		if err != nil {
			return "", fmt.Errorf("getwd: %w", err)
		}
	}
	cwdAbs, err := filepath.Abs(filepath.Clean(cwdTrim))
	if err != nil {
		return "", err
	}

	var candidate string
	if filepath.IsAbs(p) {
		candidate = filepath.Clean(p)
	} else {
		candidate = filepath.Clean(filepath.Join(cwdAbs, p))
	}
	resolved := normalizeAbsPath(candidate)
	if resolved == "" {
		return "", fmt.Errorf("invalid path")
	}

	ext := strings.ToLower(filepath.Ext(resolved))
	if _, ok := allowedImageExt[ext]; !ok {
		return "", fmt.Errorf("unsupported image extension %q", ext)
	}

	st, err := os.Stat(resolved)
	if err != nil {
		return "", fmt.Errorf("stat: %w", err)
	}
	if st.IsDir() {
		return "", fmt.Errorf("not a regular file")
	}
	if st.Size() > 0 && st.Size() > 1<<30 {
		return "", fmt.Errorf("file too large on disk")
	}
	return resolved, nil
}

func normalizeAbsPath(p string) string {
	abs, err := filepath.Abs(filepath.Clean(p))
	if err != nil {
		return ""
	}
	if link, err := filepath.EvalSymlinks(abs); err == nil {
		return link
	}
	return abs
}
