package workspaces

import (
	"context"
	"fmt"

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

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

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

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

func (l *UpdateWorkspaceBrandingLogic) UpdateWorkspaceBranding(req *types.WorkspaceBrandingReq) (*types.WorkspacePublic, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	hide := req.HidePoweredBy
	w, err := l.svcCtx.Growth.UpdateWorkspaceBranding(l.ctx, uid, req.Id, req.FooterText, req.BrandName, &hide)
	if err != nil {
		return nil, err
	}
	return growthmap.Workspace(w), nil
}
