<#
.SYNOPSIS
    Sets PC identity: computer name, description, and creates C:\X9 folder structure.

.DESCRIPTION
    Renames the computer if deployment.pcName is set in config.json. Sets the
    computer description (visible in System properties and network neighborhood).
    Creates C:\X9\ directory structure with subdirectories for logs, scripts and
    assets. Copies X9 icon and creates Desktop.ini so the folder shows a custom
    icon in Explorer. Computer rename requires a restart - this step runs last
    before the final summary.

.ITEMS
    rename-computer-dle-config-deployment-pcn: Renames the computer via Rename-Computer if config.json deployment.pcName is set and differs from the current name. Rename takes effect after restart. If pcName is empty, rename is skipped and the current name is preserved.
    popis-pocitace-computer-description: Sets the computer description shown in System Properties and Network Neighborhood. Read from config.json deployment.pcDescription, default "X9 deployment". Written to HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\SrvComment.
    vytvorit-cx9-adresar: Creates C:\X9\ with subdirectories Logs\, Scripts\, Assets\. Used for deployment logs, custom per-client scripts, and client-specific configuration assets.
    cx9-vlastni-ikonka-desktop-ini: Copies X9-ikona.ico to C:\X9\ and creates Desktop.ini with IconResource entry. Sets System+Hidden attributes on Desktop.ini and ReadOnly on C:\X9\ so Explorer displays the custom folder icon.
#>
param(
    [string]$ConfigPath,
    [string]$LogFile
)

. "$PSScriptRoot\common.ps1"
$Config = Load-Config $ConfigPath

# -----------------------------------------------------------------------
# C:\X9 directory structure
# -----------------------------------------------------------------------
Write-Log "Creating C:\X9 directory structure" -Level INFO

$x9Root = "C:\X9"
$x9Dirs = @("$x9Root\Logs", "$x9Root\Scripts", "$x9Root\Assets")

foreach ($dir in $x9Dirs) {
    try {
        if (-not (Test-Path $dir)) {
            New-Item -ItemType Directory -Path $dir -Force | Out-Null
        }
        Write-Log "  Dir: $dir" -Level OK
    }
    catch {
        Write-Log "  Failed to create $dir - $_" -Level ERROR
    }
}

# -----------------------------------------------------------------------
# Copy X9 icon and create Desktop.ini for custom folder appearance
# -----------------------------------------------------------------------
$assetsLogo = Join-Path $PSScriptRoot "..\assets\Logo"
$icoSrc     = Get-ChildItem -Path $assetsLogo -Filter "*.ico" -ErrorAction SilentlyContinue |
              Select-Object -First 1

if ($icoSrc) {
    $icoDest = "$x9Root\X9-ikona.ico"
    try {
        Copy-Item -Path $icoSrc.FullName -Destination $icoDest -Force
        Write-Log "  Copied icon: $icoDest" -Level OK
    }
    catch {
        Write-Log "  Failed to copy icon: $_" -Level WARN
    }

    $desktopIni = "$x9Root\desktop.ini"
    try {
        @"
[.ShellClassInfo]
IconResource=X9-ikona.ico,0
[ViewState]
Mode=
Vid=
FolderType=Generic
"@ | Set-Content -Path $desktopIni -Encoding Unicode -Force

        # desktop.ini must be System+Hidden; folder must be ReadOnly for Explorer to show the icon
        (Get-Item $desktopIni -Force).Attributes = "System,Hidden"
        (Get-Item $x9Root).Attributes            = "ReadOnly,Directory"
        Write-Log "  Desktop.ini created for custom folder icon" -Level OK
    }
    catch {
        Write-Log "  Failed to create desktop.ini: $_" -Level WARN
    }
} else {
    Write-Log "  No .ico found in assets\Logo - custom folder icon skipped" -Level WARN
}

# -----------------------------------------------------------------------
# Computer description
# -----------------------------------------------------------------------
$pcDesc = "X9 deployment"
if ($Config -and $Config.deployment -and $Config.deployment.pcDescription) {
    $pcDesc = $Config.deployment.pcDescription
}

Write-Log "Setting computer description: $pcDesc" -Level INFO
try {
    Set-ItemProperty `
        -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" `
        -Name "SrvComment" -Value $pcDesc -Type String -Force
    Write-Log "  Computer description set" -Level OK
}
catch {
    Write-Log "  Failed to set computer description: $_" -Level ERROR
}

# -----------------------------------------------------------------------
# Rename computer (must be last - requires restart to take effect)
# -----------------------------------------------------------------------
$pcName = $null
if ($Config -and $Config.deployment -and $Config.deployment.pcName) {
    $pcName = $Config.deployment.pcName.Trim()
}

$renamed = $false
if ($pcName -and $pcName -ne "") {
    $currentName = $env:COMPUTERNAME
    if ($currentName -eq $pcName) {
        Write-Log "Computer name already '$pcName' - no rename needed" -Level OK
    } else {
        Write-Log "Renaming computer: '$currentName' -> '$pcName'" -Level INFO
        try {
            Rename-Computer -NewName $pcName -Force -ErrorAction Stop
            Write-Log "  Computer renamed to '$pcName' (restart required)" -Level OK
            $renamed = $true
        }
        catch {
            Write-Log "  Failed to rename computer: $_" -Level ERROR
        }
    }
} else {
    Write-Log "No pcName in config - computer rename skipped" -Level INFO
}

Write-Log "Step 9 complete" -Level OK

# Signal reboot only when rename actually happened
if ($renamed) {
    Write-Log "Step 9 - reboot required for rename (exit 9)" -Level OK
    exit 9
}
