$envCountry = 'CH'
#import Country settings
. .\..\.env\Import-EnvironmentSettings.ps1 -envCountry $envCountry
# This is minimal dane witch can be inserted to database
$minDatedte = Get-Date "1777/01/01"

$svcPassword = ConvertTo-SecureString -String $ADPassword -AsPlainText -Force
$ADCredential = New-Object System.Management.Automation.PSCredential("$ActiveDirectoryDomain\$ADAccount", $svcPassword)

Write-host 'Processing country ... '  $country

foreach($server in $servers){
    
    $serverName = $server[0]
    
    $serverEnv = $server[3]
    $instanceIp = $server[1]
    # jeśli jest na jednym IP wiele instancji należy zbudować odpowiedni connection string i dopiero potem można się podłączyć do bazy
    if($server[4]) {
        $instance = $server[1] + '\' + $server[4]
    } else {
        $instance = $server[1]
    }

    $tmpVerInstance = test-DbaBuild -SqlInstance $instance -Latest  -SqlCredential $ADCredential -update
    $System_name = (Get-WmiObject -comp $instanceIp -Credential $ADCredential -class Win32_OperatingSystem ).caption #Version # #caption
    $System_Version = (Get-WmiObject -comp $instanceIp -Credential $ADCredential -class Win32_OperatingSystem ).Version

    # $IP_Address = $HostNames[$i]
    # $ServerName = 'Server ' + $serverName
    $Instance_Name = $tmpVerInstance.SqlInstance
    Write-host '=> Processing server ... '  $serverName
    $dba_info = Invoke-DbaQuery -SqlInstance $instanceIp  -Query "EXEC sp_server_info" -SqlCredential $ADCredential
    $Database_Type =  $dba_info[0].attribute_value

    $Build = $tmpVerInstance.Build
    $Server_Name =   (Get-WmiObject -comp  $instanceIp -Credential $ADCredential -class Win32_OperatingSystem ).CSName
    $Build = $tmpVerInstance.Build
    $KBLevel = $tmpVerInstance.KBLevel
    $Compliant = $tmpVerInstance.Compliant
    $SPLevel = $tmpVerInstance.SPLevel
    $SupportedUntil = $tmpVerInstance.SupportedUntil

    $instanceid = Invoke-DbaQuery -SqlInstance $dbadminServer -Database "DBAdmin" -Query "EXEC dbaInstance '$Instance_Name', '$country', '$System_name', '$System_Version', '$instanceIp', '$Database_Type', '$Build', '$KBLevel', '$Compliant', '$SPLevel', '$SupportedUntil', '$serverEnv', '$serverName'" -SqlCredential $DBACredential 
    
    $InsID = $instanceid['InstanceID']
    $DatabaseInfo = Get-DbaDatabase -SqlInstance $instance -SqlCredential $ADCredential
    
    foreach($db in $DatabaseInfo){
        if ($db.LastFullBackup -lt $minDatedte) {
            $LastFullBackup = $null
        }
        else {
            $LastFullBackup = $db.LastFullBackup
        }
        if ($db.LastDiffBackup -lt $minDatedte ) {
            $LastDiffBackup = $null
        }
        else {
            $LastDiffBackup = $db.LastDiffBackup
        }
        if ($db.LastLogBackup -lt $minDatedte) {
            $LastLogBackup = $null
        }  
        else {
            $LastLogBackup = $db.LastLogBackup
        } 
        $sqlParams = @{
            _instanceID = $InsID
            _DatabaseName = $db.Name
            _DatabaseStatus = $db.Status
			_IsAccessible = $db.IsAccessible
			_RecoveryModel = $db.RecoveryModel
			_LogReuseWaitStatus = $db.LogReuseWaitStatus
			_Compatibility = $db.Compatibility
			_Collation = $db.Collation
			_DBOwner = $db.Owner
			_IsEncrypted = $db.Encrypted
			_LastFullBackup =  $LastFullBackup 
			_LastDiffBackup  = $LastDiffBackup
			_LastLogBackup = $LastLogBackup
        }

		Write-host '==> Processing database ... '  $db.Name
        Invoke-DbaQuery -SqlInstance $dbadminServer -Database "DBAdmin"  -Query 'exec dbaDatabase @_instanceID, @_DatabaseName, @_DatabaseStatus, @_IsAccessible, @_RecoveryModel, @_LogReuseWaitStatus, @_Compatibility, @_Collation, @_DBOwner, @_IsEncrypted, @_LastFullBackup, @_LastDiffBackup, @_LastLogBackup ' -SqlParameter $sqlParams -SqlCredential $DBACredential 
    }
}

