package admin

import (
	"strconv"

	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	wechatConfigService "github.com/break/junhong_cmp_fiber/internal/service/wechat_config"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

// WechatConfigHandler 微信参数配置 HTTP 处理器
type WechatConfigHandler struct {
	service *wechatConfigService.Service
}

// NewWechatConfigHandler 创建微信参数配置处理器实例
func NewWechatConfigHandler(service *wechatConfigService.Service) *WechatConfigHandler {
	return &WechatConfigHandler{service: service}
}

// Create 创建微信参数配置
// POST /api/admin/wechat-configs
func (h *WechatConfigHandler) Create(c *fiber.Ctx) error {
	var req dto.CreateWechatConfigRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

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

	return response.Success(c, result)
}

// List 获取微信参数配置列表
// GET /api/admin/wechat-configs
func (h *WechatConfigHandler) List(c *fiber.Ctx) error {
	var req dto.WechatConfigListRequest
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	configs, total, err := h.service.List(c.UserContext(), &req)
	if err != nil {
		return err
	}

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

// Get 获取微信参数配置详情
// GET /api/admin/wechat-configs/:id
func (h *WechatConfigHandler) Get(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的配置 ID")
	}

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

	return response.Success(c, result)
}

// Update 更新微信参数配置
// PUT /api/admin/wechat-configs/:id
func (h *WechatConfigHandler) Update(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的配置 ID")
	}

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

	result, err := h.service.Update(c.UserContext(), uint(id), &req)
	if err != nil {
		return err
	}

	return response.Success(c, result)
}

// Delete 删除微信参数配置
// DELETE /api/admin/wechat-configs/:id
func (h *WechatConfigHandler) Delete(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的配置 ID")
	}

	if err := h.service.Delete(c.UserContext(), uint(id)); err != nil {
		return err
	}

	return response.Success(c, nil)
}

// Activate 激活微信参数配置
// POST /api/admin/wechat-configs/:id/activate
func (h *WechatConfigHandler) Activate(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的配置 ID")
	}

	result, err := h.service.Activate(c.UserContext(), uint(id))
	if err != nil {
		return err
	}

	return response.Success(c, result)
}

// Deactivate 停用微信参数配置
// POST /api/admin/wechat-configs/:id/deactivate
func (h *WechatConfigHandler) Deactivate(c *fiber.Ctx) error {
	id, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的配置 ID")
	}

	result, err := h.service.Deactivate(c.UserContext(), uint(id))
	if err != nil {
		return err
	}

	return response.Success(c, result)
}

// GetActive 获取当前生效的微信参数配置
// GET /api/admin/wechat-configs/active
func (h *WechatConfigHandler) GetActive(c *fiber.Ctx) error {
	result, err := h.service.GetActiveConfigForAPI(c.UserContext())
	if err != nil {
		return err
	}

	if result == nil {
		return response.SuccessWithMessage(c, nil, "当前无生效的支付配置，仅支持钱包支付")
	}

	return response.Success(c, result)
}
