package admin

import (
	"context"
	"strconv"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	exchangeService "github.com/break/junhong_cmp_fiber/internal/service/exchange"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/logger"
	"github.com/break/junhong_cmp_fiber/pkg/response"
	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"
	"go.uber.org/zap"
)

// ExchangeLister 定义换货列表读取用例。
type ExchangeLister interface {
	List(ctx context.Context, req *dto.ExchangeListRequest) (*dto.ExchangeListResponse, error)
}

// ExchangeHandler 处理后台换货管理接口。
type ExchangeHandler struct {
	service   *exchangeService.Service
	listQuery ExchangeLister
	validator *validator.Validate
}

// NewExchangeHandler 创建后台换货管理 Handler。
func NewExchangeHandler(service *exchangeService.Service, listQuery ExchangeLister, validator *validator.Validate) *ExchangeHandler {
	return &ExchangeHandler{service: service, listQuery: listQuery, validator: validator}
}

// Create 创建换货单。
// POST /api/admin/exchanges
func (h *ExchangeHandler) Create(c *fiber.Ctx) error {
	var req dto.CreateExchangeRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}
	if err := h.validator.Struct(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

	data, err := h.service.Create(c.UserContext(), &req)
	if err != nil {
		return err
	}
	return response.Success(c, data)
}

// List 查询换货单列表。
// GET /api/admin/exchanges
func (h *ExchangeHandler) List(c *fiber.Ctx) error {
	var req dto.ExchangeListRequest
	if err := c.QueryParser(&req); err != nil {
		h.logListValidationFailure(c, err)
		return errors.New(errors.CodeInvalidParam)
	}
	if err := h.validator.Struct(&req); err != nil {
		h.logListValidationFailure(c, err)
		return errors.New(errors.CodeInvalidParam)
	}

	data, err := h.listQuery.List(c.UserContext(), &req)
	if err != nil {
		return err
	}
	return response.Success(c, data)
}

func (h *ExchangeHandler) logListValidationFailure(c *fiber.Ctx, err error) {
	logger.GetAppLogger().Warn("换货列表参数验证失败",
		zap.String("method", c.Method()),
		zap.String("path", c.Path()),
		zap.String("query", c.Context().QueryArgs().String()),
		zap.Error(err),
	)
}

// Get 查询换货单详情。
// GET /api/admin/exchanges/:id
func (h *ExchangeHandler) Get(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

	data, err := h.service.Get(c.UserContext(), uint(id))
	if err != nil {
		return err
	}
	return response.Success(c, data)
}

// Ship 执行物流换货发货。
// POST /api/admin/exchanges/:id/ship
func (h *ExchangeHandler) Ship(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

	var req dto.ExchangeShipRequest
	if err = c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}
	if err = h.validator.Struct(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

	data, err := h.service.Ship(c.UserContext(), uint(id), &req)
	if err != nil {
		return err
	}
	return response.Success(c, data)
}

// Complete 确认换货完成。
// POST /api/admin/exchanges/:id/complete
func (h *ExchangeHandler) Complete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

	if err = h.service.Complete(c.UserContext(), uint(id)); err != nil {
		return err
	}
	return response.Success(c, nil)
}

// Cancel 取消换货单。
// POST /api/admin/exchanges/:id/cancel
func (h *ExchangeHandler) Cancel(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

	var req dto.ExchangeCancelRequest
	if err = c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}
	if err = h.validator.Struct(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

	if err = h.service.Cancel(c.UserContext(), uint(id), &req); err != nil {
		return err
	}
	return response.Success(c, nil)
}

// Renew 将已换出的旧资产转为新资产。
// POST /api/admin/exchanges/:id/renew
func (h *ExchangeHandler) Renew(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的换货单ID")
	}

	if err = h.service.Renew(c.UserContext(), uint(id)); err != nil {
		return err
	}
	return response.Success(c, nil)
}
