# VideoD - tek tikla kurulum (paket surumu)
$ErrorActionPreference = "Stop"
$root  = $PSScriptRoot
$extId = "ebmnplmojihclcmmgadpoloaeokgacif"
$hostName = "com.videod.host"

$hostExe = Join-Path $root "app\VideoDownloader.NativeHost.exe"
$appExe  = Join-Path $root "app\VideoDownloader.App.exe"
if (-not (Test-Path $hostExe)) { throw "app\VideoDownloader.NativeHost.exe bulunamadi." }

Write-Host "[1/2] Araclar indiriliyor (yt-dlp + ffmpeg)..." -ForegroundColor Cyan
& (Join-Path $root "setup-tools.ps1")

Write-Host ""
Write-Host "[2/2] Chrome/Edge kaydi yapiliyor..." -ForegroundColor Cyan
[IO.File]::WriteAllText((Join-Path $root "app\apppath.txt"), $appExe, (New-Object Text.UTF8Encoding($false)))

$manifestPath = Join-Path $root "app\$hostName.json"
$manifest = [ordered]@{
    name            = $hostName
    description     = "VideoD video indirici koprusu"
    path            = $hostExe
    type            = "stdio"
    allowed_origins = @("chrome-extension://$extId/")
} | ConvertTo-Json
[IO.File]::WriteAllText($manifestPath, $manifest, (New-Object Text.UTF8Encoding($false)))

foreach ($b in @("HKCU:\Software\Google\Chrome\NativeMessagingHosts",
                 "HKCU:\Software\Microsoft\Edge\NativeMessagingHosts")) {
    $key = "$b\$hostName"
    New-Item -Path $key -Force | Out-Null
    Set-ItemProperty -Path $key -Name "(default)" -Value $manifestPath
}

Write-Host ""
Write-Host "Kurulum tamamlandi." -ForegroundColor Green
Write-Host ""
Write-Host "SON ADIM - eklentiyi Chrome'a yukleyin:" -ForegroundColor Yellow
Write-Host "  1. VideoD-Chrome-Eklentisi.zip dosyasini bir klasore cikarin"
Write-Host "  2. Chrome'da chrome://extensions adresini acin"
Write-Host "  3. Sag ustten 'Gelistirici modu'nu acin"
Write-Host "  4. 'Paketlenmemis oge yukle' -> cikardiginiz klasoru secin"
Write-Host ""
Write-Host "Ayrintili anlatim: KURULUM.md" -ForegroundColor Cyan