package grpc

import (
	"context"

	activityv1 "hyapp.local/api/proto/activity/v1"
	"hyapp/pkg/appcode"
	"hyapp/pkg/xerr"
	domain "hyapp/services/activity-service/internal/domain/activitytemplate"
)

func (s *AdminActivityTemplateServer) ListActivityTemplateLeaderboard(ctx context.Context, req *activityv1.AdminListActivityTemplateLeaderboardRequest) (*activityv1.AdminListActivityTemplateLeaderboardResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetMeta().GetAppCode())
	result, err := s.svc.AdminListRuntimeLeaderboard(ctx, domain.LeaderboardQuery{
		TemplateID: req.GetTemplateId(), VersionNo: req.GetVersionNo(), BoardType: req.GetBoardType(),
		PeriodKey: req.GetPeriodKey(), Page: req.GetPage(), PageSize: req.GetPageSize(),
	})
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	resp := &activityv1.AdminListActivityTemplateLeaderboardResponse{
		Period: &activityv1.ActivityTemplateRankPeriod{
			TemplateId: result.Period.TemplateID, TemplateCode: result.Period.TemplateCode,
			VersionNo: result.Period.VersionNo, BoardType: result.Period.BoardType, PeriodKey: result.Period.PeriodKey,
			PeriodStartMs: result.Period.StartMS, PeriodEndMs: result.Period.EndMS,
			Status: result.Period.Status, SettledAtMs: result.Period.SettledAtMS, SettleAfterMs: result.Period.SettleAfterMS,
		},
		Entries: make([]*activityv1.ActivityTemplateLeaderboardEntry, 0, len(result.Entries)), Total: result.Total,
	}
	for _, item := range result.Entries {
		resp.Entries = append(resp.Entries, activityTemplateLeaderboardEntryToProto(item))
	}
	return resp, nil
}

func (s *AdminActivityTemplateServer) ListActivityTemplateRewardJobs(ctx context.Context, req *activityv1.ListActivityTemplateRewardJobsRequest) (*activityv1.ListActivityTemplateRewardJobsResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetMeta().GetAppCode())
	items, total, err := s.svc.ListRewardJobs(ctx, domain.RewardJobQuery{
		TemplateID: req.GetTemplateId(), VersionNo: req.GetVersionNo(), BoardType: req.GetBoardType(),
		PeriodKey: req.GetPeriodKey(), Status: req.GetStatus(), Page: req.GetPage(), PageSize: req.GetPageSize(),
	})
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	resp := &activityv1.ListActivityTemplateRewardJobsResponse{Jobs: make([]*activityv1.ActivityTemplateRewardJob, 0, len(items)), Total: total}
	for _, item := range items {
		resp.Jobs = append(resp.Jobs, activityTemplateRewardJobToProto(item))
	}
	return resp, nil
}

func (s *AdminActivityTemplateServer) RetryActivityTemplateRewardJob(ctx context.Context, req *activityv1.RetryActivityTemplateRewardJobRequest) (*activityv1.RetryActivityTemplateRewardJobResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetMeta().GetAppCode())
	item, err := s.svc.RetryRewardJob(ctx, req.GetTemplateId(), req.GetRewardJobId(), req.GetOperatorAdminId())
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	return &activityv1.RetryActivityTemplateRewardJobResponse{Job: activityTemplateRewardJobToProto(item)}, nil
}

func (s *AdminActivityTemplateServer) ListActivityTemplateTaskClaims(ctx context.Context, req *activityv1.ListActivityTemplateTaskClaimsRequest) (*activityv1.ListActivityTemplateTaskClaimsResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetMeta().GetAppCode())
	items, total, err := s.svc.ListTaskClaims(ctx, domain.TaskClaimQuery{
		TemplateID: req.GetTemplateId(), VersionNo: req.GetVersionNo(), TaskDay: req.GetTaskDay(), TaskKey: req.GetTaskKey(),
		UserID: req.GetUserId(), Status: req.GetStatus(), Page: req.GetPage(), PageSize: req.GetPageSize(),
	})
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	resp := &activityv1.ListActivityTemplateTaskClaimsResponse{Claims: make([]*activityv1.ActivityTemplateTaskClaim, 0, len(items)), Total: total}
	for _, item := range items {
		resp.Claims = append(resp.Claims, activityTemplateTaskClaimToProto(item))
	}
	return resp, nil
}

func (s *AdminActivityTemplateServer) RetryActivityTemplateTaskClaim(ctx context.Context, req *activityv1.RetryActivityTemplateTaskClaimRequest) (*activityv1.RetryActivityTemplateTaskClaimResponse, error) {
	ctx = appcode.WithContext(ctx, req.GetMeta().GetAppCode())
	item, err := s.svc.RetryTaskClaim(ctx, req.GetTemplateId(), req.GetClaimId(), req.GetOperatorAdminId())
	if err != nil {
		return nil, xerr.ToGRPCError(err)
	}
	return &activityv1.RetryActivityTemplateTaskClaimResponse{Claim: activityTemplateTaskClaimToProto(item)}, nil
}
