# Encrypt guide files using AES-256-CBC with fixed key/IV
$base = Split-Path -Parent $MyInvocation.MyCommand.Path
$assetsDir = Join-Path $base "src\AxCopilot\Assets"

$key = [byte[]]@(0x41,0x58,0x43,0x6F,0x70,0x69,0x6C,0x6F,
                  0x74,0x47,0x75,0x69,0x64,0x65,0x4B,0x65,
                  0x79,0x32,0x30,0x32,0x36,0x53,0x65,0x63,
                  0x75,0x72,0x65,0x46,0x69,0x78,0x65,0x64)

$iv  = [byte[]]@(0x47,0x75,0x69,0x64,0x65,0x49,0x56,0x46,
                  0x69,0x78,0x65,0x64,0x32,0x30,0x32,0x36)

function Encrypt-File($inputPath, $outputPath) {
    $plainBytes = [System.IO.File]::ReadAllBytes($inputPath)

    $aes = [System.Security.Cryptography.Aes]::Create()
    $aes.Key = $key
    $aes.IV = $iv
    $aes.Mode = [System.Security.Cryptography.CipherMode]::CBC
    $aes.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7

    $enc = $aes.CreateEncryptor()
    $encrypted = $enc.TransformFinalBlock($plainBytes, 0, $plainBytes.Length)

    [System.IO.File]::WriteAllBytes($outputPath, $encrypted)
    $enc.Dispose()
    $aes.Dispose()

    Write-Host "Encrypted: $inputPath -> $outputPath ($($plainBytes.Length) -> $($encrypted.Length) bytes)"
}

# Find guide files
$htmFiles = Get-ChildItem $assetsDir -Filter "*.htm"
foreach ($f in $htmFiles) {
    Write-Host "Found: $($f.Name) ($($f.Length) bytes)"
    if ($f.Name.Length -eq 20) {
        # Shorter name = user guide (사용가이드)
        $userGuidePath = $f.FullName
    } else {
        # Longer name = developer guide (개발자가이드)
        $devGuidePath = $f.FullName
    }
}

if ($userGuidePath) {
    Encrypt-File $userGuidePath (Join-Path $assetsDir "guide_user.enc")
}
if ($devGuidePath) {
    Encrypt-File $devGuidePath (Join-Path $assetsDir "guide_dev.enc")
}

Write-Host "Done!"
