package utils

import (
	"errors"
	"os"

	"github.com/gofiber/fiber/v2"
)

func ParamsParserHelper(c *fiber.Ctx, params interface{}) error {
	if err := c.ParamsParser(params); err != nil {
		return errors.New("Failed to parse params")
	}

	if errValidation := ValidateStruct(params); errValidation != nil {
		return errors.New("Failed to validate params")
	}

	return nil
}

func BodyParserHelper(c *fiber.Ctx, body interface{}) error {
	if err := c.BodyParser(body); err != nil {

		return errors.New("Failed to parse body")
	}

	if errValidation := ValidateStruct(body); errValidation != nil {
		return errors.New("Failed to validate body")
	}

	return nil
}

func QueryParserHelper(c *fiber.Ctx, query interface{}) error {
	if err := c.QueryParser(query); err != nil {
		return errors.New("Failed to parse query")
	}

	if errValidation := ValidateStruct(query); errValidation != nil {
		return errors.New("Failed to validate query")
	}

	return nil
}

func CreateDirectoryIfNotExists(path string) {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		os.Mkdir(path, 0755)
	}
}
