#Requires -Modules @{ ModuleName='Pester'; ModuleVersion='5.0.0' }

Describe 'Set-SmRegValue' {
    BeforeAll {
        . "$PSScriptRoot\..\branding\lib\RegistryHelpers.ps1"
        $script:root = 'HKCU:\Software\SilverMetalTest'
        if (Test-Path $script:root) { Remove-Item $script:root -Recurse -Force }
    }
    AfterAll {
        if (Test-Path $script:root) { Remove-Item $script:root -Recurse -Force }
    }

    It 'creates the key path and writes a string value' {
        Set-SmRegValue -Root $script:root -SubKey 'A\B' -Name 'Greeting' -Type String -Value 'hi'
        (Get-ItemProperty "$script:root\A\B").Greeting | Should -Be 'hi'
    }

    It 'writes a dword value' {
        Set-SmRegValue -Root $script:root -SubKey 'A' -Name 'Flag' -Type DWord -Value 1
        (Get-ItemProperty "$script:root\A").Flag | Should -Be 1
    }
}

Describe 'Branding layer writers' {
    BeforeAll {
        . "$PSScriptRoot\..\branding\lib\BrandingLayers.ps1"
        $script:sw = 'HKCU:\Software\SilverMetalTest\SW'
        $script:du = 'HKCU:\Software\SilverMetalTest\DU'
        $script:m  = Get-Content "$PSScriptRoot\..\branding\branding.manifest.json" -Raw | ConvertFrom-Json
    }
    AfterAll {
        Remove-Item 'HKCU:\Software\SilverMetalTest' -Recurse -Force -ErrorAction SilentlyContinue
    }

    It 'writes OEM About info' {
        Set-OemInformation -SoftwareRoot $script:sw -Manifest $script:m -LogoPath 'C:\Windows\System32\oemlogo.bmp'
        $k = Get-ItemProperty "$script:sw\Microsoft\Windows\CurrentVersion\OEMInformation"
        $k.Manufacturer | Should -Be 'SilverLABS'
        $k.Model        | Should -Be 'SilverMetal Windows'
        $k.SupportURL   | Should -Be 'https://silverlabs.uk'
        $k.SupportHours | Should -Be '24/7 community + paid SLA'
        $k.Logo         | Should -Be 'C:\Windows\System32\oemlogo.bmp'
    }

    It 'writes a locked lock-screen image' {
        Set-LockScreen -SoftwareRoot $script:sw -ImagePath 'C:\Windows\Web\Screen\SilverMetal\lockscreen.jpg' -Lock $true
        (Get-ItemProperty "$script:sw\Microsoft\Windows\CurrentVersion\PersonalizationCSP").LockScreenImageStatus | Should -Be 1
        (Get-ItemProperty "$script:sw\Policies\Microsoft\Windows\Personalization").NoChangingLockScreen | Should -Be 1
    }

    It 'writes desktop wallpaper + dark mode into the default-user root' {
        Set-DesktopBranding -DefaultUserRoot $script:du -Manifest $script:m -WallpaperPath 'C:\Windows\Web\Wallpaper\SilverMetal\wallpaper.jpg'
        (Get-ItemProperty "$script:du\Control Panel\Desktop").WallPaper | Should -Be 'C:\Windows\Web\Wallpaper\SilverMetal\wallpaper.jpg'
        (Get-ItemProperty "$script:du\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize").AppsUseLightTheme | Should -Be 0
    }

    It 'writes the BitLocker pre-boot recovery message policy' {
        Set-BitLockerPreboot -SoftwareRoot $script:sw -Manifest $script:m
        $k = Get-ItemProperty "$script:sw\Policies\Microsoft\FVE"
        $k.UseCustomRecoveryMessage | Should -Be 1
        $k.RecoveryMessage          | Should -Be 'SilverMetal Windows. Locked out? silverlabs.uk'
        $k.RecoveryUrl              | Should -Be 'https://silverlabs.uk'
    }
}

Describe 'Apply-Branding -Mode Offline' {
    BeforeAll {
        # Build a throwaway "mount" tree with empty SOFTWARE + default NTUSER hives.
        $script:mount = Join-Path $env:TEMP ("sm-brandtest-" + [guid]::NewGuid().ToString('N'))
        New-Item -ItemType Directory -Force "$script:mount\Windows\System32\config","$script:mount\Users\Default" | Out-Null
        # Create two empty hive files by loading/unloading a fresh key.
        foreach ($h in @("$script:mount\Windows\System32\config\SOFTWARE","$script:mount\Users\Default\NTUSER.DAT")) {
            reg load 'HKLM\SM_SEED' (New-Item -ItemType File -Force $h | Select-Object -Expand FullName) 2>$null | Out-Null
        }
        & reg unload 'HKLM\SM_SEED' 2>$null | Out-Null
        # The above seed is best-effort; if reg can't init an empty file, the apply
        # script creates the hives via reg load of the path it expects.
    }
    AfterAll { Remove-Item $script:mount -Recurse -Force -ErrorAction SilentlyContinue }

    It 'applies all layers into the offline SOFTWARE hive and reports success' {
        $r = & "$PSScriptRoot\..\branding\Apply-Branding.ps1" -Mode Offline -MountPath $script:mount -PassThru
        $r.OemApplied        | Should -BeTrue
        $r.LockScreenApplied | Should -BeTrue
        $r.DesktopApplied    | Should -BeTrue
        $r.BitLockerApplied  | Should -BeTrue
    }
}
