package persona

import (
	"context"
	"strings"

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

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

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

func (l *ListPersonaCopyDraftsLogic) ListPersonaCopyDrafts(req *types.PersonaPath) (*types.ListPersonaCopyDraftsData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	personaID := strings.TrimSpace(req.ID)
	if _, err := l.svcCtx.Persona.Get(l.ctx, tenantID, uid, personaID); err != nil {
		return nil, err
	}

	drafts, err := l.svcCtx.CopyDraft.List(l.ctx, tenantID, uid, personaID, 50)
	if err != nil {
		return nil, err
	}
	list := make([]types.CopyDraftData, 0, len(drafts))
	for _, item := range drafts {
		list = append(list, types.CopyDraftData{
			ID:             item.ID,
			PersonaID:      item.PersonaID,
			CopyMissionID:  item.CopyMissionID,
			ScanPostID:     item.ScanPostID,
			DraftType:      item.DraftType,
			SortOrder:      item.SortOrder,
			Text:           item.Text,
			Angle:          item.Angle,
			Hook:           item.Hook,
			Rationale:      item.Rationale,
			ReferenceNotes: item.ReferenceNotes,
			Sources:        item.Sources,
			Status:         item.Status,
			CreateAt:       item.CreateAt,
		})
	}
	return &types.ListPersonaCopyDraftsData{List: list, Total: len(list)}, nil
}
