package utils

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

	"github.com/google/uuid"
	"github.com/jackc/pgx/v5/pgtype"
)

// resolveMediaURL resolves a relative path (cover or file) to a full URL
func ResolveMediaURL(libraryID pgtype.UUID, relativePath pgtype.Text) string {
	if !relativePath.Valid || relativePath.String == "" {
		return ""
	}

	if strings.HasPrefix(relativePath.String, "/uploads/") {
		return relativePath.String
	}

	if filepath.IsAbs(relativePath.String) {
		return relativePath.String
	}

	libraryIDStr := uuid.UUID(libraryID.Bytes).String()
	return fmt.Sprintf("/uploads/library-%s/%s", libraryIDStr, relativePath.String)
}
