package copy_mission

import (
	"context"
	"strings"

	scanpostdomain "haixun-backend/internal/model/scan_post/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

type ListCopyMissionScanPostsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListCopyMissionScanPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCopyMissionScanPostsLogic {
	return &ListCopyMissionScanPostsLogic{ctx: ctx, svcCtx: svcCtx}
}

func (l *ListCopyMissionScanPostsLogic) ListCopyMissionScanPosts(
	req *types.ListCopyMissionScanPostsHandlerReq,
) (*types.ListPersonaViralScanPostsData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	personaID := strings.TrimSpace(req.PersonaID)
	missionID := strings.TrimSpace(req.ID)
	if _, err := l.svcCtx.CopyMission.Get(l.ctx, tenantID, uid, personaID, missionID); err != nil {
		return nil, err
	}

	limit := 100
	if req.Limit > 0 {
		limit = req.Limit
	}
	posts, err := l.svcCtx.ScanPost.ListForPersona(l.ctx, scanpostdomain.PersonaListRequest{
		TenantID:      tenantID,
		OwnerUID:      uid,
		PersonaID:     personaID,
		CopyMissionID: missionID,
		Limit:         limit,
	})
	if err != nil {
		return nil, err
	}
	return toViralScanPostsData(posts), nil
}
