package activityapi

import (
	"context"

	"hyapp/pkg/userleaderboard"
	"hyapp/services/gateway-service/internal/client"
	"hyapp/services/gateway-service/internal/transport/http/httproutes"
)

// Handler owns activity-facing HTTP endpoints.
// It keeps task and registration reward reads/writes behind activity-service, with gateway only adding auth context.
type Handler struct {
	taskClient           client.TaskClient
	growthLevelClient    client.GrowthLevelClient
	achievementClient    client.AchievementClient
	userProfileClient    client.UserProfileClient
	roomQueryClient      client.RoomQueryClient
	walletClient         client.WalletClient
	userLeaderboard      UserLeaderboardStore
	registrationReward   client.RegistrationRewardClient
	firstRechargeReward  client.FirstRechargeRewardClient
	cumulativeRecharge   client.CumulativeRechargeRewardClient
	inviteActivityReward client.InviteActivityRewardClient
	sevenDayCheckIn      client.SevenDayCheckInClient
	luckyGift            client.LuckyGiftClient
	roomTurnoverReward   client.RoomTurnoverRewardClient
	weeklyStar           client.WeeklyStarClient
	cpWeeklyRank         client.CPWeeklyRankClient
	agencyOpening        client.AgencyOpeningClient
	wheel                client.WheelClient
}

type UserLeaderboardStore interface {
	List(ctx context.Context, query userleaderboard.Query) (userleaderboard.Page, error)
	RankForUser(ctx context.Context, query userleaderboard.Query, userID int64) (userleaderboard.Entry, bool, error)
}

type Config struct {
	TaskClient           client.TaskClient
	GrowthLevelClient    client.GrowthLevelClient
	AchievementClient    client.AchievementClient
	UserProfileClient    client.UserProfileClient
	RoomQueryClient      client.RoomQueryClient
	WalletClient         client.WalletClient
	UserLeaderboard      UserLeaderboardStore
	RegistrationReward   client.RegistrationRewardClient
	FirstRechargeReward  client.FirstRechargeRewardClient
	CumulativeRecharge   client.CumulativeRechargeRewardClient
	InviteActivityReward client.InviteActivityRewardClient
	SevenDayCheckIn      client.SevenDayCheckInClient
	LuckyGift            client.LuckyGiftClient
	RoomTurnoverReward   client.RoomTurnoverRewardClient
	WeeklyStar           client.WeeklyStarClient
	CPWeeklyRank         client.CPWeeklyRankClient
	AgencyOpening        client.AgencyOpeningClient
	Wheel                client.WheelClient
}

func New(config Config) *Handler {
	return &Handler{
		taskClient:           config.TaskClient,
		growthLevelClient:    config.GrowthLevelClient,
		achievementClient:    config.AchievementClient,
		userProfileClient:    config.UserProfileClient,
		roomQueryClient:      config.RoomQueryClient,
		walletClient:         config.WalletClient,
		userLeaderboard:      config.UserLeaderboard,
		registrationReward:   config.RegistrationReward,
		firstRechargeReward:  config.FirstRechargeReward,
		cumulativeRecharge:   config.CumulativeRecharge,
		inviteActivityReward: config.InviteActivityReward,
		sevenDayCheckIn:      config.SevenDayCheckIn,
		luckyGift:            config.LuckyGift,
		roomTurnoverReward:   config.RoomTurnoverReward,
		weeklyStar:           config.WeeklyStar,
		cpWeeklyRank:         config.CPWeeklyRank,
		agencyOpening:        config.AgencyOpening,
		wheel:                config.Wheel,
	}
}

func (h *Handler) TaskHandlers() httproutes.TaskHandlers {
	return httproutes.TaskHandlers{
		ListTaskTabs:                      h.listTaskTabs,
		ClaimTaskReward:                   h.claimTaskReward,
		GetRegistrationRewardEligibility:  h.getRegistrationRewardEligibility,
		GetFirstRechargeRewardStatus:      h.getFirstRechargeRewardStatus,
		GetCumulativeRechargeRewardStatus: h.getCumulativeRechargeRewardStatus,
		GetInviteActivityRewardStatus:     h.getInviteActivityRewardStatus,
		ClaimInviteActivityReward:         h.claimInviteActivityReward,
		ListInviteActivityLeaderboard:     h.listInviteActivityLeaderboard,
		GetSevenDayCheckInStatus:          h.getSevenDayCheckInStatus,
		SignSevenDayCheckIn:               h.signSevenDayCheckIn,
		ListUserLeaderboards:              h.listUserLeaderboards,
		CheckLuckyGift:                    h.checkLuckyGift,
		GetRoomTurnoverRewardStatus:       h.getRoomTurnoverRewardStatus,
		GetWeeklyStarCurrent:              h.getWeeklyStarCurrent,
		ListWeeklyStarLeaderboard:         h.listWeeklyStarLeaderboard,
		ListWeeklyStarHistory:             h.listWeeklyStarHistory,
		GetCPWeeklyRankStatus:             h.getCPWeeklyRankStatus,
		GetAgencyOpeningStatus:            h.getAgencyOpeningStatus,
		ApplyAgencyOpening:                h.applyAgencyOpening,
		ListAgencyOpeningLeaderboard:      h.listAgencyOpeningLeaderboard,
		GetWheelConfig:                    h.getWheelConfig,
		DrawWheel:                         h.drawWheel,
		ListWheelHistory:                  h.listWheelHistory,
		ListWheelHints:                    h.listWheelHints,
	}
}

func (h *Handler) LevelHandlers() httproutes.LevelHandlers {
	return httproutes.LevelHandlers{
		GetMyLevelOverview: h.getMyLevelOverview,
		GetLevelTrack:      h.getLevelTrack,
		ListLevelRewards:   h.listLevelRewards,
	}
}

func (h *Handler) AchievementHandlers() httproutes.AchievementHandlers {
	return httproutes.AchievementHandlers{
		ListAchievements: h.listAchievements,
		ListMyBadges:     h.listMyBadges,
		SetBadgeDisplay:  h.setBadgeDisplay,
	}
}
