package copy_mission

import (
	missionentity "haixun-backend/internal/model/copy_mission/domain/entity"
	missiondomain "haixun-backend/internal/model/copy_mission/domain/usecase"
)

func summaryResearchItemsToEntity(items []missiondomain.ResearchItemSummary) []missionentity.ResearchItem {
	out := make([]missionentity.ResearchItem, 0, len(items))
	for _, item := range items {
		out = append(out, missionentity.ResearchItem{
			Title:   item.Title,
			URL:     item.URL,
			Snippet: item.Snippet,
			Query:   item.Query,
		})
	}
	return out
}

func summaryToEntityResearchMap(summary missiondomain.ResearchMapSummary) missionentity.ResearchMap {
	tags := make([]missionentity.SuggestedTag, 0, len(summary.SuggestedTags))
	for _, tag := range summary.SuggestedTags {
		tags = append(tags, missionentity.SuggestedTag{
			Tag:          tag.Tag,
			Reason:       tag.Reason,
			SearchIntent: tag.SearchIntent,
			SearchType:   tag.SearchType,
		})
	}
	accounts := make([]missionentity.SimilarAccount, 0, len(summary.SimilarAccounts))
	for _, acc := range summary.SimilarAccounts {
		accounts = append(accounts, missionentity.SimilarAccount{
			Username:        acc.Username,
			Reason:          acc.Reason,
			Source:          acc.Source,
			MatchedSource:   append([]string(nil), acc.MatchedSource...),
			Confidence:      acc.Confidence,
			Status:          acc.Status,
			TopicRelevance:  acc.TopicRelevance,
			LastSeenAt:      acc.LastSeenAt,
			ProfileURL:      acc.ProfileURL,
			AuthorVerified:  acc.AuthorVerified,
			FollowerCount:   acc.FollowerCount,
			EngagementScore: acc.EngagementScore,
			LikeCount:       acc.LikeCount,
			ReplyCount:      acc.ReplyCount,
			PostCount:       acc.PostCount,
		})
	}
	samples := make([]missionentity.AudienceSample, 0, len(summary.AudienceSamples))
	for _, sample := range summary.AudienceSamples {
		samples = append(samples, missionentity.AudienceSample{
			Username:       sample.Username,
			SamplePostID:   sample.SamplePostID,
			SampleText:     sample.SampleText,
			ReplyLikeCount: sample.ReplyLikeCount,
			Appearances:    sample.Appearances,
			FirstSeenAt:    sample.FirstSeenAt,
			LastSeenAt:     sample.LastSeenAt,
			Status:         sample.Status,
		})
	}
	return missionentity.ResearchMap{
		AudienceSummary:        summary.AudienceSummary,
		ContentGoal:            summary.ContentGoal,
		Questions:              append([]string(nil), summary.Questions...),
		Pillars:                append([]string(nil), summary.Pillars...),
		Exclusions:             append([]string(nil), summary.Exclusions...),
		KnowledgeItems:         append([]string(nil), summary.KnowledgeItems...),
		SelectedKnowledgeItems: append([]string(nil), summary.SelectedKnowledgeItems...),
		ResearchItems:          summaryResearchItemsToEntity(summary.ResearchItems),
		SuggestedTags:          tags,
		SimilarAccounts:        accounts,
		AudienceSamples:        samples,
		BenchmarkNotes:         summary.BenchmarkNotes,
	}
}