package threads_account

import (
	"context"

	cfdom "haixun-backend/internal/model/content_formula/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

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

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

func (l *PatchContentFormulaLogic) PatchContentFormula(req *types.PatchContentFormulaHandlerReq) (*types.ContentFormulaData, error) {
	tenantID, uid, err := actorFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	item, err := l.svcCtx.ContentFormula.Update(l.ctx, cfdom.UpdateRequest{
		TenantID:     tenantID,
		OwnerUID:     uid,
		AccountID:    req.ID,
		FormulaID:    req.FormulaID,
		Label:        req.Label,
		Formula:      req.Formula,
		PostTemplate: req.PostTemplate,
		HookPattern:  req.HookPattern,
		Structure:    req.Structure,
		Tags:         req.Tags,
	})
	if err != nil {
		return nil, err
	}
	data := toContentFormulaData(*item)
	return &data, nil
}
