package admin

import (
	"strconv"

	"github.com/gofiber/fiber/v2"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	shopQuery "github.com/break/junhong_cmp_fiber/internal/query/shop"
	shopCommissionService "github.com/break/junhong_cmp_fiber/internal/service/shop_commission"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/response"
)

// ShopCommissionHandler 代理商资金管理 Handler
type ShopCommissionHandler struct {
	service          *shopCommissionService.Service
	fundSummaryQuery *shopQuery.FundSummaryQuery
}

// NewShopCommissionHandler 创建代理商资金管理 Handler
func NewShopCommissionHandler(service *shopCommissionService.Service) *ShopCommissionHandler {
	return &ShopCommissionHandler{service: service}
}

// SetFundSummaryQuery 注入代理商资金概况 Query。
func (h *ShopCommissionHandler) SetFundSummaryQuery(query *shopQuery.FundSummaryQuery) {
	h.fundSummaryQuery = query
}

// ListFundSummary 代理商资金概况列表
// GET /api/admin/shops/fund-summary
func (h *ShopCommissionHandler) ListFundSummary(c *fiber.Ctx) error {
	var req dto.ShopFundSummaryListReq
	if err := c.QueryParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam)
	}

	if h.fundSummaryQuery == nil {
		return errors.New(errors.CodeInternalError, "代理商资金概况查询能力未配置")
	}
	result, err := h.fundSummaryQuery.List(c.UserContext(), req)
	if err != nil {
		return err
	}

	return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
}

// ListWithdrawalRequests 代理商提现记录列表
// GET /api/admin/shops/:shop_id/withdrawal-requests
func (h *ShopCommissionHandler) ListWithdrawalRequests(c *fiber.Ctx) error {
	shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
	}

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

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

	return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
}

// ListCommissionRecords 代理商佣金明细列表
// GET /api/admin/shops/:shop_id/commission-records
func (h *ShopCommissionHandler) ListCommissionRecords(c *fiber.Ctx) error {
	shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
	}

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

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

	return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
}

// ResolveCommissionRecord 修正待审佣金记录
// POST /api/admin/commission-records/:id/resolve
func (h *ShopCommissionHandler) ResolveCommissionRecord(c *fiber.Ctx) error {
	recordID, err := strconv.ParseUint(c.Params("id"), 10, 64)
	if err != nil || recordID == 0 {
		return errors.New(errors.CodeInvalidParam, "无效的佣金记录ID")
	}

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

	if err := h.service.ResolveCommissionRecord(c.UserContext(), uint(recordID), &req); err != nil {
		return err
	}

	return response.Success(c, nil)
}

// GetCommissionStats 代理商佣金统计
// GET /api/admin/shops/:shop_id/commission-stats
func (h *ShopCommissionHandler) GetCommissionStats(c *fiber.Ctx) error {
	shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
	}

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

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

	return response.Success(c, result)
}

// GetCommissionDailyStats 代理商每日佣金统计
// GET /api/admin/shops/:shop_id/commission-daily-stats
func (h *ShopCommissionHandler) GetCommissionDailyStats(c *fiber.Ctx) error {
	shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
	}

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

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

	return response.Success(c, result)
}

// CreateWithdrawal 发起提现申请
// POST /api/admin/shops/:shop_id/withdrawal-requests
func (h *ShopCommissionHandler) CreateWithdrawal(c *fiber.Ctx) error {
	shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
	}

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

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

	return response.Success(c, result)
}

// ListMainWalletTransactions 预充值钱包流水列表
// GET /api/admin/shops/:shop_id/main-wallet/transactions
func (h *ShopCommissionHandler) ListMainWalletTransactions(c *fiber.Ctx) error {
	shopID, err := strconv.ParseUint(c.Params("shop_id"), 10, 64)
	if err != nil {
		return errors.New(errors.CodeInvalidParam, "无效的店铺 ID")
	}

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

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

	return response.SuccessWithPagination(c, result.Items, result.Total, result.Page, result.Size)
}
