package playbooks

import (
	"context"

	"apps/backend/internal/middleware"
	"apps/backend/internal/module/growth/usecase"
	"apps/backend/internal/svc"
	"apps/backend/internal/types"

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

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

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

func (l *GetPlaybookLogic) GetPlaybook(req *types.PlaybookIdPath) (*types.PlaybookPublic, error) {
	uid, _ := middleware.UIDFrom(l.ctx)
	p, err := l.svcCtx.Growth.GetPlaybook(l.ctx, req.Id)
	if err != nil {
		return nil, err
	}
	return &types.PlaybookPublic{
		Id: p.ID, Kind: p.Kind, Title: p.Title, Niche: p.Niche, Body: p.Body,
		Anonymous: p.Anonymous, AuthorLabel: usecase.PlaybookAuthorLabel(p, uid),
		ImportCount: p.ImportCount, CreatedAt: p.CreatedAt, Mine: p.OwnerUID == uid,
	}, nil
}
