# API Configuration
$apiUrl = "https://localhost/action/login"

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Headers
$headers = @{
    "Content-Type" = "application/json"
}

# Get credentials
$username = Read-Host "Enter username"
$password = Read-Host "Enter password" -AsSecureString
$plainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))

# Prepare body
$body = @{
    username = $username
    password = $plainPassword
} | ConvertTo-Json

# Send request
try {
    $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body
    Write-Host "`nAuthentication successful!"
    if ($response.isAdmin) {
        Write-Host "Admin access granted"
        Write-Host "API Token: $($response.token)"
    } else {
        Write-Host "Regular user access granted"
    }
} catch {
    Write-Host "`nAuthentication failed. Details:"
    Write-Host $_.Exception.Message
    if ($_.Exception.Response) {
        Write-Host "Status Code:" $_.Exception.Response.StatusCode.value__
    }
}
