# -----------------------------------------------
# Elevate script if not running as Administrator
# ---------------------------------------------------
if (-not ([Security.Principal.WindowsPrincipal] `
          [Security.Principal.WindowsIdentity]::GetCurrent() `
          ).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {

    Start-Process powershell `
        -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" `
        -Verb RunAs
    exit
}

# Force normal PowerShell colors after elevation
try {
    $Host.UI.RawUI.BackgroundColor = 'Black'
    $Host.UI.RawUI.ForegroundColor = 'White'
    Clear-Host
} catch {}

# ------------
# Load Config
# ----------------
# Get the parent folder of the currently running script
$ProjectRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)

# Check if "scripts\config" exists
$ScriptConfigPath1up = Join-Path $ProjectRoot "scripts\config"
$ScriptConfigPath2up = Join-Path $ProjectRoot "config"

if (Test-Path $ScriptConfigPath1up) {
    # If Scripts\Config exists
    $ConfigFile = Join-Path $ScriptConfigPath1up "config.ps1"
} elseif (Test-Path $ScriptConfigPath2up) {
    # If \Config exists
    $ConfigFile = Join-Path $ScriptConfigPath2up "config.ps1"
} else {
    throw "Could not find Config.ps1!"
}

# Dot-source the config
. $ConfigFile

$ConfigPath = Split-Path -Parent $ConfigFile
$Script  = Split-Path $PSCommandPath -Leaf

# Disable QuickEdit for this session
. "$ConfigPath\DisableConsoleQuickEdit.ps1"
Set-QuickEdit -DisableQuickEdit
cls

# --------------------
# Display Main Banner
# ------------------------
Write-Host ""
Write-Host "============================================================" -ForegroundColor DarkGray
Write-Host " RMTechCentral Automation Scripts (v$($ReleaseInfo.Version))" -ForegroundColor Cyan

switch ($Global:ReleaseInfo.Channel) {
    'Internal' {
        Write-Host " Internal Use Only" -ForegroundColor Yellow
    }
    'Public' {
        Write-Host " Public Release" -ForegroundColor Green
    }
    default {
        Write-Host " Release Channel: $($Global:ReleaseInfo.Channel)" -ForegroundColor DarkYellow
    }
}

Write-Host "============================================================" -ForegroundColor DarkGray
Write-Host " Author:  $($RMTechMeta.Author)" -ForegroundColor Gray
Write-Host " Website: $($RMTechMeta.Website)" -ForegroundColor Gray
Write-Host " Purpose: $($RMTechMeta.Purpose)" -ForegroundColor Gray
Write-Host "============================================================" -ForegroundColor DarkGray
Write-Host ""

# Small delay so user can read
Start-Sleep -Seconds 2
Write-Host "$Script running as: $($RMTechMeta.User) on $($RMTechMeta.Machine)" -ForegroundColor DarkGray
Write-Host ""
Write-Host ""

# Small delay so user can read
Start-Sleep -Seconds 3
# Display Pending Operation
Write-Host "============================================================" -ForegroundColor DarkGray
Write-Host " Pending Operation: Inject AppX and MsiX Packages" -ForegroundColor DarkGreen
Write-Host " Review this step carefully. Close this window to cancel." -ForegroundColor Yellow
Write-Host "============================================================" -ForegroundColor DarkGray
Write-Host ""

# Wait for user to confirm
Write-Host "Press ENTER to continue..." -ForegroundColor Gray
Read-Host

# -----------
# Run Script
# ---------------
Write-Host "Injecting AppX/MsiX packages from $($Paths.AppxBundlesRoot) ..." -ForegroundColor Cyan
Start-Sleep -Seconds 3
Get-ChildItem "$($Paths.AppxBundlesRoot)" -File |
    Where-Object { $_.Extension -in '.appx', '.msix', '.appxbundle', '.msixbundle' } |
    ForEach-Object {
        Write-Host "Injecting package: $($_.Name)" -ForegroundColor Yellow
        dism /Image:"$($Paths.WIMMountRoot)" /Add-ProvisionedAppxPackage /PackagePath:$($_.FullName) /SkipLicense
    }

Start-Sleep -Seconds 1

# --------------------------------------------
# Optional: Inject individual package
# --------------------------------------------
# Write-Host "Injecting individual package..." -ForegroundColor Cyan
# dism /Image:"$($Paths.WIMMountRoot)" /Add-ProvisionedAppxPackage /PackagePath:"$(Join-Path $Paths.AppxBundlesRoot 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle')" /SkipLicense

# -------
# Finish
# -----------
Write-Host ""
Write-Host ""
Write-Host ""
Write-Host "=========================================================================" -ForegroundColor DarkGray
Write-Host " [DONE] Operation complete!" -ForegroundColor Green
Write-Host " Please review the output above for any warnings or errors before exiting." -ForegroundColor Yellow
Write-Host "=========================================================================" -ForegroundColor DarkGray
Write-Host ""

# Wait for user to confirm
Write-Host "Press ENTER to quit..." -ForegroundColor Gray
Read-Host