package admin

import (
	"strconv"

	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model"
	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	batchOrderSvc "github.com/break/junhong_cmp_fiber/internal/service/asset_package_batch_order"
	"github.com/break/junhong_cmp_fiber/pkg/constants"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/middleware"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

// AssetPackageBatchOrderHandler 资产套餐批量订购 Handler。
type AssetPackageBatchOrderHandler struct {
	service   *batchOrderSvc.Service
	validator *validator.Validate
}

// NewAssetPackageBatchOrderHandler 创建资产套餐批量订购 Handler。
func NewAssetPackageBatchOrderHandler(service *batchOrderSvc.Service, validator *validator.Validate) *AssetPackageBatchOrderHandler {
	return &AssetPackageBatchOrderHandler{service: service, validator: validator}
}

// Create 创建资产套餐批量订购任务。
// POST /api/admin/asset-package-batch-orders
func (h *AssetPackageBatchOrderHandler) Create(c *fiber.Ctx) error {
	var req dto.CreateAssetPackageBatchOrderRequest
	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)
	}
	userType := middleware.GetUserTypeFromContext(c.UserContext())
	if req.PaymentMethod == model.PaymentMethodOffline && userType != constants.UserTypeSuperAdmin && userType != constants.UserTypePlatform {
		return errors.New(errors.CodeForbidden, "只有平台可以使用线下支付")
	}
	if req.PaymentMethod == model.PaymentMethodWallet && userType != constants.UserTypeSuperAdmin && userType != constants.UserTypePlatform && userType != constants.UserTypeAgent {
		return errors.New(errors.CodeForbidden, "无权创建批量订购任务")
	}
	result, err := h.service.Create(c.UserContext(), &req)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// List 查询资产套餐批量订购任务列表。
// GET /api/admin/asset-package-batch-orders
func (h *AssetPackageBatchOrderHandler) List(c *fiber.Ctx) error {
	if !canAccessAssetPackageBatchOrders(middleware.GetUserTypeFromContext(c.UserContext())) {
		return errors.New(errors.CodeForbidden, "无权查询批量订购任务")
	}
	var req dto.ListAssetPackageBatchOrderRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}
	if err := h.validator.Struct(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}
	result, err := h.service.List(c.UserContext(), &req)
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

// Get 查询资产套餐批量订购任务详情。
// GET /api/admin/asset-package-batch-orders/:id
func (h *AssetPackageBatchOrderHandler) Get(c *fiber.Ctx) error {
	if !canAccessAssetPackageBatchOrders(middleware.GetUserTypeFromContext(c.UserContext())) {
		return errors.New(errors.CodeForbidden, "无权查询批量订购任务")
	}
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || id == 0 {
		return errors.New(errors.CodeInvalidParam, "任务ID无效")
	}
	result, err := h.service.GetByID(c.UserContext(), uint(id))
	if err != nil {
		return err
	}
	return response.Success(c, result)
}

func canAccessAssetPackageBatchOrders(userType int) bool {
	return userType == constants.UserTypeSuperAdmin || userType == constants.UserTypePlatform || userType == constants.UserTypeAgent
}
