package uk.ac.ebi.subs.api.validators;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import uk.ac.ebi.subs.data.component.Team;

@Component
public class TeamValidator implements Validator {

    @Value("${usi.teamName.prefix}")
    @Getter
    private String expectedTeamNamePrefix;


    @Override
    public boolean supports(Class<?> clazz) {
        return Team.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Team team = (Team) target;
        SubsApiErrors.rejectIfEmptyOrWhitespace(errors,"name");

        if (team.getName() == null || team.getName().isEmpty()){
            return;
        }

        if (!team.getName().startsWith(expectedTeamNamePrefix)){
            SubsApiErrors.not_a_subs_team.addError(errors,"name");
        }
    }
}

@@ -457,16 +457,108 @@
 refix;%0A%0A
+    private static final String%5B%5D expectedProfileAttributes = new String%5B%5D%7B%22centre name%22%7D;%0A%0A
 %0A    @Ov
@@ -821,32 +821,33 @@
 itespace(errors,
+ 
 %22name%22);%0A%0A      
@@ -903,16 +903,17 @@
 Empty())
+ 
 %7B%0A      
@@ -1000,16 +1000,17 @@
 Prefix))
+ 
 %7B%0A      
@@ -1061,16 +1061,17 @@
 (errors,
+ 
 %22name%22);
@@ -1084,13 +1084,499 @@
    %7D
+%0A%0A        for (String expectedProfileAttributeKey : expectedProfileAttributes) %7B%0A            if (%0A                    !team.getProfile().containsKey(expectedProfileAttributeKey) %7C%7C%0A                            team.getProfile().get(expectedProfileAttributeKey).isEmpty()) %7B%0A                SubsApiErrors.missing_profile_attribute.addError(%0A                        errors,%0A                        %22profile%5B'%22 + expectedProfileAttributeKey + %22'%5D%22%0A                );%0A            %7D%0A        %7D
 %0A    %7D%0A%7D
 %0A
@@ -1574,9 +1574,8 @@
 %0A    %7D%0A%7D
-%0A

validate that the team has a centre name