# Synopsis: Deploys files from AgentOS production to a MirrorOS development workspace.
# Description: Creates the target directory structure, copies core files and skills,
# then patches file names, identity strings, and internal references so the copy
# knows it is the development (Mirror) instance — not production.

param(
    [Parameter(Mandatory=$true, HelpMessage="Enter the absolute path to the target MirrorOS development directory")]
    [string]$TargetMirrorDir
)

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

Write-Output "=== INITIALIZING MIRROR DEPLOYMENT ==="
Write-Output "Source (AgentOS): $SourceDir"
Write-Output "Target (MirrorOS): $TargetMirrorDir"

# 1. Create Directory Structure
if (!(Test-Path $TargetMirrorDir)) { New-Item -ItemType Directory -Path $TargetMirrorDir -Force | Out-Null }
New-Item -ItemType Directory -Path "$TargetMirrorDir\Config" -Force -ErrorAction SilentlyContinue | Out-Null
New-Item -ItemType Directory -Path "$TargetMirrorDir\Memory" -Force -ErrorAction SilentlyContinue | Out-Null
New-Item -ItemType Directory -Path "$TargetMirrorDir\Skills" -Force -ErrorAction SilentlyContinue | Out-Null
Write-Output "Directories created."

# 2. Copy Core Files — rename AgentOS* to MirrorOS* in the target
$SourceScripts = @(
    @{ Src = "Scripts\AgentOS.ps1";  Dst = "Scripts\MirrorOS.ps1" },
    @{ Src = "Scripts\AgentOSX.ps1"; Dst = "Scripts\MirrorOSX.ps1" },
    @{ Src = "Scripts\AgentOSP.py";  Dst = "Scripts\MirrorOSP.py" },
    @{ Src = "Start AgentOS.bat";  Dst = "Start MirrorOS.bat" },
    @{ Src = "Start AgentOSP.bat"; Dst = "Start MirrorOSP.bat" }
)

foreach ($pair in $SourceScripts) {
    $SrcPath = Join-Path $SourceDir $pair.Src
    $DstPath = Join-Path $TargetMirrorDir $pair.Dst
    if (Test-Path $SrcPath) {
        Copy-Item -Path $SrcPath -Destination $DstPath -Force
    }
}

# Copy HTML, configs, and skills
$AgentOshHtml = Join-Path $SourceDir "Scripts\AgentOSH_Chat.html"
if (Test-Path $AgentOshHtml) {
    Copy-Item -Path $AgentOshHtml -Destination (Join-Path $TargetMirrorDir "Scripts\MirrorOSH_Chat.html") -Force
}
Copy-Item -Path "$SourceDir\Config\*" -Destination "$TargetMirrorDir\Config\" -Force -Recurse
Copy-Item -Path "$SourceDir\Skills\*" -Destination "$TargetMirrorDir\Skills\" -Force -Recurse
Write-Output "Core files and skills cloned."

# 3. Patch internal references in the mirror orchestrator scripts
# AgentOS -> MirrorOS, AgentOSX -> MirrorOSX, AgentOSH -> MirrorOSH, AgentOSP -> MirrorOSP
$MirrorScripts = @("MirrorOS.ps1", "MirrorOSX.ps1", "MirrorOSP.py")
foreach ($scriptName in $MirrorScripts) {
    $scriptPath = Join-Path $TargetMirrorDir $scriptName
    if (Test-Path $scriptPath) {
        $scriptContent = Get-Content $scriptPath -Raw
        # Replace agent variant names (longest first to avoid partial matches)
        $scriptContent = $scriptContent -replace "AgentOSH_Chat\.html", "MirrorOSH_Chat.html"
        $scriptContent = $scriptContent -replace "AgentOSP", "MirrorOSP"
        $scriptContent = $scriptContent -replace "AgentOSX", "MirrorOSX"
        $scriptContent = $scriptContent -replace "AgentOSH", "MirrorOSH"
        $scriptContent = $scriptContent -replace "AgentOS", "MirrorOS"
        $scriptContent = $scriptContent -replace "Invoke-AgentSelfTest", "Invoke-MirrorSelfTest"
        $scriptContent = $scriptContent -replace "class AgentOSP", "class MirrorOSP"
        # File references
        $scriptContent = $scriptContent -replace "Start AgentOS\.bat", "Start MirrorOS.bat"
        $scriptContent = $scriptContent -replace "Start AgentOS\.bat", "Start MirrorOS.bat"
        $scriptContent = $scriptContent -replace "Start AgentOSP\.bat", "Start MirrorOSP.bat"
        $scriptContent = $scriptContent -replace "Launch-AgentOSH\.ps1", "Launch-MirrorOSH.ps1"
        $scriptContent = $scriptContent -replace "Subagent-AgentOSH\.ps1", "Subagent-MirrorOSH.ps1"
        $scriptContent = $scriptContent -replace "Sync-AgentOS\.ps1", "Sync-MirrorOS.ps1"
        $scriptContent = $scriptContent -replace "To_AgentOSH\.json", "To_MirrorOSH.json"
        # Production identity strings -> development
        $scriptContent = $scriptContent -replace "AgentOS - Production Multimodal Branch", "MirrorOS - Experimental Multimodal Branch"
        $scriptContent = $scriptContent -replace "AgentOS Production Branch", "MirrorOS Experimental Branch"
        $scriptContent = $scriptContent -replace "AgentOS Online", "MirrorOS Online"
        $scriptContent = $scriptContent -replace "Terminating AgentOS Session", "Terminating MirrorOS Session"
        $scriptContent = $scriptContent -replace "Terminating AgentOSP session", "Terminating MirrorOSP session"
        $scriptContent = $scriptContent -replace "Bootstrapping AgentOS Context", "Bootstrapping MirrorOS Context"
        $scriptContent = $scriptContent -replace "Sending to AgentOS\.\.\.", "Sending to MirrorOS..."
        $scriptContent = $scriptContent -replace "Closes AgentOSP\.", "Closes MirrorOSP."
        $scriptContent = $scriptContent -replace "Closes AgentOS\.", "Closes MirrorOS."
        $scriptContent = $scriptContent -replace "inside AgentOS", "inside MirrorOS"
        $scriptContent = $scriptContent -replace "AgentOSP:", "MirrorOSP:"
        $scriptContent = $scriptContent -replace "Modified print loop block inside AgentOS", "Modified print loop block inside MirrorOS"
        $scriptContent = $scriptContent -replace "EdgeInsecureAgentOSH", "EdgeInsecureMirrorOSH"
        $scriptContent = $scriptContent -replace "AgentOSP Python agent", "MirrorOSP Python agent"
        Set-Content -Path $scriptPath -Value $scriptContent -Encoding UTF8
    }
}

