package kanban

import (
	"errors"
	"strings"

	"tm/pkg/response"

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

func handleServiceError(c echo.Context, err error, fallback string) error {
	if err == nil {
		return nil
	}

	if errors.Is(err, ErrAccessDenied) {
		return response.NotFound(c, "Resource not found")
	}

	if errors.Is(err, ErrWorkflowUnavailable) {
		return response.BadRequest(c, "Workflow rules are temporarily unavailable", err.Error())
	}

	msg := err.Error()
	if strings.Contains(msg, "not allowed") ||
		strings.Contains(msg, "cannot be changed") ||
		strings.Contains(msg, "cannot be deleted") ||
		strings.Contains(msg, "cannot be reordered") ||
		strings.Contains(msg, "fixed") ||
		strings.Contains(msg, "only column color") {
		return response.BadRequest(c, msg, "")
	}

	if strings.Contains(msg, "not found") {
		return response.NotFound(c, "Resource not found")
	}

	return response.InternalServerError(c, fallback)
}
