package types

import (
	inspireDomain "apps/backend/internal/module/inspire/domain"
	scoutDomain "apps/backend/internal/module/scout/domain"
)

func TrendFromDomain(t *inspireDomain.TrendItem) TrendPublic {
	if t == nil {
		return TrendPublic{}
	}
	return TrendPublic{
		Id: t.ID, Kind: t.Kind, Label: t.Label, Summary: t.Summary, Heat: t.Heat,
		Keywords: t.Keywords, Samples: t.Samples, SourceLabel: t.SourceLabel, ObservedAt: t.ObservedAt,
	}
}

func ElementFromDomain(e *inspireDomain.Element) InspireElementPublic {
	if e == nil {
		return InspireElementPublic{}
	}
	return InspireElementPublic{
		Id: e.ID, Kind: e.Kind, Title: e.Title, Body: e.Body, RefId: e.RefID,
		Reusable: e.Reusable, CreatedAt: e.CreatedAt, UpdatedAt: e.UpdatedAt,
	}
}

func SessionFromDomain(s *inspireDomain.Session) InspireSessionPublic {
	if s == nil {
		return InspireSessionPublic{}
	}
	msgs := make([]InspireMessagePublic, 0, len(s.Messages))
	for _, m := range s.Messages {
		mp := InspireMessagePublic{Id: m.ID, Role: m.Role, Text: m.Text, CreatedAt: m.CreatedAt}
		if m.Draft != nil {
			mp.Draft = InspireDraftPublic{Title: m.Draft.Title, Body: m.Draft.Body}
		}
		msgs = append(msgs, mp)
	}
	return InspireSessionPublic{
		Id: s.ID, Title: s.Title, Messages: msgs, PinnedElementIds: s.PinnedElementIDs,
		CreatedAt: s.CreatedAt, UpdatedAt: s.UpdatedAt,
	}
}

func SessionSummaryFromDomain(s inspireDomain.SessionSummary) InspireSessionSummaryPublic {
	return InspireSessionSummaryPublic{
		Id: s.ID, Title: s.Title, Preview: s.Preview, MessageCount: s.MessageCount,
		UpdatedAt: s.UpdatedAt, CreatedAt: s.CreatedAt, Active: s.Active,
	}
}

func ResearchHitFromDomain(h *inspireDomain.ResearchHit) ResearchHitPublic {
	if h == nil {
		return ResearchHitPublic{}
	}
	return ResearchHitPublic{
		Id: h.ID, Title: h.Title, Snippet: h.Snippet, Url: h.URL, Summary: h.Summary,
		LearnPoints: h.LearnPoints, ReplyHooks: h.ReplyHooks, SourceLabel: h.SourceLabel,
	}
}

func BrandFromDomain(b *scoutDomain.Brand) BrandPublic {
	if b == nil {
		return BrandPublic{}
	}
	return BrandPublic{
		Id: b.ID, DisplayName: b.DisplayName, Brief: b.Brief,
		TargetAudience: b.TargetAudience, Goals: b.Goals,
		LearningSummary: b.LearningSummary, LearningVersion: b.LearningVersion,
		LearnedFromPostsCount: b.LearnedFromPostsCount, LastLearnedAt: b.LastLearnedAt,
	}
}

func ProductFromDomain(p *scoutDomain.Product) ProductPublic {
	if p == nil {
		return ProductPublic{}
	}
	return ProductPublic{
		Id: p.ID, BrandId: p.BrandID, Label: p.Label, ProductContext: p.ProductContext,
		MatchTags: p.MatchTags, PainPoints: p.PainPoints,
		ProviderCapabilityTerms: p.ProviderCapabilityTerms, ProviderExcludeTerms: p.ProviderExcludeTerms,
		PlacementUrl: p.PlacementURL,
		CreatedAt:    p.CreatedAt, UpdatedAt: p.UpdatedAt,
	}
}

