param(
    [string]$BaseUrl = 'http://localhost:5099'
)

$ErrorActionPreference = 'Stop'

$stamp = Get-Date -Format 'yyyyMMddHHmmss'
$phoneA = "+7999$stamp"
$phoneB = "+7888$stamp"

function Invoke-JsonPost {
    param(
        [string]$Uri,
        [hashtable]$Body,
        [hashtable]$Headers = @{}
    )

    return Invoke-RestMethod `
        -Uri $Uri `
        -Method Post `
        -Headers $Headers `
        -ContentType 'application/json' `
        -Body ($Body | ConvertTo-Json)
}

$challengeA = Invoke-JsonPost `
    -Uri "$BaseUrl/api/auth/request-code" `
    -Body @{ phoneNumber = $phoneA }

$sessionA = Invoke-JsonPost `
    -Uri "$BaseUrl/api/auth/verify-code" `
    -Body @{
        challengeId = $challengeA.challengeId
        phoneNumber = $phoneA
        code = $challengeA.testCode
        displayName = 'Phone Smoke A'
    }

$challengeB = Invoke-JsonPost `
    -Uri "$BaseUrl/api/auth/request-code" `
    -Body @{ phoneNumber = $phoneB }

$sessionB = Invoke-JsonPost `
    -Uri "$BaseUrl/api/auth/verify-code" `
    -Body @{
        challengeId = $challengeB.challengeId
        phoneNumber = $phoneB
        code = $challengeB.testCode
        displayName = 'Phone Smoke B'
    }

$headersA = @{
    Authorization = "Bearer $($sessionA.accessToken)"
}

$meA = Invoke-RestMethod `
    -Uri "$BaseUrl/api/users/me" `
    -Method Get `
    -Headers $headersA

$resolve = Invoke-JsonPost `
    -Uri "$BaseUrl/api/users/contacts/resolve" `
    -Headers $headersA `
    -Body @{
        phoneNumbers = @(
            $phoneB,
            '+7 (777) 000-00-00'
        )
    }

if ($null -eq $resolve) {
    $resolve = @()
}
else {
    $resolve = @($resolve)
}

$repeatChallengeA = Invoke-JsonPost `
    -Uri "$BaseUrl/api/auth/request-code" `
    -Body @{ phoneNumber = $phoneA }

$repeatSessionA = Invoke-JsonPost `
    -Uri "$BaseUrl/api/auth/verify-code" `
    -Body @{
        challengeId = $repeatChallengeA.challengeId
        phoneNumber = $phoneA
        code = $repeatChallengeA.testCode
        displayName = $null
    }

$chat = Invoke-JsonPost `
    -Uri "$BaseUrl/api/chats/direct" `
    -Headers $headersA `
    -Body @{
        userId = $sessionB.user.id
    }

$chats = Invoke-RestMethod `
    -Uri "$BaseUrl/api/chats" `
    -Method Get `
    -Headers $headersA

if ($null -eq $chats) {
    $chats = @()
}
else {
    $chats = @($chats)
}

[pscustomobject]@{
    PhoneARegistered      = -not $challengeA.isRegistered
    PhoneBRegistered      = -not $challengeB.isRegistered
    PhoneAMeDisplayName   = $meA.displayName
    PhoneAMePhone         = $meA.phoneNumber
    ResolvedCount         = $resolve.Count
    ResolvedPhone         = if ($resolve.Count -gt 0) { $resolve[0].phoneNumber } else { $null }
    ResolvedDisplayName   = if ($resolve.Count -gt 0) { $resolve[0].user.displayName } else { $null }
    ReLoginRegistered     = $repeatChallengeA.isRegistered
    ReLoginSameUser       = $repeatSessionA.user.id -eq $sessionA.user.id
    DirectChatType        = $chat.type
    DirectChatCreated     = $chat.type -eq 1 -or $chat.type -eq 'Direct'
    ChatsCount            = $chats.Count
}
