package copy_mission

import (
	"context"
	"strings"

	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

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

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

func (l *ListCopyMissionCopyDraftsLogic) ListCopyMissionCopyDrafts(req *types.CopyMissionPath) (*types.ListCopyMissionCopyDraftsData, 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
	}

	drafts, err := l.svcCtx.CopyDraft.ListByMission(l.ctx, tenantID, uid, personaID, missionID, 50)
	if err != nil {
		return nil, err
	}
	list := make([]types.CopyDraftData, 0, len(drafts))
	for _, item := range drafts {
		list = append(list, toCopyDraftData(item))
	}
	return &types.ListCopyMissionCopyDraftsData{List: list, Total: len(list)}, nil
}
