@startuml
    title Merge Multiple Line Accounts API
    autonumber "<color:red><b>00 "
    skinparam backgroundColor #FFFFFF
    skinparam sequence {
        TitleFontSize 20
        ActorFontSize 12
        ParticipantFontSize 12
        ParticipantPadding 40
        ArrowFontSize 12
    }
    actor       "        User       " as USR
    box "<size:16>Secure ID/BRASS" #Beige
    participant "   EUI App Profile   " as EUI_PROFILE
    participant "  EUI App Services " as EUI_SERVICES
    participant "  NSE " as BRASS_NSE
    end box
    participant "      IAM    " as IAM


    USR -> EUI_PROFILE:     User selects to merge multiple linked lines into one profile
    EUI_PROFILE -> EUI_SERVICES:          <b>POST</b> /svr/mergeMultipleLineAccounts

    alt Validate the CORS, CSRF & SEC_SESSION Tokens AND if cors validation fails
        EUI_SERVICES -> EUI_PROFILE:     <b> 403 Forbidden</b>
        note right of EUI_PROFILE: { \n     "error": "invalid_request" \n }
        EUI_PROFILE -> USR:         User will be navigated to failure screen.
    end

    alt Validate whether session exists and compares session userId to request userId AND if session validation fails
        EUI_SERVICES -> EUI_PROFILE:     <b> 403 Forbidden</b>
        note right of EUI_PROFILE: { \n     'action': 'authenticate' \n }
        EUI_PROFILE -> USR:         User will be navigated to login screen.
    end

    alt Validate request body AND if no lines to merge
        EUI_SERVICES -> EUI_PROFILE:     <b> 400 Invalid Request</b>
        note right of EUI_PROFILE: { \n   'error_code': 'invalid_request',\n  'error_description': 'Uh-oh, it looks like we have our wires crossed. Please try again later' \n }
        EUI_PROFILE -> USR:        Error message will be displayed to the user.
    end


    loop For every line to be linked
        EUI_SERVICES -> IAM:    POST /oauth2/v1/unlinkmsisdn            - Unlink Msisdn from existing profile
        EUI_SERVICES -> IAM:    POST /oauth2/v1/linkmsisdn              - Link Msisdn to other profile
        EUI_SERVICES -> IAM:    GET /profile_management/v1/user/profile - Retreive TMO Profile
        EUI_SERVICES -> IAM:    PUT /profile_management/v1/user/profile - Update Email in TMO Profile
    end

    EUI_SERVICES -> IAM:    DELETE /profile_management/v1/user/profile - Delete IAM Profile
    EUI_SERVICES -> IAM:    PUT /profile_management/v1/user/profile - Update Email in IAM Profile

    loop For every line to be linked
        EUI_SERVICES -> IAM:   PUT /profile_management/v1/user/profile - Update Email in TMO Profile
    end

    loop For every line to be linked
        EUI_SERVICES -> IAM:    GET /v1/permissions/permission-management - Query account permissions
        EUI_SERVICES -> IAM:    POST /v1/permissions/permission-management - Grant account permissions
    end

    EUI_SERVICES -> EUI_SERVICES: Send SMS Notification to all the MSISDN's after linking
    alt Get all linked lines and send SMS notification to all lines
        EUI_SERVICES -> IAM:     GET /profile_management/v1/user/profile
        note right of EUI_SERVICES: profileType=IAM&userKey=msisdn&keyValue=<<msisdn>>
        IAM -> EUI_SERVICES: IAM Profile Response
        note left of IAM: { \n  'iamUserProfile': {\n        ...,\n        msisdns: "1234567890, 1111111111" \n    } \n }
        loop For every msisdn in profile response
            EUI_SERVICES -> BRASS_NSE:    POST /nse/v1/sendSms
            note right of EUI_SERVICES: notification_process=LINK_MSISDN
        end
    end

    EUI_SERVICES -> EUI_SERVICES: Send SMS Notification to primary account holder (PAH) MSISDN after linking
    alt Get PAH msisdn and Send SMS notification to PAH msisdn
        EUI_SERVICES -> IAM:     GET /profile_management/v1/user/profile
        note right of EUI_SERVICES: profileType=TMO&userKey=msisdn&keyValue=<<msisdn>>
        IAM -> EUI_SERVICES: TMO Profile Response
        note left of IAM:  { \n   'tmoLineProfile': {\n     ..., \n     tmban: "123456789" \n   } \n } 
        EUI_SERVICES -> IAM:     GET /oauth2/v1/querytmoprofilesbyban
        note right of EUI_SERVICES: ban=<<tmban>>
        IAM -> EUI_SERVICES: TMO Profiles Response
        note left of IAM: { \n 'lines': [{\n ...,  \n tmMLCustType: "PAH", \n MSISDN: "1234567890" \n }] \n }
        EUI_SERVICES -> BRASS_NSE:    POST /nse/v1/sendSms
        note right of EUI_SERVICES: notification_process=PAH_NOTIFICATION
    end
@enduml