package workspaces

import (
	"context"
	"fmt"

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

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

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

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

func (l *ArchiveWorkspaceLogic) ArchiveWorkspace(req *types.WorkspaceIdReq) (*types.OkData, error) {
	uid, ok := middleware.UIDFrom(l.ctx)
	if !ok || uid <= 0 {
		return nil, fmt.Errorf("unauthorized")
	}
	if err := l.svcCtx.Growth.ArchiveWorkspace(l.ctx, uid, req.Id); err != nil {
		return nil, err
	}
	return &types.OkData{Ok: true}, nil
}
