package order

import (
	"hfb_sys/backend/pkg/response"

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

func (h *Handler) SubmitHandoff(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req SubmitHandoffRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "交接说明不能为空")
		return
	}
	record, err := h.service.SubmitHandoff(c.Request.Context(), userID, id, req)
	if err != nil {
		writeOrderError(c, err)
		return
	}
	response.Created(c, record)
}

func (h *Handler) ConfirmReceive(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	if err := h.service.ConfirmReceive(c.Request.Context(), userID, id); err != nil {
		writeOrderError(c, err)
		return
	}
	response.OK(c, gin.H{"received": true})
}

func (h *Handler) HandoffRecords(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	items, err := h.service.HandoffRecords(c.Request.Context(), userID, id)
	if err != nil {
		writeOrderError(c, err)
		return
	}
	response.OK(c, gin.H{"items": items})
}

func (h *Handler) SubmitReturn(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req SubmitReturnRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "归还说明不能为空")
		return
	}
	record, err := h.service.SubmitReturn(c.Request.Context(), userID, id, req)
	if err != nil {
		writeOrderError(c, err)
		return
	}
	response.Created(c, record)
}

func (h *Handler) ConfirmReturn(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	if err := h.service.ConfirmReturn(c.Request.Context(), userID, id); err != nil {
		writeOrderError(c, err)
		return
	}
	response.OK(c, gin.H{"completed": true})
}

func (h *Handler) SubmitCheckout(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req SubmitCheckoutRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "结账说明不能为空")
		return
	}
	record, err := h.service.SubmitCheckout(c.Request.Context(), userID, id, req)
	if err != nil {
		writeOrderError(c, err)
		return
	}
	response.Created(c, record)
}

func (h *Handler) ConfirmCheckout(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	if err := h.service.ConfirmCheckout(c.Request.Context(), userID, id); err != nil {
		writeOrderError(c, err)
		return
	}
	response.OK(c, gin.H{"completed": true})
}

func (h *Handler) CounterCheckout(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req CounterCheckoutRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "结账修正原因不能为空")
		return
	}
	checkout, err := h.service.CounterCheckout(c.Request.Context(), userID, id, req)
	if err != nil {
		writeOrderError(c, err)
		return
	}
	response.OK(c, checkout)
}

func (h *Handler) AcceptCheckout(c *gin.Context) {
	userID, ok := currentUserID(c)
	if !ok {
		response.Unauthorized(c, "缺少用户上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	if err := h.service.AcceptCheckout(c.Request.Context(), userID, id); err != nil {
		writeOrderError(c, err)
		return
	}
	response.OK(c, gin.H{"completed": true})
}
