package cms

import (
	"errors"
	"strings"

	"tm/pkg/response"

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

func handleParseError(c echo.Context, err error) error {
	if err == nil {
		return nil
	}
	if strings.Contains(err.Error(), "invalid request format") {
		return response.BadRequest(c, "Invalid request format", err.Error())
	}
	return response.ValidationError(c, "Please fix the validation errors", FormatValidationErrors(err))
}

func handleServiceError(c echo.Context, err error, fallback string) error {
	if err == nil {
		return nil
	}
	if errors.Is(err, ErrCMSKeyExists) {
		return response.Conflict(c, "A marketing page with this key already exists")
	}
	if errors.Is(err, ErrCMSNotFound) {
		return response.NotFound(c, "Marketing page not found")
	}
	return response.InternalServerError(c, fallback)
}
