package listing

import (
	"context"

	"hfb_sys/backend/internal/middleware"
	"hfb_sys/backend/pkg/response"

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

func (h *Handler) ListPendingReview(c *gin.Context) {
	items, err := h.service.ListPendingReview(c.Request.Context())
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, gin.H{"items": items})
}

func (h *Handler) ListAdmin(c *gin.Context) {
	query, ok := parseAdminListQuery(c)
	if !ok {
		return
	}
	result, err := h.service.ListAdmin(c.Request.Context(), query)
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, result)
}

func (h *Handler) FindAdmin(c *gin.Context) {
	id, ok := parseID(c)
	if !ok {
		return
	}
	item, err := h.service.FindAdmin(c.Request.Context(), id)
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, item)
}

func (h *Handler) AdminOffline(c *gin.Context) {
	h.adminAction(c, h.service.AdminOffline)
}

func (h *Handler) AdminMarkAbnormal(c *gin.Context) {
	h.adminAction(c, h.service.AdminMarkAbnormal)
}

func (h *Handler) TransferOwner(c *gin.Context) {
	adminID, ok := currentAdminID(c)
	if !ok {
		response.Unauthorized(c, "缺少管理员上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req TransferOwnerRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "转移参数不正确")
		return
	}
	item, err := h.service.TransferOwner(c.Request.Context(), adminID, id, req, auditMeta(c))
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, item)
}

func (h *Handler) adminAction(c *gin.Context, fn func(context.Context, uint64, uint64, AdminActionRequest, AuditMeta) (*ListingDTO, error)) {
	adminID, ok := currentAdminID(c)
	if !ok {
		response.Unauthorized(c, "缺少管理员上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req AdminActionRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "操作原因不能为空")
		return
	}
	item, err := fn(c.Request.Context(), adminID, id, req, auditMeta(c))
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, item)
}

func auditMeta(c *gin.Context) AuditMeta {
	return AuditMeta{
		IP:        c.ClientIP(),
		UserAgent: c.GetHeader("User-Agent"),
		RequestID: middleware.GetRequestID(c),
	}
}

func (h *Handler) Approve(c *gin.Context) {
	id, ok := parseID(c)
	if !ok {
		return
	}
	item, err := h.service.Approve(c.Request.Context(), id)
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, item)
}

func (h *Handler) AdjustReviewPrice(c *gin.Context) {
	adminID, ok := currentAdminID(c)
	if !ok {
		response.Unauthorized(c, "缺少管理员上下文")
		return
	}
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req AdminPriceAdjustRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "调价参数不正确")
		return
	}
	item, err := h.service.AdjustReviewPrice(c.Request.Context(), adminID, id, req, auditMeta(c))
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, item)
}

func (h *Handler) Reject(c *gin.Context) {
	id, ok := parseID(c)
	if !ok {
		return
	}
	var req ReviewRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.BadRequest(c, "审核拒绝原因不能为空")
		return
	}
	item, err := h.service.Reject(c.Request.Context(), id, req)
	if err != nil {
		writeListingError(c, err)
		return
	}
	response.OK(c, item)
}
