package permission

import (
	"context"

	"haixun-backend/internal/logic/authz"
	domusecase "haixun-backend/internal/model/permission/domain/usecase"
	"haixun-backend/internal/svc"
	"haixun-backend/internal/types"
)

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

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

func (l *GetPermissionCatalogLogic) GetPermissionCatalog(req *types.PermissionCatalogQuery) (*types.PermissionCatalogData, error) {
	if err := authz.RequireAdmin(l.ctx, l.svcCtx); err != nil {
		return nil, err
	}
	tree, list, err := l.svcCtx.Permission.Catalog(l.ctx, domusecase.CatalogRequest{
		Status: req.Status,
		Type:   req.Type,
		Tree:   req.Tree,
	})
	if err != nil {
		return nil, err
	}
	return &types.PermissionCatalogData{
		Tree: toPermissionNodes(tree),
		List: toPermissionNodes(list),
	}, nil
}
