package domain

import (
	"errors"

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

func UnProcessableEntityResponse(c *fiber.Ctx) error {
	return c.Status(fiber.StatusUnprocessableEntity).JSON(Response{
		Message:    "failed to parse request body",
		StatusCode: fiber.StatusUnprocessableEntity,
		Success:    false,
	})
}

func UnExpectedErrorResponse(c *fiber.Ctx) error {
	return c.Status(fiber.StatusInternalServerError).JSON(Response{
		Message:    "Unexpected internal error",
		StatusCode: fiber.StatusInternalServerError,
		Success:    false,
	})
}

func FiberErrorResponse(c *fiber.Ctx, err error) error {
	var statusCode int
	var message string

	switch {
	case errors.Is(err, fiber.ErrNotFound):
		statusCode = fiber.StatusNotFound
		message = "resource not found"

	case errors.Is(err, fiber.ErrBadRequest):
		statusCode = fiber.StatusBadRequest
		message = "bad request"

	case errors.Is(err, fiber.ErrUnauthorized):
		statusCode = fiber.StatusUnauthorized
		message = "unauthorized"

	case errors.Is(err, fiber.ErrForbidden):
		statusCode = fiber.StatusForbidden
		message = "forbidden"

	case errors.Is(err, fiber.ErrConflict):
		statusCode = fiber.StatusConflict
		message = "conflict occurred"

	default:
		statusCode = fiber.StatusInternalServerError
		message = "unexpected server error"
	}

	return c.Status(statusCode).JSON(fiber.Map{
		"success":     false,
		"status_code": statusCode,
		"error":       message,
		"details":     err.Error(),
	})
}
