package app

import (
	"strconv"

	"github.com/break/junhong_cmp_fiber/internal/middleware"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	clientorder "github.com/break/junhong_cmp_fiber/internal/service/client_order"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
	"github.com/gofiber/fiber/v2"
	"go.uber.org/zap"
)

// ClientOrderHandler C 端订单处理器
// 提供 D1~D4 下单、列表、详情、支付接口。
type ClientOrderHandler struct {
	clientOrderService *clientorder.Service
	logger             *zap.Logger
}

// NewClientOrderHandler 创建 C 端订单处理器。
func NewClientOrderHandler(
	clientOrderService *clientorder.Service,
	logger *zap.Logger,
) *ClientOrderHandler {
	return &ClientOrderHandler{
		clientOrderService: clientOrderService,
		logger:             logger,
	}
}

// CreateOrder D1 创建订单。
// POST /api/c/v1/orders/create
func (h *ClientOrderHandler) CreateOrder(c *fiber.Ctx) error {
	var req dto.ClientCreateOrderRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

	customerID, ok := middleware.GetCustomerID(c)
	if !ok || customerID == 0 {
		return errors.New(errors.CodeUnauthorized)
	}

	resp, err := h.clientOrderService.CreateOrder(c.UserContext(), customerID, &req)
	if err != nil {
		return err
	}

	return response.Success(c, resp)
}

// ListOrders D2 订单列表。
// GET /api/c/v1/orders
func (h *ClientOrderHandler) ListOrders(c *fiber.Ctx) error {
	var req dto.ClientOrderListRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

	if req.Page < 1 {
		req.Page = 1
	}
	if req.PageSize < 1 {
		req.PageSize = constants.DefaultPageSize
	}
	if req.PageSize > constants.MaxPageSize {
		req.PageSize = constants.MaxPageSize
	}

	customerID, ok := middleware.GetCustomerID(c)
	if !ok || customerID == 0 {
		return errors.New(errors.CodeUnauthorized)
	}

	list, total, err := h.clientOrderService.ListOrders(c.UserContext(), customerID, &req)
	if err != nil {
		return err
	}

	return response.SuccessWithPagination(c, list, total, req.Page, req.PageSize)
}

// PayOrder D4 订单支付。
// POST /api/c/v1/orders/:id/pay
func (h *ClientOrderHandler) PayOrder(c *fiber.Ctx) error {
	customerID, ok := middleware.GetCustomerID(c)
	if !ok || customerID == 0 {
		return errors.New(errors.CodeUnauthorized)
	}

	orderID, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || orderID == 0 {
		return errors.New(errors.CodeInvalidParam)
	}

	var req dto.ClientPayOrderRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

	resp, err := h.clientOrderService.PayOrder(c.UserContext(), customerID, uint(orderID), &req)
	if err != nil {
		return err
	}

	return response.Success(c, resp)
}

// GetOrderDetail D3 订单详情。
// GET /api/c/v1/orders/:id
func (h *ClientOrderHandler) GetOrderDetail(c *fiber.Ctx) error {
	customerID, ok := middleware.GetCustomerID(c)
	if !ok || customerID == 0 {
		return errors.New(errors.CodeUnauthorized)
	}

	orderID, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || orderID == 0 {
		return errors.New(errors.CodeInvalidParam)
	}

	resp, err := h.clientOrderService.GetOrderDetail(c.UserContext(), customerID, uint(orderID))
	if err != nil {
		return err
	}

	return response.Success(c, resp)
}
