package growthmap

import (
	"apps/backend/internal/module/growth/domain"
	"apps/backend/internal/types"
)

func Outcome(e *domain.OutcomeEvent) *types.OutcomeEventPublic {
	if e == nil {
		return nil
	}
	return &types.OutcomeEventPublic{
		Id:                 e.ID,
		SourceType:         e.SourceType,
		SourceId:           e.SourceID,
		ThreadsAccountId:   e.ThreadsAccountID,
		Kind:               e.Kind,
		Confidence:         e.Confidence,
		Status:             e.Status,
		ReplyCountDelta:    e.ReplyCountDelta,
		LikeCountDelta:     e.LikeCountDelta,
		FollowersDelta:     e.FollowersDelta,
		ConversionAmount:   e.ConversionAmount,
		ConversionNote:     e.ConversionNote,
		ConversionCurrency: e.ConversionCurrency,
		WindowEndsAt:       e.WindowEndsAt,
		SentAt:             e.SentAt,
		CreatedAt:          e.CreatedAt,
		UpdatedAt:          e.UpdatedAt,
	}
}

func OutcomeList(list []*domain.OutcomeEvent) []types.OutcomeEventPublic {
	out := make([]types.OutcomeEventPublic, 0, len(list))
	for _, e := range list {
		if p := Outcome(e); p != nil {
			out = append(out, *p)
		}
	}
	return out
}

func Summary(s *domain.OutcomeSummary) *types.OutcomeSummaryData {
	if s == nil {
		return nil
	}
	return &types.OutcomeSummaryData{
		From:             s.From,
		To:               s.To,
		Reach:            s.Reach,
		Conversations:    s.Conversations,
		FollowsPossible:  s.FollowsPossible,
		FollowsConfirmed: s.FollowsConfirmed,
		Conversions:      s.Conversions,
		ConversionAmount: s.ConversionAmount,
	}
}

func Checkup(c *domain.WeeklyCheckup) *types.WeeklyCheckupPublic {
	if c == nil {
		return nil
	}
	findings := make([]types.CheckupFindingPublic, 0, len(c.Findings))
	for _, f := range c.Findings {
		findings = append(findings, types.CheckupFindingPublic{Claim: f.Claim, Evidence: f.Evidence})
	}
	actions := make([]types.CheckupActionPublic, 0, len(c.Actions))
	for _, a := range c.Actions {
		actions = append(actions, types.CheckupActionPublic{Title: a.Title, Reason: a.Reason, Deeplink: a.Deeplink})
	}
	return &types.WeeklyCheckupPublic{
		Id: c.ID, WeekKey: c.WeekKey, Status: c.Status, Summary: c.Summary,
		Findings: findings, Actions: actions, Error: c.Error,
		CreatedAt: c.CreatedAt, UpdatedAt: c.UpdatedAt,
	}
}

func CheckupList(list []*domain.WeeklyCheckup) []types.WeeklyCheckupPublic {
	out := make([]types.WeeklyCheckupPublic, 0, len(list))
	for _, c := range list {
		if p := Checkup(c); p != nil {
			out = append(out, *p)
		}
	}
	return out
}

func Health(h *domain.AccountHealth) *types.AccountHealthPublic {
	if h == nil {
		return nil
	}
	return &types.AccountHealthPublic{
		ThreadsAccountId: h.ThreadsAccountID,
		Score:            h.Score,
		Level:            h.Level,
		Factors:          h.Factors,
		Advice:           h.Advice,
		ComputedAt:       h.ComputedAt,
	}
}

func Workspace(w *domain.Workspace) *types.WorkspacePublic {
	if w == nil {
		return nil
	}
	return &types.WorkspacePublic{
		Id: w.ID, Name: w.Name, ReviewRequired: w.ReviewRequired,
		IsDefault: w.IsDefault, Archived: w.Archived,
		FooterText: w.FooterText, HidePoweredBy: w.HidePoweredBy, BrandName: w.BrandName,
		CreatedAt: w.CreatedAt, UpdatedAt: w.UpdatedAt,
	}
}

func WorkspaceList(list []*domain.Workspace) []types.WorkspacePublic {
	out := make([]types.WorkspacePublic, 0, len(list))
	for _, w := range list {
		if p := Workspace(w); p != nil {
			out = append(out, *p)
		}
	}
	return out
}

func Review(r *domain.DraftReview) *types.DraftReviewPublic {
	if r == nil {
		return nil
	}
	return &types.DraftReviewPublic{
		Id: r.ID, RefType: r.RefType, RefId: r.RefID, WorkspaceId: r.WorkspaceID,
		Status: r.Status, Reason: r.Reason, CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
	}
}

func ReviewList(list []*domain.DraftReview) []types.DraftReviewPublic {
	out := make([]types.DraftReviewPublic, 0, len(list))
	for _, r := range list {
		if p := Review(r); p != nil {
			out = append(out, *p)
		}
	}
	return out
}

func Pagination(page, pageSize int, total int64) types.Pagination {
	if page < 1 {
		page = 1
	}
	if pageSize < 1 {
		pageSize = 20
	}
	tp := int(total) / pageSize
	if int(total)%pageSize != 0 {
		tp++
	}
	return types.Pagination{Page: page, PageSize: pageSize, Total: total, TotalPages: tp}
}
