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

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import uk.ac.ebi.subs.data.component.Team;
import uk.ac.ebi.tsc.aap.client.model.Domain;
import uk.ac.ebi.tsc.aap.client.model.User;
import uk.ac.ebi.tsc.aap.client.repo.DomainService;
import uk.ac.ebi.tsc.aap.client.repo.ProfileService;
import uk.ac.ebi.tsc.aap.client.repo.TokenService;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TeamCreationService {

    @NonNull
    private TeamNameSequenceService teamNameSequenceService;

    @NonNull
    private UsiTokenService usiTokenService;

    @NonNull
    private DomainService domainService;

    @NonNull
    private ProfileService profileService;

    @NonNull
    private TokenService tokenService;

    public Team createTeam(User user, TeamDto teamDto){
        String description = teamDto.getDescription();
        if (description == null) {
            description = "";
        }

        String teamName = teamNameSequenceService.nextTeamName();

        Domain domain = domainService.createDomain(teamName, description, usiTokenService.aapToken());
        domainService.addUserToDomain(domain, user, usiTokenService.aapToken());
        domainService.addManagerToDomain(domain,user,usiTokenService.aapToken());


        Map<String,String> domainProfileAttributes = new HashMap<>();
        domainProfileAttributes.put("centre name",teamDto.getCentreName());

        profileService.createDomainProfile(
           domain.getDomainReference(),
                domainProfileAttributes,
                usiTokenService.aapToken()
        );


        //remove USI as domain manager
        String usiUserRef = tokenService.getUserReference(usiTokenService.aapToken());
        User usiUser = new User.Builder(usiUserRef).build();

        domainService.removeManagerFromDomain(usiUser,domain,usiTokenService.aapToken());


        Team team = new Team();
        team.setName(domain.getDomainName());

        return team;
    }
}

@@ -312,32 +312,92 @@
 .DomainService;%0A
+import uk.ac.ebi.tsc.aap.client.repo.ProfileRepositoryRest;%0A
 import uk.ac.ebi
@@ -827,49 +827,230 @@
 e;%0A%0A
-    @NonNull%0A    private ProfileService p
+%0A    //private ProfileService profileService; TODO - use profile service directly once AAP adds it to auto-config%0A%0A    @NonNull%0A    private ProfileRepositoryRest profileRepositoryRest; //TODO remove this once AAP exposes P
 rofi
@@ -1050,33 +1050,32 @@
 s ProfileService
-;
 %0A%0A    @NonNull%0A 
@@ -1169,16 +1169,101 @@
 amDto)%7B%0A
+        ProfileService profileService = new ProfileService(profileRepositoryRest);%0A%0A%0A
         

tidy up comments about workaround