# Synopsis: Promotes validated assets from a MirrorOS development workspace to an AgentOS production directory.
# Description: Copies files and scripts from the source (MirrorOS dev) to the target (AgentOS prod),
# patches file names and internal references so the production copy knows it is AgentOS,
# and cleans up development-only skills from the production target.

param(
    [Parameter(Mandatory=$true, HelpMessage="Enter the absolute path to the target AgentOS production directory")]
    [string]$TargetProdDir
)

$MirrorDir = Split-Path -Parent $PSScriptRoot
$MirrorDir = Split-Path -Parent $MirrorDir  # Go up from Skills/PowerShell to workspace root

Write-Output "=== DEPLOYING TO PRODUCTION (AGENTOS) ==="
Write-Output "Source (MirrorOS): $MirrorDir"
Write-Output "Target (AgentOS): $TargetProdDir"

if (!(Test-Path $TargetProdDir)) {
    Write-Output "[+] Target production directory missing. Creating: $TargetProdDir"
    New-Item -ItemType Directory -Path $TargetProdDir -Force | Out-Null
}

# 0. Scaffold Required Subdirectories
Write-Output "Ensuring target directories exist..."
$SubDirs = @("Config", "Skills", "Projects", "Memory", "Mailbox")
foreach ($sub in $SubDirs) {
    $SubPath = Join-Path $TargetProdDir $sub
    if (!(Test-Path $SubPath)) {
        New-Item -ItemType Directory -Path $SubPath -Force | Out-Null
        Write-Output "  -> Created: $sub"
    }
}

# 1. Copy & Patch Orchestrator Scripts — rename MirrorOS* to AgentOS* in the target
$ScriptPairs = @(
    @{ Src = "Scripts\MirrorOS.ps1";  Dst = "Scripts\AgentOS.ps1" },
    @{ Src = "Scripts\MirrorOSX.ps1"; Dst = "Scripts\AgentOSX.ps1" },
    @{ Src = "Scripts\MirrorOSP.py";  Dst = "Scripts\AgentOSP.py" },
    @{ Src = "Start MirrorOS.bat";  Dst = "Start AgentOS.bat" },
    @{ Src = "Start MirrorOSP.bat"; Dst = "Start AgentOSP.bat" }
)

foreach ($pair in $ScriptPairs) {
    $SrcPath = Join-Path $MirrorDir $pair.Src
    $DstPath = Join-Path $TargetProdDir $pair.Dst
    if (Test-Path $SrcPath) {
        $Content = Get-Content -Path $SrcPath -Raw
        # Patch internal references: Mirror -> Agent
        $Content = $Content -replace "Scripts\\MirrorOSH_Chat\.html", "Scripts\\AgentOSH_Chat.html"
        $Content = $Content -replace "MirrorOSP", "AgentOSP"
        $Content = $Content -replace "MirrorOSX", "AgentOSX"
        $Content = $Content -replace "MirrorOSH", "AgentOSH"
        $Content = $Content -replace "MirrorOS", "AgentOS"
        $Content = $Content -replace "Invoke-MirrorSelfTest", "Invoke-AgentSelfTest"
        $Content = $Content -replace "class MirrorOSP", "class AgentOSP"
        $Content = $Content -replace "Start MirrorOS\.bat", "Start AgentOS.bat"
        $Content = $Content -replace "Start MirrorOS\.bat", "Start AgentOS.bat"
        $Content = $Content -replace "Start MirrorOSP\.bat", "Start AgentOSP.bat"
        $Content = $Content -replace "Launch-MirrorOSH\.ps1", "Launch-AgentOSH.ps1"
        $Content = $Content -replace "Subagent-MirrorOSH\.ps1", "Subagent-AgentOSH.ps1"
        $Content = $Content -replace "Sync-MirrorOS\.ps1", "Sync-AgentOS.ps1"
        $Content = $Content -replace "To_MirrorOSH\.json", "To_AgentOSH.json"
        $Content = $Content -replace "MirrorOS - Experimental Multimodal Branch", "AgentOS - Production Multimodal Branch"
        $Content = $Content -replace "MirrorOS Experimental Branch", "AgentOS Production Branch"
        $Content = $Content -replace "MirrorOS Online", "AgentOS Online"
        $Content = $Content -replace "Terminating MirrorOS Session", "Terminating AgentOS Session"
        $Content = $Content -replace "Terminating MirrorOSP session", "Terminating AgentOSP session"
        $Content = $Content -replace "Bootstrapping MirrorOS Context", "Bootstrapping AgentOS Context"
        $Content = $Content -replace "Sending to MirrorOS\.\.\.", "Sending to AgentOS..."
        $Content = $Content -replace "Closes MirrorOSP\.", "Closes AgentOSP."
        $Content = $Content -replace "Closes MirrorOS\.", "Closes AgentOS."
        $Content = $Content -replace "inside MirrorOS", "inside AgentOS"
        $Content = $Content -replace "MirrorOSP:", "AgentOSP:"
        $Content = $Content -replace "Modified print loop block inside MirrorOS", "Modified print loop block inside AgentOS"
        $Content = $Content -replace "EdgeInsecureMirrorOSH", "EdgeInsecureAgentOSH"
        $Content = $Content -replace "MirrorOSP Python agent", "AgentOSP Python agent"
        Set-Content -Path $DstPath -Value $Content -Encoding UTF8
    }
}

