package compose

import (
	"context"

	"apps/backend/internal/middleware"
	"apps/backend/internal/response"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *ComposePersonaPreviewLogic) ComposePersonaPreview(req *types.ComposePersonaPreviewReq) (*types.ComposePersonaPreviewData, error) {
	if l.svcCtx.Studio == nil {
		return nil, response.Biz(503, 503001, "studio not configured")
	}
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok {
		return nil, response.Biz(401, 401001, "missing authorization")
	}
	// UseNews：API 可選；FE 預設 true。若 JSON 省略 bool 為 false，FE 會明確傳 true。
	useNews := req.UseNews
	out, err := l.svcCtx.Studio.PersonaPreview(l.ctx, uid, req.PersonaId, req.Topic, useNews)
	if err != nil {
		return nil, err
	}
	return &types.ComposePersonaPreviewData{
		Topic:       out.Topic,
		TopicSource: out.TopicSource,
		PostText:    out.PostText,
		ReplyText:   out.ReplyText,
		Notes:       out.Notes,
	}, nil
}
