package activitytemplate

import (
	"strings"

	activityv1 "hyapp.local/api/proto/activity/v1"
)

type localeRequest struct {
	Locale string `json:"locale"`
	Title  string `json:"title"`
	Rules  string `json:"rules"`
}

type giftRequest struct {
	GiftID       string `json:"gift_id"`
	SortOrder    int32  `json:"sort_order"`
	Name         string `json:"name"`
	IconURL      string `json:"icon_url"`
	CoinPrice    int64  `json:"coin_price"`
	PriceVersion string `json:"price_version"`
}

type taskRequest struct {
	TaskKey               string `json:"task_key"`
	TaskType              string `json:"task_type"`
	TargetValue           int64  `json:"target_value"`
	RewardResourceGroupID int64  `json:"reward_resource_group_id"`
	SortOrder             int32  `json:"sort_order"`
}

type rankRewardRequest struct {
	BoardType       string `json:"board_type"`
	RankFrom        int32  `json:"rank_from"`
	RankTo          int32  `json:"rank_to"`
	ResourceGroupID int64  `json:"resource_group_id"`
	SortOrder       int32  `json:"sort_order"`
}

type assetRequest struct {
	AssetKey  string `json:"asset_key"`
	Locale    string `json:"locale"`
	URL       string `json:"url"`
	MediaType string `json:"media_type"`
	SortOrder int32  `json:"sort_order"`
}

// templateRequest 与 HTTP JSON 一一对应；不在 admin 层重复业务校验，确保保存和发布都复用 activity-service 的同一规则。
type templateRequest struct {
	TemplateCode         string              `json:"template_code"`
	Name                 string              `json:"name"`
	ActivityType         string              `json:"activity_type"`
	StartMS              int64               `json:"start_ms"`
	EndMS                int64               `json:"end_ms"`
	AllRegions           bool                `json:"all_regions"`
	RegionIDs            []int64             `json:"region_ids"`
	Locales              []localeRequest     `json:"locales"`
	Gifts                []giftRequest       `json:"gifts"`
	Tasks                []taskRequest       `json:"tasks"`
	DailyLeaderboardSize int32               `json:"daily_leaderboard_size"`
	TotalLeaderboardSize int32               `json:"total_leaderboard_size"`
	RankRewards          []rankRewardRequest `json:"rank_rewards"`
	Assets               []assetRequest      `json:"assets"`
	DisplayConfigJSON    string              `json:"display_config_json"`
	ExpectedRevision     int64               `json:"expected_revision,omitempty"`
}

type statusRequest struct {
	Status           string `json:"status"`
	ExpectedRevision int64  `json:"expected_revision"`
}

type cloneRequest struct {
	SourceVersion int64  `json:"source_version"`
	TemplateCode  string `json:"template_code"`
	Name          string `json:"name"`
}

type validateRequest struct {
	Template   templateRequest `json:"template"`
	ForPublish bool            `json:"for_publish"`
}

func (r templateRequest) toProto(templateID string) *activityv1.ActivityTemplate {
	item := &activityv1.ActivityTemplate{
		TemplateId:           strings.TrimSpace(templateID),
		TemplateCode:         strings.TrimSpace(r.TemplateCode),
		Name:                 strings.TrimSpace(r.Name),
		ActivityType:         strings.TrimSpace(r.ActivityType),
		StartMs:              r.StartMS,
		EndMs:                r.EndMS,
		AllRegions:           r.AllRegions,
		RegionIds:            append([]int64(nil), r.RegionIDs...),
		Locales:              make([]*activityv1.ActivityTemplateLocale, 0, len(r.Locales)),
		Gifts:                make([]*activityv1.ActivityTemplateGift, 0, len(r.Gifts)),
		Tasks:                make([]*activityv1.ActivityTemplateTask, 0, len(r.Tasks)),
		DailyLeaderboardSize: r.DailyLeaderboardSize,
		TotalLeaderboardSize: r.TotalLeaderboardSize,
		RankRewards:          make([]*activityv1.ActivityTemplateRankReward, 0, len(r.RankRewards)),
		Assets:               make([]*activityv1.ActivityTemplateAsset, 0, len(r.Assets)),
		DisplayConfigJson:    strings.TrimSpace(r.DisplayConfigJSON),
	}
	for _, locale := range r.Locales {
		item.Locales = append(item.Locales, &activityv1.ActivityTemplateLocale{Locale: locale.Locale, Title: locale.Title, Rules: locale.Rules})
	}
	for _, gift := range r.Gifts {
		item.Gifts = append(item.Gifts, &activityv1.ActivityTemplateGift{
			GiftId: gift.GiftID, SortOrder: gift.SortOrder, Name: gift.Name, IconUrl: gift.IconURL,
			CoinPrice: gift.CoinPrice, PriceVersion: gift.PriceVersion,
		})
	}
	for _, task := range r.Tasks {
		item.Tasks = append(item.Tasks, &activityv1.ActivityTemplateTask{
			TaskKey: task.TaskKey, TaskType: task.TaskType, TargetValue: task.TargetValue,
			RewardResourceGroupId: task.RewardResourceGroupID, SortOrder: task.SortOrder,
		})
	}
	for _, reward := range r.RankRewards {
		item.RankRewards = append(item.RankRewards, &activityv1.ActivityTemplateRankReward{
			BoardType: reward.BoardType, RankFrom: reward.RankFrom, RankTo: reward.RankTo,
			ResourceGroupId: reward.ResourceGroupID, SortOrder: reward.SortOrder,
		})
	}
	for _, asset := range r.Assets {
		item.Assets = append(item.Assets, &activityv1.ActivityTemplateAsset{
			AssetKey: asset.AssetKey, Locale: asset.Locale, Url: asset.URL, MediaType: asset.MediaType, SortOrder: asset.SortOrder,
		})
	}
	return item
}
