package order

import (
	"errors"
	"net/http"

	"hfb_sys/backend/pkg/response"

	"github.com/gin-gonic/gin"
)

func writeOrderError(c *gin.Context, err error) {
	switch {
	case errors.Is(err, ErrDependencyUnavailable):
		response.ServiceUnavailable(c, "数据库未连接")
	case errors.Is(err, ErrInvalidRentHours):
		response.BadRequest(c, "订单信息不符合规则")
	case errors.Is(err, ErrListingUnavailable):
		response.Error(c, http.StatusConflict, "listing_unavailable", "该账号暂不可租")
	case errors.Is(err, ErrCannotRentOwnListing):
		response.BadRequest(c, "不能租用自己发布的账号")
	case errors.Is(err, ErrInsufficientBalance):
		response.Error(c, http.StatusConflict, "insufficient_balance", "钱包余额不足，请先充值")
	case errors.Is(err, ErrChannelPaymentRequired):
		response.Error(c, http.StatusGone, "channel_payment_required", "请使用第三方支付入口完成订单付款")
	case errors.Is(err, ErrOrderCannotPay):
		response.Error(c, http.StatusConflict, "order_cannot_pay", "当前订单不能支付")
	case errors.Is(err, ErrOrderCannotCancel):
		response.Error(c, http.StatusConflict, "order_cannot_cancel", "当前订单不能取消")
	case errors.Is(err, ErrOrderCannotHandoff):
		response.Error(c, http.StatusConflict, "order_cannot_handoff", "当前订单不能交接")
	case errors.Is(err, ErrOrderCannotResetHandoff):
		response.Error(c, http.StatusConflict, "order_cannot_reset_handoff", "仅超时卡住的订单可重置到对应待办阶段")
	case errors.Is(err, ErrOrderCannotReceive):
		response.Error(c, http.StatusConflict, "order_cannot_receive", "当前订单不能确认收号")
	case errors.Is(err, ErrOrderCannotReturn):
		response.Error(c, http.StatusConflict, "order_cannot_return", "当前订单不能归还")
	case errors.Is(err, ErrOrderCannotComplete):
		response.Error(c, http.StatusConflict, "order_cannot_complete", "当前订单不能完成")
	case errors.Is(err, ErrCheckoutCannotSubmit):
		response.Error(c, http.StatusConflict, "checkout_cannot_submit", "当前订单不能发起结账")
	case errors.Is(err, ErrCheckoutCannotConfirm):
		response.Error(c, http.StatusConflict, "checkout_cannot_confirm", "当前结账不能确认")
	case errors.Is(err, ErrCheckoutCannotCounter):
		response.Error(c, http.StatusConflict, "checkout_cannot_counter", "当前结账不能修改")
	case errors.Is(err, ErrInvalidCheckoutAmount):
		response.BadRequest(c, "结账金额不符合规则")
	case errors.Is(err, ErrPermissionDenied):
		response.Error(c, http.StatusForbidden, "permission_denied", "无权操作该订单")
	case IsNotFound(err):
		response.Error(c, http.StatusNotFound, "not_found", "订单不存在")
	default:
		response.Error(c, http.StatusInternalServerError, "internal_error", "订单服务暂时不可用")
	}
}
