// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package brand

import (
	"context"
	"strings"

	app "haixun-backend/internal/library/errors"
	"haixun-backend/internal/library/errors/code"
	scanpostentity "haixun-backend/internal/model/scan_post/domain/entity"
	scanpostusecase "haixun-backend/internal/model/scan_post/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PatchScanPostOutreachLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPatchScanPostOutreachLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PatchScanPostOutreachLogic {
	return &PatchScanPostOutreachLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PatchScanPostOutreachLogic) PatchScanPostOutreach(req *types.PatchScanPostOutreachHandlerReq) (resp *types.ScanPostData, err error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	status := ""
	if req.PatchScanPostOutreachReq.OutreachStatus != nil {
		status = strings.TrimSpace(*req.PatchScanPostOutreachReq.OutreachStatus)
	}
	if status == "" {
		return nil, app.For(code.Brand).InputMissingRequired("outreach_status is required")
	}
	switch status {
	case scanpostentity.OutreachStatusHandled, scanpostentity.OutreachStatusSkipped, scanpostentity.OutreachStatusPending:
	default:
		return nil, app.For(code.Brand).InputInvalidFormat("outreach_status must be handled, skipped, or pending")
	}
	if _, err := l.svcCtx.Brand.Get(l.ctx, tenantID, uid, req.BrandPath.ID); err != nil {
		return nil, err
	}
	updated, err := l.svcCtx.ScanPost.UpdateOutreach(l.ctx, scanpostusecase.UpdateOutreachRequest{
		TenantID: tenantID,
		OwnerUID: uid,
		BrandID:  req.BrandPath.ID,
		PostID:   req.PostID,
		Status:   status,
	})
	if err != nil {
		return nil, err
	}
	return toScanPostData(updated), nil
}

func toScanPostData(post *scanpostusecase.ScanPostSummary) *types.ScanPostData {
	if post == nil {
		return nil
	}
	return &types.ScanPostData{
		ID:                 post.ID,
		GraphNodeID:        post.GraphNodeID,
		SearchTag:          post.SearchTag,
		QueryDimension:     post.QueryDimension,
		ExternalID:         post.ExternalID,
		Permalink:          post.Permalink,
		Author:             post.Author,
		Text:               post.Text,
		Priority:           post.Priority,
		PlacementScore:     post.PlacementScore,
		ProductFitScore:    post.ProductFitScore,
		SolvedByProduct:    post.SolvedByProduct,
		Source:             post.Source,
		ScanJobID:          post.ScanJobID,
		OutreachStatus:     post.OutreachStatus,
		PublishedReplyID:   post.PublishedReplyID,
		PublishedPermalink: post.PublishedPermalink,
		OutreachUpdateAt:   post.OutreachUpdateAt,
		PostedAt:           post.PostedAt,
		Replies:            toScanReplyData(post.Replies),
		CreateAt:           post.CreateAt,
	}
}

func toScanReplyData(replies []scanpostusecase.ScanReplySummary) []types.ScanReplyData {
	if len(replies) == 0 {
		return nil
	}
	out := make([]types.ScanReplyData, 0, len(replies))
	for _, reply := range replies {
		out = append(out, types.ScanReplyData{
			ExternalID: reply.ExternalID,
			Author:     reply.Author,
			Text:       reply.Text,
			Permalink:  reply.Permalink,
			LikeCount:  reply.LikeCount,
			PostedAt:   reply.PostedAt,
		})
	}
	return out
}