func BriefFromDomain(b *scoutDomain.RunBrief) ScoutBriefPublic {
	if b == nil {
		return ScoutBriefPublic{}
	}
	return ScoutBriefPublic{
		Intent: b.Intent, Mode: b.Mode, BrandId: b.BrandID, ProductId: b.ProductID,
		ProductLabel: b.ProductLabel, Pains: b.Pains, Tags: b.Tags, Periphery: b.Periphery,
		ScanTerms: b.ScanTerms, PlacementNote: b.PlacementNote, ResponseStance: b.ResponseStance,
		ThemeKey: b.ThemeKey, ThemeLabel: b.ThemeLabel, ProductContext: b.ProductContext,
		TargetCount: b.TargetCount,
	}
}

func BriefToDomain(b *ScoutBriefPublic) *scoutDomain.RunBrief {
	if b == nil {
		return nil
	}
	return &scoutDomain.RunBrief{
		Intent: b.Intent, Mode: b.Mode, BrandID: b.BrandId, ProductID: b.ProductId,
		ProductLabel: b.ProductLabel, Pains: b.Pains, Tags: b.Tags, Periphery: b.Periphery,
		ScanTerms: b.ScanTerms, PlacementNote: b.PlacementNote, ResponseStance: b.ResponseStance,
		ThemeKey: b.ThemeKey, ThemeLabel: b.ThemeLabel, ProductContext: b.ProductContext,
		TargetCount: b.TargetCount,
	}
}

func ScoutPostFromDomain(p *scoutDomain.Post) ScoutPostPublic {
	if p == nil {
		return ScoutPostPublic{}
	}
	return ScoutPostPublic{
		Id: p.ID, RunId: p.RunID, BrandId: p.BrandID, Author: p.Author, Text: p.Text, SearchTag: p.SearchTag,
		Opportunity: p.Opportunity, OutreachStatus: p.OutreachStatus, DraftText: p.DraftText,
		Score: p.Score, MatchedProductId: p.MatchedProductID, MatchedProductLabel: p.MatchedProductLabel,
		MatchReason: p.MatchReason, ScoutMode: p.ScoutMode, IntentSnippet: p.IntentSnippet,
		ThemeKey: p.ThemeKey, ThemeLabel: p.ThemeLabel, ScanPath: p.ScanPath,
		Classification: p.Classification, Permalink: p.Permalink,
		PostedAt: p.PostedAt, CreatedAt: p.CreatedAt,
	}
}

func ScoutRunFromDomain(r *scoutDomain.Run) ScoutRunPublic {
	if r == nil {
		return ScoutRunPublic{}
	}
	reasons := append([]string(nil), r.ShortfallReasons...)
	return ScoutRunPublic{
		Id: r.ID, JobId: r.JobID, ThemeKey: r.ThemeKey, ThemeLabel: r.ThemeLabel,
		Intent: r.Intent, Mode: r.Mode, BrandId: r.BrandID, TargetCount: r.TargetCount,
		Status: r.Status, SearchedCount: r.SearchedCount, DuplicateCount: r.DuplicateCount,
		IrrelevantCount: r.IrrelevantCount, EligibleCount: r.EligibleCount,
		PendingCount: r.PendingCount, ShortfallCount: r.ShortfallCount,
		ShortfallReasons: reasons, CreatedAt: r.CreatedAt, StartedAt: r.StartedAt,
		CompletedAt: r.CompletedAt, Error: r.Error,
	}
}

func PaginationFromDomain(p scoutDomain.PageInfo) Pagination {
	return Pagination{Page: p.Page, PageSize: p.PageSize, Total: p.Total, TotalPages: p.TotalPages}
}

func HomeworkFromDomain(h *scoutDomain.Homework) ScoutHomeworkPublic {
	if h == nil {
		return ScoutHomeworkPublic{}
	}
	return ScoutHomeworkPublic{
		ThemeKey: h.ThemeKey, ThemeLabel: h.ThemeLabel, Purpose: h.Purpose,
		Brief: BriefFromDomain(&h.Brief), CreatedAt: h.CreatedAt,
	}
}
