package permissions

import (
	"context"

	"github.com/nexuscrm/backend/pkg/errors"
)

func (s *PermissionService) GetEffectivePermissionsForUser(ctx context.Context, userID string) (*EffectivePermissions, error) {
	user, err := s.userRepo.GetUserByID(ctx, userID)
	if err != nil {
		return nil, errors.NewInternalError("failed to get user", err)
	}
	if user == nil {
		return nil, errors.NewNotFoundError("user", userID)
	}

	profileID := user.ProfileID

	systemPerms, err := s.GetEffectiveSystemPermissions(ctx, userID, profileID)
	if err != nil {
		return nil, errors.NewInternalError("failed to get system permissions", err)
	}

	customPerms, err := s.GetEffectiveCustomPermissions(ctx, userID, profileID)
	if err != nil {
		return nil, errors.NewInternalError("failed to get custom permissions", err)
	}

	return &EffectivePermissions{
		UserID:            userID,
		ProfileID:         profileID,
		SystemPermissions: systemPerms,
		CustomPermissions: customPerms,
	}, nil
}
