class CreateFreeIpaRequestValidator implements Validator<CreateFreeIpaRequest> {

    @Override
    public ValidationResult validate(CreateFreeIpaRequest subject) {
        String accountId = crnService.getCurrentAccountId();
        ValidationResultBuilder validationBuilder = ValidationResult.builder();
        if (CollectionUtils.isEmpty(subject.getInstanceGroups())) {
            validationBuilder.error("FreeIPA request must contain at least one instance group.");
        } else {
            int nodesPerInstanceGroup = subject.getInstanceGroups().get(0).getNodeCount();
            if ((nodesPerInstanceGroup > 1 || subject.getInstanceGroups().size() > 1) &&
                    !entitlementService.freeIpaHaEnabled(INTERNAL_ACTOR_CRN, accountId)) {
                validationBuilder.error("The FreeIPA HA capability is disabled.");
            }
            if (subject.getInstanceGroups().stream().filter(ig -> ig.getNodeCount() != nodesPerInstanceGroup || ig.getNodeCount() < 1).count() > 0) {
                validationBuilder.error("All instance groups in the FreeIPA request must contain the same number of nodes per instance group " +
                        "and there must be at least 1 instance per instance group.");
            }
            if (nodesPerInstanceGroup * subject.getInstanceGroups().size() > maxInstances) {
                validationBuilder.error(String.format("FreeIPA request must contain at most %d instances.", maxInstances));
            }
            if (subject.getInstanceGroups().size() > maxInstanceGroups) {
                validationBuilder.error(String.format("FreeIPA request must contain at most %d instance groups.", maxInstanceGroups));
            }
        }

        if (!stackService.findAllByEnvironmentCrnAndAccountId(subject.getEnvironmentCrn(), accountId).isEmpty()) {
            validationBuilder.error("FreeIPA already exists in environment");
        }

        return validationBuilder.build();
    }

}