Remove-CDBDocument.psm1

$FunctionScriptName = "Remove-CDBDocument"
Write-Verbose "Import-Start| [$($FunctionScriptName)]"

#* Dependencies
# New-CDBAuthHeader

#todo: ADD SQL query feature
#todo: pagination loop
#todo: Add error handling
#todo: Add socket throttling check


function Remove-CDBDocument {
    <#
        .SYNOPSIS
            Removes a Document from the Database.
         
        .DESCRIPTION
            Removes a Document from the Database.
            Used by SQL/Core REST API
         
        .PARAMETER Config
            Config array
            Accepts the following values: DatabaseID / CollectionID / AuthKey / keyType / CosmosDBEndpoint / KeyType
            All required values can be set directly. Directly set values have precedent.
            Not all values may be required.
         
        .PARAMETER DatabaseID
            Can be set via -Config value
            Name of the CosmosDB Database
         
        .PARAMETER CollectionId
            Can be set via -Config value
            Name of the CosmosDB Collection
         
        .PARAMETER AuthKey
            Can be set via -Config value
            Key value to authenticate against cosmosDB
 
        .PARAMETER KeyType
            Can be set via -Config value
            Type of AuthKey provided.
            Default value: "master"
             
        .PARAMETER CosmosDBEndpoint
            URL of the CosmosDB
             
        .PARAMETER PartitionKey
            Value of the Partition. Not the Name of the Key itself!
         
        .PARAMETER DocumentID
            ID of the document
            Value of the document value "id"
         
        .EXAMPLE
            Remove-CDBDocument -Config $Config -PartitionKey $PKEY -DocumentID $DocumentID
         
        .NOTES
            AUTHOR: Ken Dobrunz // Ken.Dobrunz@Skaylink.com
            LASTEDIT: 05.08.2022 - Version: 1.0
        #>

    [cmdletbinding()]
    Param(
        #* Active data
        # Config or direct values needed
        [Parameter()]$Config,
        [Parameter()][string]$DatabaseID,
        [Parameter()][Alias('col')][string]$CollectionId,
        [Parameter()][Alias('key')][string]$AuthKey,
        [Parameter()][Alias('type')][string]$KeyType = "master",
        [Parameter()][Alias('uri')][string]$CosmosDBEndpoint,
        
        # Entity
        [parameter()][Alias('pkey')][string]$PartitionKey,
        [Parameter()][Alias('doc')][string]$DocumentID
    )
    Begin {
        $SelfIdentifier = "Remove-CDBDocument"
        
        # Check Config / Set Variables
        $DatabaseID = if ($DatabaseID) { $DatabaseID }elseif ($Config.DatabaseID) { $Config.DatabaseID }else { Write-Error "[$($SelfIdentifier)] No DatabaseID provided" }
        $CollectionId = if ($CollectionId) { $CollectionId }elseif ($Config.CollectionId) { $Config.CollectionId }else { Write-Error "[$($SelfIdentifier)] No CollectionId provided" }
        $AuthKey = if ($AuthKey) { $AuthKey }elseif ($Config.AuthKey) { $Config.AuthKey }else { Write-Error "[$($SelfIdentifier)] No AuthKey provided" }
        $KeyType = if ($KeyType) { $KeyType }elseif ($Config.KeyType) { $Config.KeyType }else { Write-Error "[$($SelfIdentifier)] No KeyType provided" }
        $CosmosDBEndpoint = if ($CosmosDBEndpoint) { $CosmosDBEndpoint }elseif ($Config.CosmosDBEndpoint) { $Config.CosmosDBEndpoint }else { Write-Error "[$($SelfIdentifier)] No CosmosDBEndpoint provided" }
        
        $CosmosURI = "$CosmosDBEndPoint/dbs/$DatabaseId/colls/$CollectionId/docs/"

        if (!$PartitionKey) { throw "[$($SelfIdentifier)] No PartitionKey [$($PartitionKey)]" }
        if (!$DocumentID) { throw "[$($SelfIdentifier)] No DocumentID [$($DocumentID)]" }
        
        $functionverbosecount = 0
    }
    Process { 
        Write-Verbose "[$($SelfIdentifier)] DELETE id [$($DocumentID)] in [$($CollectionId)]"
        
        # Auth Header
        $header = New-CDBAuthHeader -DatabaseID $DatabaseID -CollectionID $CollectionId -AuthKey $AuthKey -keytype $KeyType -HTTPverb "DELETE" -PartitionKey $PartitionKey -DocumentID $DocumentID
        
        Try {
            Invoke-RestMethod -Method DELETE -Uri ($CosmosURI + $DocumentID) -Headers $header -ContentType application/json
        } catch {
            $catch = ($_.Exception).Response.StatusCode
            Write-Output "[$($SelfIdentifier)] Document [$($DocumentID)] @ [$($PartitionKey)] ERROR: [$($catch)] "
            Write-Verbose "[$($SelfIdentifier)] Document [$($DocumentID)] @ [$($PartitionKey)] ERROR: [$($catch)] "
        }

        $functionverbosecount++
    }
    End {
        if ($functionverbosecount -gt 1) { 
            Write-Verbose "[$($SelfIdentifier)] Deleted $($functionverbosecount) entries"
        }
    }
} #v0.1

Export-ModuleMember -Function *
Write-Verbose "Import-END| [$($FunctionScriptName)]"