package playbooks

import (
	"context"
	"fmt"

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

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

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

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

func (l *ImportPlaybookLogic) ImportPlaybook(req *types.PlaybookIdPath) (*types.ImportPlaybookData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	p, err := l.svcCtx.Growth.ImportPlaybook(l.ctx, uid, req.Id)
	if err != nil {
		return nil, err
	}
	return &types.ImportPlaybookData{Id: p.ID, Copied: true, Message: "已複製到我的 playbook"}, nil
}
