package brand

import (
	brandentity "haixun-backend/internal/model/brand/domain/entity"
	domusecase "haixun-backend/internal/model/brand/domain/usecase"
	"haixun-backend/internal/types"
)

func toBrandData(item domusecase.BrandSummary) types.BrandData {
	products := make([]types.BrandProductData, 0, len(item.Products))
	for _, product := range item.Products {
		products = append(products, toBrandProductData(product))
	}
	return types.BrandData{
		ID:             item.ID,
		DisplayName:    item.DisplayName,
		TopicName:      item.TopicName,
		SeedQuery:      item.SeedQuery,
		Brief:          item.Brief,
		ProductBrief:   item.ProductBrief,
		ProductContext: item.ProductContext,
		ProductID:      item.ProductID,
		Products:       products,
		TargetAudience: item.TargetAudience,
		Goals:          item.Goals,
		ResearchMap:    toResearchMapData(item.ResearchMap),
		CreateAt:       item.CreateAt,
		UpdateAt:       item.UpdateAt,
	}
}

func toBrandProductData(item domusecase.ProductSummary) types.BrandProductData {
	return types.BrandProductData{
		ID:             item.ID,
		Label:          item.Label,
		ProductContext: item.ProductContext,
		MatchTags:      append([]string(nil), item.MatchTags...),
		CreateAt:       item.CreateAt,
		UpdateAt:       item.UpdateAt,
	}
}

func toListBrandProductsData(result *domusecase.ListProductsResult) *types.ListBrandProductsData {
	if result == nil {
		return &types.ListBrandProductsData{List: []types.BrandProductData{}}
	}
	list := make([]types.BrandProductData, 0, len(result.List))
	for _, item := range result.List {
		list = append(list, toBrandProductData(item))
	}
	return &types.ListBrandProductsData{List: list}
}

func toResearchMapData(item brandentity.ResearchMap) types.ResearchMapData {
	items := make([]types.ResearchItemData, 0, len(item.ResearchItems))
	for _, researchItem := range item.ResearchItems {
		items = append(items, types.ResearchItemData{
			Title:   researchItem.Title,
			URL:     researchItem.URL,
			Snippet: researchItem.Snippet,
			Query:   researchItem.Query,
		})
	}
	return types.ResearchMapData{
		AudienceSummary: item.AudienceSummary,
		ContentGoal:     item.ContentGoal,
		Questions:       item.Questions,
		Pillars:         item.Pillars,
		Exclusions:      item.Exclusions,
		ResearchItems:   items,
		ExpandStrategy:  item.ExpandStrategy,
		PatrolKeywords:  append([]string(nil), item.PatrolKeywords...),
	}
}

func toListData(result *domusecase.ListResult) *types.ListBrandsData {
	if result == nil {
		return &types.ListBrandsData{List: []types.BrandData{}}
	}
	list := make([]types.BrandData, 0, len(result.List))
	for _, item := range result.List {
		list = append(list, toBrandData(item))
	}
	return &types.ListBrandsData{List: list}
}

func toBrandPatch(req *types.UpdateBrandReq) domusecase.BrandPatch {
	if req == nil {
		return domusecase.BrandPatch{}
	}
	patch := domusecase.BrandPatch{
		DisplayName:    req.DisplayName,
		TopicName:      req.TopicName,
		SeedQuery:      req.SeedQuery,
		Brief:          req.Brief,
		ProductBrief:   req.ProductBrief,
		ProductContext: req.ProductContext,
		ProductID:      req.ProductID,
		TargetAudience: req.TargetAudience,
		Goals:          req.Goals,
	}
	if req.AudienceSummary != nil {
		patch.AudienceSummary = req.AudienceSummary
	}
	if req.ContentGoal != nil {
		patch.ContentGoal = req.ContentGoal
	}
	if req.Questions != nil {
		patch.QuestionsSet = true
		patch.Questions = append([]string(nil), req.Questions...)
	}
	if req.Pillars != nil {
		patch.PillarsSet = true
		patch.Pillars = append([]string(nil), req.Pillars...)
	}
	if req.Exclusions != nil {
		patch.ExclusionsSet = true
		patch.Exclusions = append([]string(nil), req.Exclusions...)
	}
	if req.PatrolKeywords != nil {
		patch.PatrolKeywordsSet = true
		patch.PatrolKeywords = append([]string(nil), req.PatrolKeywords...)
	}
	return patch
}