# Copy OSH HTML
$MirrorOshHtml = Join-Path $MirrorDir "Scripts\MirrorOSH_Chat.html"
if (Test-Path $MirrorOshHtml) {
    $OshContent = Get-Content $MirrorOshHtml -Raw
    $OshContent = $OshContent -replace "MirrorOSH", "AgentOSH"
    $OshContent = $OshContent -replace "MirrorOS", "AgentOS"
    Set-Content -Path (Join-Path $TargetProdDir "AgentOSH_Chat.html") -Value $OshContent -Encoding UTF8
}

# 2. Copy Configs & Patch
Write-Output "Deploying and Patching Configs..."
Copy-Item -Path "$MirrorDir\Config\*" -Destination "$TargetProdDir\Config\" -Force -Recurse

# Patch identity for production
$ProdIdentityPath = Join-Path $TargetProdDir "Config\identity.txt"
if (Test-Path $ProdIdentityPath) {
    $IdentityContent = Get-Content -Path $ProdIdentityPath -Raw
    $IdentityContent = $IdentityContent -replace "You are MirrorOS, the development branch", "You are AgentOS, the production branch"
    $IdentityContent = $IdentityContent -replace "development branch", "production branch"
    $IdentityContent = $IdentityContent -replace "MirrorOS: development workspace.*?first\.", "AgentOS: production/stable workspace. Update sparingly after validation."
    $IdentityContent = $IdentityContent -replace "MirrorOS \(OS\)", "AgentOS (OS)"
    $IdentityContent = $IdentityContent -replace "MirrorOSX \(OSX\)", "AgentOSX (OSX)"
    $IdentityContent = $IdentityContent -replace "MirrorOSH \(OSH\)", "AgentOSH (OSH)"
    $IdentityContent = $IdentityContent -replace "MirrorOSP \(OSP\)", "AgentOSP (OSP)"
    Set-Content -Path $ProdIdentityPath -Value $IdentityContent -Encoding UTF8
}

# Global Scrub on all Config Files
$Configs = Get-ChildItem -Path "$TargetProdDir\Config" -File
foreach ($cfg in $Configs) {
    $content = Get-Content $cfg.FullName -Raw
    $newContent = $content -replace "MirrorOSH", "AgentOSH"
    $newContent = $newContent -replace "MirrorOSP", "AgentOSP"
    $newContent = $newContent -replace "MirrorOSX", "AgentOSX"
    $newContent = $newContent -replace "MirrorOS", "AgentOS"
    if ($content -cne $newContent) {
        Set-Content -Path $cfg.FullName -Value $newContent -Encoding UTF8
    }
}

# 3. Copy Skills
Write-Output "Deploying Skills Registry and Files..."
Copy-Item -Path "$MirrorDir\Skills\*" -Destination "$TargetProdDir\Skills\" -Force -Recurse

# 4. Patch skill scripts for production naming
Write-Output "Patching Production Skills..."
$ProdSkills = Get-ChildItem -Path "$TargetProdDir\Skills" -Filter "*.ps1" -Recurse
foreach ($skill in $ProdSkills) {
    $content = Get-Content $skill.FullName -Raw
    $newContent = $content
    $newContent = $newContent -replace "Scripts\\MirrorOSH_Chat\.html", "Scripts\\AgentOSH_Chat.html"
    $newContent = $newContent -replace "To_MirrorOSH\.json", "To_AgentOSH.json"
    $newContent = $newContent -replace "MirrorOSH \(Front-End\)", "AgentOSH (Front-End)"
    $newContent = $newContent -replace "MirrorOSH", "AgentOSH"
    $newContent = $newContent -replace "MirrorOSP", "AgentOSP"
    $newContent = $newContent -replace "MirrorOSX", "AgentOSX"
    $newContent = $newContent -replace "MirrorOS", "AgentOS"
    if ($content -cne $newContent) {
        Set-Content -Path $skill.FullName -Value $newContent -Encoding UTF8
    }
}

Write-Output "=== DEPLOYMENT COMPLETE ==="