package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/nodes/adminapi/common"
	"github.com/mhaya/game/game_cluster/nodes/adminapi/entity"
	"github.com/mhaya/game/game_cluster/nodes/adminapi/service"
)

type Synthesis struct {
	sev *service.Synthesis
}

func NewSynthesis() *Synthesis {
	return &Synthesis{
		sev: service.NewSynthesis(),
	}
}

// FindUserLogDaily 查询用户日活跃
func (s *Synthesis) FindUserLogDaily(ctx *gin.Context) {
	var req entity.UserLogDailyReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	resp, err := s.sev.FindMDBUserLogDaily(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, resp)
}

// FindUserRetention 查询用户留存
func (s *Synthesis) FindUserRetention(ctx *gin.Context) {
	var req entity.UserRetentionReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	resp, err := s.sev.FindUserRetention(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, resp)
}

// FindUserCountryCount 查询用户国家分布
func (s *Synthesis) FindUserCountryCount(ctx *gin.Context) {
	resp, err := s.sev.FindUserCountryCount()
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, resp)
}

// FindWithdrawal 查询提现记录
func (s *Synthesis) FindWithdrawal(ctx *gin.Context) {
	var req entity.UserWithdrawalReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	resp, err := s.sev.FindWithdrawal(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, resp)
}

// WithdrawalStatus 修改提现状态
func (s *Synthesis) WithdrawalStatus(ctx *gin.Context) {
	var req entity.UserWithdrawalStatus
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	err := s.sev.WithdrawalStatus(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, nil)
}

// WithdrawalStatusBatch 修改提现状态批量
func (s *Synthesis) WithdrawalStatusBatch(ctx *gin.Context) {
	var req entity.UserWithdrawalStatusBatch
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	err := s.sev.WithdrawalStatusBatch(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, nil)
}

// FindUserLevel 用户等级统计
func (s *Synthesis) FindUserLevel(ctx *gin.Context) {
	resp, err := s.sev.FindUserLevel()
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, resp)
}

// 综合统计
func (s *Synthesis) Overview(ctx *gin.Context) {
	// TODO Overview
}

// 活动统计
func (s *Synthesis) Activity(ctx *gin.Context) {
	// TODO Activity
}

// 转盘统计
func (s *Synthesis) Turntable(ctx *gin.Context) {
	// TODO Turntable
}

// 资产统计
func (s *Synthesis) Assets(ctx *gin.Context) {
	// TODO Assets
}

// 邀请统计
func (s *Synthesis) Invite(ctx *gin.Context) {
	// TODO Invite
}

// 后台操作记录
func (s *Synthesis) Records(ctx *gin.Context) {
	var req entity.RecordListReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.PackOkResult(ctx, code.ParamError)
		return
	}

	resp, err := s.sev.RecordList(req)
	if err != nil {
		common.PackOkResult(ctx, err.Code)
		return
	}

	common.PackOkResult(ctx, code.OK, resp)
}
