@startuml
skinparam monochrome true
skinparam packageStyle rectangle
skinparam shadowing false

title Sequence Diagram (SD)

autonumber

actor "Human Resources\nManager" as HMR
participant ":TeamGeneratorUI" as UI
participant ":TeamGeneratorController" as CTRL
participant ":Repositories" as RepositorySingleton
participant "repositories\n:Repositories" as PLAT
participant "skillRepository\n:SkillRepository" as skillRep
participant "collaboratorRepository\n:CollaboratorRepository" as collaboratorRep
participant "teamRepository\n:TeamRepository" as teamRep
participant "collaborator\n:Collaborator" as collaborator
participant "skill\n:Skill" as skill
participant "team\n:Team" as team

activate HMR

    HMR -> UI : ask to generate a team proposal
    activate UI

        UI -> CTRL** : create

        UI --> HMR : request data (i.e. minimum and maximum number of collaborators)
    deactivate UI

    HMR -> UI : types requested data

    activate UI

        UI -> UI : keepData

        UI -> CTRL : getSkills()
        activate CTRL

            CTRL -> RepositorySingleton : getInstance()
            activate RepositorySingleton

                RepositorySingleton --> CTRL : repositories
            deactivate RepositorySingleton

            CTRL -> PLAT : getSkillRepository()
            activate PLAT

                PLAT --> CTRL : skillRepository
            deactivate PLAT

            CTRL -> skillRep : getSkills()
            activate skillRep

                skillRep --> CTRL: skillList
            deactivate skillRep

            CTRL --> UI : skillList
        deactivate CTRL

    loop while intended
        UI --> HMR : shows a set of non-selected skills and ask to choose
        deactivate UI

        HMR -> UI : selects a skill

        activate UI

            UI --> HMR : request the number of collaborators with the skill
        deactivate UI

        HMR -> UI : types the number of collaborators with the skill

        activate UI

            UI -> UI : keepQuantityPerSkillId
    end

    UI -> CTRL : generateTeam(minCollaborators,\nmaxCollaborators, quantityPerSkillId)
    activate CTRL

        CTRL -> CTRL : getCollaboratorsWithoutTeam()

        activate CTRL

            CTRL -> RepositorySingleton : getInstance()
            activate RepositorySingleton

                RepositorySingleton -> CTRL : repositories
            deactivate RepositorySingleton

            CTRL -> PLAT : getTeamRepository()
            activate PLAT

                PLAT --> CTRL : teamRepository
            deactivate PLAT

            CTRL -> PLAT : getCollaboratorRepository()
            activate PLAT

                PLAT --> CTRL : collaboratorRepository
            deactivate PLAT

            CTRL -> teamRep : getTeams()
            activate teamRep

                teamRep --> CTRL : teamList
            deactivate teamRep

            CTRL -> collaboratorRep : getCollaborators()
            activate collaboratorRep

                collaboratorRep --> CTRL : collaboratorList
            deactivate collaboratorRep

            loop for each team
                CTRL -> team : getCollaborators()

                activate team

                    team --> CTRL : collaboratorsInTeam
                deactivate team
            end

            CTRL -> CTRL : collaboratorsWithoutTeam
        deactivate CTRL

        CTRL -> CTRL : filterCollaboratorsWithSkills(collaboratorsWithoutTeam,\nquantityPerSkillId)
        activate CTRL
            loop for each collaborator without a team

                loop for each skill id
                    CTRL -> collaborator : hasSkill(skillId)

                    activate collaborator

                        loop for each skill
                            collaborator -> skill : getId()
                            activate skill

                                skill --> collaborator : id
                            deactivate skill
                        end

                        collaborator --> CTRL : true/false
                    deactivate collaborator
                end

            end
        CTRL --> CTRL : collaboratorsWithSkills
        deactivate CTRL

        CTRL --> UI : teamProposal
    deactivate CTRL

    UI --> HMR : displays the team proposal and requests confirmation
    deactivate UI

    HMR -> UI : confirms team proposal

    activate UI

        UI -> CTRL : createTeam(teamProposal)
        activate CTRL

            CTRL -> RepositorySingleton : getInstance()
            activate RepositorySingleton

                RepositorySingleton --> CTRL : repositories
            deactivate RepositorySingleton

            CTRL -> PLAT : getTeamRepository()
            activate PLAT

                PLAT --> CTRL : teamRepository
            deactivate PLAT

            CTRL -> teamRep : createTeam(teamProposal)
            activate teamRep

                teamRep -> team : team = create(teamProposal)
                activate team
                deactivate team

                teamRep -> teamRep : addTeam(team)
                activate teamRep

                    teamRep -> teamRep : validateTeam(team)
                    activate teamRep

                        teamRep --> teamRep : true/false
                    deactivate teamRep

                    teamRep --> teamRep : true/false
                deactivate teamRep

                teamRep --> CTRL : true/false
            deactivate teamRep

            CTRL --> UI : true/false
        deactivate CTRL

        UI --> HMR : displays operation success
    deactivate UI

@enduml