# 4. Patch identity in the mirror
$identityPath = "$TargetMirrorDir\Config\identity.txt"
if (Test-Path $identityPath) {
    $identity = Get-Content $identityPath -Raw
    $identity = $identity -replace 'You are AgentOS, the production branch', 'You are MirrorOS, the development branch'
    $identity = $identity -replace 'production branch', 'development branch'
    $identity = $identity -replace 'production/stable workspace\. This is where you are\. Update sparingly after validation\.', 'development workspace. Iterate constantly, test features here first.'
    $identity = $identity -replace 'AgentOS: production/stable workspace\..*', 'MirrorOS: development workspace. Iterate constantly, test features here first.'
    $identity = $identity -replace 'MirrorOS: development workspace\..*?first\.', 'MirrorOS: development workspace. Iterate constantly, test features here first.'
    $identity = $identity -replace 'AgentOS \(OS\)', 'MirrorOS (OS)'
    $identity = $identity -replace 'AgentOSX \(OSX\)', 'MirrorOSX (OSX)'
    $identity = $identity -replace 'AgentOSH \(OSH\)', 'MirrorOSH (OSH)'
    $identity = $identity -replace 'AgentOSP \(OSP\)', 'MirrorOSP (OSP)'
    # Swap peer awareness: MirrorOS becomes the dev peer, AgentOS is the production peer
    $identity = $identity -replace 'The MirrorOS workspace at Working/MirrorOS/ is your development sandbox\. New features are tested there before promotion to this production workspace\.', 'The AgentOS workspace at Working/AgentOS/ is your production target. New features tested here are promoted there after validation.'
    $identity = $identity -replace 'Use Deploy-Mirror\.ps1 to push changes from here to MirrorOS\. Use Deploy-ToProduction\.ps1 to promote validated changes from MirrorOS back here\.', 'Use Deploy-ToProduction.ps1 to promote validated changes from here to AgentOS. Use Deploy-Mirror.ps1 to refresh this workspace from AgentOS when needed.'
    Set-Content -Path $identityPath -Value $identity -Encoding UTF8
}

# 5. Patch all skill scripts in the mirror
$MirrorSkills = Get-ChildItem -Path "$TargetMirrorDir\Skills" -Filter "*.ps1" -Recurse
foreach ($skill in $MirrorSkills) {
    $content = Get-Content $skill.FullName -Raw
    $newContent = $content
    $newContent = $newContent -replace "AgentOSH_Chat\.html", "MirrorOSH_Chat.html"
    $newContent = $newContent -replace "To_AgentOSH\.json", "To_MirrorOSH.json"
    $newContent = $newContent -replace "AgentOSH \(Front-End\)", "MirrorOSH (Front-End)"
    $newContent = $newContent -replace "AgentOSH", "MirrorOSH"
    $newContent = $newContent -replace "AgentOSP", "MirrorOSP"
    $newContent = $newContent -replace "AgentOSX", "MirrorOSX"
    $newContent = $newContent -replace "AgentOS", "MirrorOS"
    if ($content -cne $newContent) {
        Set-Content -Path $skill.FullName -Value $newContent -Encoding UTF8
    }
}

Write-Output "Identity, scripts, and skills patched for MirrorOS development."
Write-Output "=== DEPLOYMENT SUCCESSFUL ==="
Write-Output "To launch the mirror, open a new PowerShell window and run: $TargetMirrorDir\MirrorOS.ps1"