<#
===============================================================================
Script Name : PostDeployCleanup.ps1

Purpose:
    Reads embedded-adapter-config.json after deployment.
    If module "PCJ" is enabled, deletes specific XML files.

    Service root is dynamically derived as:
        2 levels above the script location

===============================================================================
#>

$ErrorActionPreference = "Stop"

try {

    # --------------------------------------------------------------------------
    # 1. Resolve Service Root (2 levels above script location)
    # --------------------------------------------------------------------------
    # $PSScriptRoot = folder where this script resides

    $serviceRoot = Split-Path -Path $PSScriptRoot -Parent
    $serviceRoot = Split-Path -Path $serviceRoot -Parent

    Write-Host "Script Path    : $PSScriptRoot"
    Write-Host "Service Root   : $serviceRoot"

    # --------------------------------------------------------------------------
    # 2. Locate JSON Config
    # --------------------------------------------------------------------------
    $jsonPath = Join-Path $serviceRoot "embedded-adapter-config.json"

    if (-not (Test-Path $jsonPath)) {
        Write-Host "Config file not found: $jsonPath"
        exit 0
    }

    Write-Host "Reading config file..."

    # --------------------------------------------------------------------------
    # 3. Read JSON
    # --------------------------------------------------------------------------
    
$jsonRaw = Get-Content $jsonPath -Raw -Encoding UTF8

# Remove non-standard whitespace characters
$jsonClean = $jsonRaw -replace '[^\x09\x0A\x0D\x20-\x7E]', ''

$jsonContent = $jsonClean | ConvertFrom-Json


    # --------------------------------------------------------------------------
    # 4. Check PCJ Module
    # --------------------------------------------------------------------------
    
# Check under ehr
if ($jsonContent.ehr.moduleConfigurations) {
    $pcjModule = $jsonContent.ehr.moduleConfigurations | Where-Object {
        $_.ModuleName -ieq "PCJ" -and $_.IsEnabled -eq $true
    }
}


    
if (-not $pcjModule) {
    Write-Host "PCJ module is NOT enabled."
    exit 0
}


    Write-Host "PCJ module is ENABLED. Starting cleanup..."

    # --------------------------------------------------------------------------
    # 5. Files to Delete
    # --------------------------------------------------------------------------
    $filesToDelete = @(
        (Join-Path $PSScriptRoot "Configs\Default\PxpEncounterCcdaRequest.xml"),
        (Join-Path $PSScriptRoot "Configs\Default\PxpLongitudinalCcdaRequest.xml"),
        (Join-Path $PSScriptRoot "Configs\Practice\PxpEncounterCcdaRequest.xml"),
        (Join-Path $PSScriptRoot "Configs\Practice\PxpLongitudinalCcdaRequest.xml")
    )

    # --------------------------------------------------------------------------
    # 6. Delete Files
    # --------------------------------------------------------------------------
    foreach ($file in $filesToDelete) {

        if (Test-Path $file) {

            Remove-Item $file -Force
            Write-Host "DELETED: $file"
        }
        else {
            Write-Host "NOT FOUND: $file"
        }
    }

    Write-Host "Cleanup completed successfully."
}
catch {

    Write-Host "ERROR OCCURRED"
    Write-Host $_.Exception.Message
    exit 1
}