function Start-PPTSolutionDeploy {
    [CmdLetBinding()]
    Param (
        [ValidateSet('DEV', 'STAGING', 'TESTING')]
        [string]$Environment,
        $Path = (Get-Location).Path
    )

    begin {

        $Path = @{
            qualifier    = (Split-Path -Path $Path -Qualifier)
            parent       = (Split-Path -Path $Path)
            leaf         = (Split-Path -Path $Path -Leaf)
            path         = $Path
            pathBuild    = (Join-Path -Path $Path -ChildPath 'Build')
            pathConfig   = (Join-Path -Path $Path -ChildPath 'Build\config.json')
            sourcePath   = (Join-Path -Path $PSScriptRoot -ChildPath 'source')
            sourceConfig = (Join-Path -Path $PSScriptRoot -ChildPath 'source\config.ps1')
            sourceIgnore = (Join-Path -Path $PSScriptRoot -ChildPath 'source\.gitignore')
        }

        $config = (Get-Content $Path.pathConfig | ConvertFrom-Json)
        $env    = ($config.env | ?{$_.name -eq $Environment})

        if (!$env) {
            do {
                #Write-Host "┌───────────────────────────────────────────────────────────────┐" -ForegroundColor Green
                #Write-Host "│" -ForegroundColor Green -NoNewLine; Write-Host "`t`t`tCONFIGURED ENVIRONMENTS`t`t`t" -ForegroundColor Cyan -NoNewline; Write-Host "│" -ForegroundColor Green
                #Write-Host "└───────────────────────────────────────────────────────────────┘" -ForegroundColor Green
                $config.env | %{"[ {0} ] {1}" -f $_.id,$_.name}
                $environmentId = Read-Host -Prompt "`nSelect Environment ($((1..$config.env.Count) -join ','))"
            } while ($environmentId -notin (1..$config.env.Count))
            $env = ($config.env | ?{$_.id -eq $environmentId})
        }

        If((Get-ChildItem -Path $Path.path -Name *.zip).Count -ge 1) {
            $index = 1
            $solutions = Get-ChildItem -Path $Path.path -Name *.zip | Get-Item | ForEach-Object {
                @{
                    $index++ = @{
                        id = $index - 1
                        name = "$($_.Name)"
                        fullname = $_.FullName
                        directory = $_.Directory
                        basename = $_.BaseName
                    }
                }
            }
            do {
                #Write-Host "┌───────────────────────────────────────────────────────────────┐" -ForegroundColor Green
                #Write-Host "│" -ForegroundColor Green -NoNewLine; Write-Host "`t`tPACKAGES AVAILABLE IN CURRENT PROJECT`t`t" -ForegroundColor Cyan -NoNewline; Write-Host "│" -ForegroundColor Green
                #Write-Host "└───────────────────────────────────────────────────────────────┘" -ForegroundColor Green
                $solutions | %{"[ {0} ] {1}" -f $_.Values.id,$_.Values.basename}
                $solutionsId = Read-Host -Prompt "`nSelect Solution Package ($((1..$solutions.Values.id.Count) -join ','))"
            } while ($solutionsId -notin (1..$solutions.Values.id.Count))
            $solutionSelectd = ($solutions | ?{$_.Values.id -eq $solutionsId})

            $Path.leaf = $solutionSelectd.Values.basename
        }

    }

    process {

        if(!(Test-Path "$($Path.path)\$($env.name).$($env.env_id).$($Path.leaf).json")) {

            Write-Warning -Message "Missing settings file for $($env)"
            return

        } else {

            Write-Host "[ Info ] " -ForegroundColor Green -nonewline
            Write-Host "Using settings for environment: $($env)" -ForegroundColor Cyan

        }

        <#if (!(Test-Path "$($Path.path)\DEV*.json") -or !(Test-Path "$($Path.path)\STAGING*.json") -or !(Test-Path "$($Path.path)\TESTING*.json")) {

            Write-Warning -Message "Missing settings file for environment(s)"
            return
        }#>

        $pacAuthResponse = & $Global:pptConfig.pacPath auth list

        switch ($pacAuthResponse) {

            {$_ -match 'No profiles were found on this computer'} {
                Write-Host "[ Error ] " -ForegroundColor Yellow -nonewline
                Write-Host "No profiles were found on this computer" -ForegroundColor Yellow
                $authResponse = auth
				if ($authResponse) {
                    $IsAuth = $true
                    & $Global:pptConfig.pacPath org select -env $env.env_id
                } else {
                    $IsAuth = $false
                }

            }

            {$_ -match '\*'} {
                Write-Host "[ Info ] " -ForegroundColor Green -nonewline
                write-host "Authentication profile found" -ForegroundColor Cyan
                $IsAuth = $true
                & $Global:pptConfig.pacPath org select -env $env.env_id}

            #Default {Write-Warning -Message "Unhandled exception"; return}

        }

        if ((& $Global:pptConfig.pacPath org list | ?{$_ -match '\*'}) -match $env.env_id -and $IsAuth) {

            Write-Host "[ Info ] " -ForegroundColor Green -nonewline; write-host "Building solution package" -ForegroundColor Cyan
            & $Global:pptConfig.pacPath solution import --path "$($Path.path)\$($Path.leaf).zip" --settings-file "$($env.name).$($env.env_id).$($Path.leaf).json" --activate-plugins --publish-changes

        } else {

            Write-Host '[ Error ] ' -ForegroundColor Yellow -nonewline; write-host 'Fatal exception' -ForegroundColor Red
            return

        }

    }

    end {

    }

}

# EXPORT ALIAS
New-Alias -Name deploySolution -Value Start-PPTSolutionDeploy -Description 'posh-PowerPlatformToolkit - Start-PPTSolutionDeploy alias' -Force
