package handlers

import (
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func getOptionalFormValue(c *gin.Context, key string) (string, bool) {
	value, exists := c.GetPostForm(key)
	if !exists {
		return "", false
	}
	value = strings.TrimSpace(value)
	return value, true
}

func parseOptionalBool(c *gin.Context, key string) (*bool, error) {
	value, exists := c.GetPostForm(key)
	if !exists {
		return nil, nil
	}
	value = strings.TrimSpace(value)
	if value == "" {
		return nil, nil
	}
	parsed, err := strconv.ParseBool(value)
	if err != nil {
		return nil, err
	}
	return &parsed, nil
}

func parseUUIDPtr(value string) (*uuid.UUID, error) {
	value = strings.TrimSpace(value)
	if value == "" {
		return nil, nil
	}
	parsed, err := uuid.Parse(value)
	if err != nil {
		return nil, err
	}
	return &parsed, nil
}
