Axeon.psm1

# ---------------------------------------------------------------------------
# Axeon PowerShell Module
# Provides helper commands for consumers of the Axeon module.
# Install from PowerShell Gallery: Install-Module -Name Axeon
# ---------------------------------------------------------------------------

$script:ModuleRoot = $PSScriptRoot

<#
.SYNOPSIS
    Returns the root path where Axeon scaffoldable content is stored.
.DESCRIPTION
    Resolves the 'content' folder bundled inside the installed module directory.
#>

function Get-AxeonContentPath {
    [CmdletBinding()]
    param()

    $contentPath = Join-Path $script:ModuleRoot 'content'

    if (-not (Test-Path $contentPath)) {
        throw "Axeon content folder not found at '$contentPath'. Ensure the module is installed correctly."
    }

    return $contentPath
}

<#
.SYNOPSIS
    Scaffolds Axeon templates, scripts and configuration into a target directory.
.DESCRIPTION
    Copies all required Axeon content (scripts, schemas, Bicep modules,
    and reference configuration) into the specified target path.
    Existing files are NOT overwritten unless -Force is specified.
.PARAMETER TargetPath
    The destination directory to scaffold into. Defaults to the current directory.
.PARAMETER Force
    Overwrite existing files if they already exist in the target directory.
.EXAMPLE
    Initialize-Axeon -TargetPath ./my-infra
.EXAMPLE
    Initialize-Axeon -Force
#>

function Initialize-Axeon {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Position = 0)]
        [string]$TargetPath = (Get-Location).Path,

        [switch]$Force
    )

    $contentPath = Get-AxeonContentPath
    $TargetPath = (Resolve-Path -Path $TargetPath -ErrorAction SilentlyContinue) ??
    (New-Item -ItemType Directory -Path $TargetPath -Force).FullName

    Write-Host "Initializing Axeon project at: $TargetPath" -ForegroundColor Cyan

    $items = Get-ChildItem -Path $contentPath -Recurse -File
    $copied = 0
    $skipped = 0

    foreach ($item in $items) {
        $relativePath = $item.FullName.Substring($contentPath.Length).TrimStart([IO.Path]::DirectorySeparatorChar, [IO.Path]::AltDirectorySeparatorChar)
        $destination = Join-Path $TargetPath $relativePath
        $destDir = Split-Path $destination -Parent

        if (-not (Test-Path $destDir)) {
            New-Item -ItemType Directory -Path $destDir -Force | Out-Null
        }

        if ((Test-Path $destination) -and -not $Force) {
            Write-Verbose "Skipping (exists): $relativePath"
            $skipped++
            continue
        }

        if ($PSCmdlet.ShouldProcess($relativePath, "Copy")) {
            Copy-Item -Path $item.FullName -Destination $destination -Force
            Write-Verbose "Copied: $relativePath"
            $copied++
        }
    }

    Write-Host ""
    Write-Host "Axeon initialization complete." -ForegroundColor Green
    Write-Host " Files copied : $copied" -ForegroundColor White
    Write-Host " Files skipped: $skipped (already exist, use -Force to overwrite)" -ForegroundColor DarkGray
    Write-Host ""
    Write-Host "Next steps:" -ForegroundColor Yellow
    Write-Host " 1. Edit 'platform-spec.json' to match your environment."
    Write-Host " 2. Review deployment definitions in 'deployments/' directory."
    Write-Host " 3. Run './src/scripts/Invoke-PlatformBootstrap.ps1 -ConfigPath ./platform-spec.json -Deployment hub-networking'"
}

<#
.SYNOPSIS
    Shows the version of the installed Axeon module.
#>

function Get-AxeonVersion {
    [CmdletBinding()]
    param()

    $manifest = Import-PowerShellDataFile (Join-Path $script:ModuleRoot 'Axeon.psd1') -ErrorAction SilentlyContinue
    if ($manifest) {
        $version = $manifest.ModuleVersion
        $prerelease = $manifest.PrivateData.PSData.Prerelease
        if ($prerelease) { $version = "$version-$prerelease" }
        return $version
    }

    return "unknown"
}

# ---------------------------------------------------------------------------
# Exports
# ---------------------------------------------------------------------------
Export-ModuleMember -Function @(
    'Initialize-Axeon'
    'Get-AxeonContentPath'
    'Get-AxeonVersion'
)