#Requires -Version 5.1
<#
.SYNOPSIS
  Register CCS PDF Editor for .ccspdf (default) and .pdf (Open with only). HKCU - no admin.

.DESCRIPTION
  Lives at tools\scripts\register-file-types.ps1.
  Package root (CcsPdfEditor.exe) is two levels up.
#>
[CmdletBinding()]
param(
    [string]$ExePath = ""
)

$ErrorActionPreference = "Stop"

$PackageRoot = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path

if (-not $ExePath) {
    $ExePath = Join-Path $PackageRoot "CcsPdfEditor.exe"
    if (-not (Test-Path $ExePath)) {
        # Dev fallback: published shell under deploy_windows
        $dev = Join-Path $PackageRoot "deploy_windows\CcsPdfEditor.Shell\bin\Release\net8.0-windows\win-x64\CcsPdfEditor.exe"
        if (Test-Path $dev) { $ExePath = $dev }
    }
}
if (-not (Test-Path -LiteralPath $ExePath)) {
    throw "CcsPdfEditor.exe not found. Build with scripts\build-windows.ps1 first. Looked for: $ExePath"
}
$ExePath = (Resolve-Path -LiteralPath $ExePath).Path

# Use the EXE itself so Explorer shows the embedded ApplicationIcon
$icon = $ExePath

$progIdProject = "CCS.PdfEditor.ccspdf"
$progIdPdf = "CCS.PdfEditor.pdf"
$cmd = "`"$ExePath`" `"%1`""

function Set-ProgId {
    param([string]$ProgId, [string]$FriendlyName, [string]$IconPath)
    $base = "HKCU:\Software\Classes\$ProgId"
    New-Item -Path $base -Force | Out-Null
    Set-ItemProperty -Path $base -Name "(default)" -Value $FriendlyName
    New-Item -Path "$base\DefaultIcon" -Force | Out-Null
    Set-ItemProperty -Path "$base\DefaultIcon" -Name "(default)" -Value "`"$IconPath`",0"
    New-Item -Path "$base\shell\open\command" -Force | Out-Null
    Set-ItemProperty -Path "$base\shell\open\command" -Name "(default)" -Value $cmd
}

Write-Host "Registering file types for:" -ForegroundColor Cyan
Write-Host "  $ExePath"

# --- .ccspdf : default handler (our project format) ---
Set-ProgId -ProgId $progIdProject -FriendlyName "CCS PDF Editor Project" -IconPath $icon
New-Item -Path "HKCU:\Software\Classes\.ccspdf" -Force | Out-Null
Set-ItemProperty -Path "HKCU:\Software\Classes\.ccspdf" -Name "(default)" -Value $progIdProject
New-Item -Path "HKCU:\Software\Classes\.ccspdf\OpenWithProgids" -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Classes\.ccspdf\OpenWithProgids" -Name $progIdProject -PropertyType String -Value "" -Force | Out-Null

# --- .pdf : Open with only (do NOT steal default from Edge/Acrobat) ---
Set-ProgId -ProgId $progIdPdf -FriendlyName "CCS PDF Editor" -IconPath $icon
New-Item -Path "HKCU:\Software\Classes\.pdf\OpenWithProgids" -Force | Out-Null
New-ItemProperty -Path "HKCU:\Software\Classes\.pdf\OpenWithProgids" -Name $progIdPdf -PropertyType String -Value "" -Force | Out-Null

# Application registration (Default Apps list)
$appKey = "HKCU:\Software\Classes\Applications\CcsPdfEditor.exe"
New-Item -Path "$appKey\shell\open\command" -Force | Out-Null
Set-ItemProperty -Path "$appKey\shell\open\command" -Name "(default)" -Value $cmd
New-Item -Path "$appKey\SupportedTypes" -Force | Out-Null
New-ItemProperty -Path "$appKey\SupportedTypes" -Name ".pdf" -PropertyType String -Value "" -Force | Out-Null
New-ItemProperty -Path "$appKey\SupportedTypes" -Name ".ccspdf" -PropertyType String -Value "" -Force | Out-Null

# Notify shell
Add-Type -Namespace Win32 -Name Native -MemberDefinition @'
[System.Runtime.InteropServices.DllImport("shell32.dll")]
public static extern void SHChangeNotify(int wEventId, uint uFlags, System.IntPtr dwItem1, System.IntPtr dwItem2);
'@ -ErrorAction SilentlyContinue
try { [Win32.Native]::SHChangeNotify(0x08000000, 0, [IntPtr]::Zero, [IntPtr]::Zero) } catch { }

Write-Host ""
Write-Host "Done." -ForegroundColor Green
Write-Host "  .ccspdf  -> default open with CCS PDF Editor"
Write-Host "  .pdf     -> listed under Open with (default app unchanged)"
Write-Host ""
Write-Host "Tip: Right-click a PDF -> Open with -> CCS PDF Editor"
Write-Host "To undo: run tools\Unregister File Types.bat"
