# Copy DOS batch files to AD2
# Destination: \\AD2\test\COMMON\ProdSW

$Source = "D:\ClaudeTools"
$Destination = "\\192.168.0.6\C$\Shares\test\COMMON\ProdSW"
$Username = "INTRANET\sysadmin"
$Password = ConvertTo-SecureString "Paper123!@#" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential($Username, $Password)

# Files to copy
$Files = @(
    "NWTOC.BAT",
    "CTONW.BAT",
    "UPDATE.BAT",
    "STAGE.BAT",
    "REBOOT.BAT",
    "CHECKUPD.BAT",
    "DEPLOY.BAT"
)

Write-Host "Connecting to AD2..."

# Test connection first
try {
    New-PSDrive -Name TEMP_AD2 -PSProvider FileSystem -Root "\\192.168.0.6\C$" -Credential $Cred -ErrorAction Stop | Out-Null
    Write-Host "[OK] Connected to AD2 C$ share"

    # Copy to both COMMON and _COMMON locations (sync script checks both)
    $destinations = @(
        "TEMP_AD2:\Shares\test\COMMON\ProdSW",
        "TEMP_AD2:\Shares\test\_COMMON\ProdSW"
    )

    foreach ($destBase in $destinations) {
        Write-Host ""
        Write-Host "Checking: $destBase"

        # Create directory if it doesn't exist
        if (-not (Test-Path $destBase)) {
            Write-Host "  Creating directory: $destBase"
            New-Item -ItemType Directory -Path $destBase -Force | Out-Null
        }

        Write-Host "  [OK] Destination exists"

        # Copy each file
        foreach ($file in $Files) {
            $sourcePath = Join-Path $Source $file
            $destPath = Join-Path $destBase $file

            if (Test-Path $sourcePath) {
                Write-Host "  Copying: $file"
                Copy-Item -Path $sourcePath -Destination $destPath -Force
                Write-Host "    [OK] $file copied"
            } else {
                Write-Host "    [ERROR] Source file not found: $sourcePath"
            }
        }
    }

    Write-Host ""
    Write-Host "[SUCCESS] All files copied to AD2"
    Write-Host "Files copied to both COMMON and _COMMON"
    Write-Host "Files will sync to NAS within 15 minutes"

    Remove-PSDrive TEMP_AD2

} catch {
    Write-Host "[ERROR] Failed to connect to AD2: $_"
    exit 1
}
