package handlers

import (
	"errors"

	"github.com/gofiber/fiber/v2"
	"gorm.io/gorm"

	"wucher/internal/shared/pkg/apperrors"
	"wucher/internal/transport/http/jsonapi"
	"wucher/internal/transport/http/response"
)

func writeDeleteError(c *fiber.Ctx, err error) error {
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return response.WriteErrors(c, fiber.StatusNotFound, []jsonapi.ErrorObject{{
			Status: "404",
			Title:  "Not found",
			Detail: "resource not found",
		}})
	}
	if conflict, ok := apperrors.AsDeleteConflict(err); ok {
		return response.WriteErrors(c, fiber.StatusConflict, []jsonapi.ErrorObject{{
			Status: "409",
			Title:  "Conflict",
			Detail: conflict.Error(),
		}})
	}
	return response.WriteErrors(c, fiber.StatusBadRequest, []jsonapi.ErrorObject{{
		Status: "400",
		Title:  "Delete failed",
		Detail: safeInternalDetail(),
	}})
}
