# Edge store submission bundle: extension zip + listing + assets + screenshots
# Usage: .\scripts\build-edge-submission.ps1 -ApiBase "https://api.example.com"

param(
  [Parameter(Mandatory = $true)]
  [string]$ApiBase
)

$ErrorActionPreference = "Stop"
$root = Resolve-Path (Join-Path $PSScriptRoot "..")
$manifestRaw = Get-Content (Join-Path $root "manifest.json") -Raw -Encoding UTF8
if ($manifestRaw -notmatch '"version"\s*:\s*"([^"]+)"') {
  throw "Cannot read version from manifest.json"
}
$version = $Matches[1]

& (Join-Path $PSScriptRoot "build-store.ps1") -ApiBase $ApiBase

$storeZip = Join-Path $root "dist/biliindex-$version-store.zip"
if (-not (Test-Path $storeZip)) {
  throw "Store zip not found: $storeZip"
}

$outRoot = Join-Path $root "dist/edge-submission"
if (Test-Path $outRoot) { Remove-Item $outRoot -Recurse -Force }
New-Item -ItemType Directory -Path $outRoot -Force | Out-Null

$extDir = Join-Path $outRoot "extension"
New-Item -ItemType Directory -Path $extDir -Force | Out-Null
Copy-Item $storeZip (Join-Path $extDir (Split-Path $storeZip -Leaf)) -Force

$listingSrc = Join-Path $root "store/edge/listing"
$listingDst = Join-Path $outRoot "listing"
Copy-Item $listingSrc $listingDst -Recurse -Force

Copy-Item (Join-Path $root "store/PRIVACY.md") (Join-Path $outRoot "PRIVACY.md") -Force
Copy-Item (Join-Path $root "store/edge/README_SUBMIT.md") (Join-Path $outRoot "README_SUBMIT.md") -Force

$assetsDir = Join-Path $outRoot "assets"
$shotsDir = Join-Path $outRoot "screenshots"
New-Item -ItemType Directory -Path $assetsDir -Force | Out-Null
New-Item -ItemType Directory -Path $shotsDir -Force | Out-Null

$icon128 = Join-Path $root "icons/icon128.png"
if (-not (Test-Path $icon128)) { throw "Missing icons/icon128.png" }

Add-Type -AssemblyName System.Drawing

function Resize-ImageFile {
  param(
    [string]$SourcePath,
    [string]$DestPath,
    [int]$Width,
    [int]$Height
  )
  $src = [System.Drawing.Image]::FromFile($SourcePath)
  try {
    $bmp = New-Object System.Drawing.Bitmap $Width, $Height
    $g = [System.Drawing.Graphics]::FromImage($bmp)
    $g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
    $g.Clear([System.Drawing.Color]::FromArgb(255, 255, 255, 255))
    $g.DrawImage($src, 0, 0, $Width, $Height)
    $g.Dispose()
    $bmp.Save($DestPath, [System.Drawing.Imaging.ImageFormat]::Png)
    $bmp.Dispose()
  } finally {
    $src.Dispose()
  }
}

Resize-ImageFile -SourcePath $icon128 -DestPath (Join-Path $assetsDir "logo-300.png") -Width 300 -Height 300
Copy-Item $icon128 (Join-Path $assetsDir "logo-128.png") -Force

Resize-ImageFile -SourcePath $icon128 -DestPath (Join-Path $assetsDir "promo-tile-440x280.png") -Width 440 -Height 280

$srcShots = Join-Path $root "store/edge/screenshots"
if (Test-Path $srcShots) {
  Get-ChildItem $srcShots -Filter "*.png" | ForEach-Object {
    $dest = Join-Path $shotsDir $_.Name
    Resize-ImageFile -SourcePath $_.FullName -DestPath $dest -Width 1280 -Height 800
  }
}
if (-not (Get-ChildItem $shotsDir -Filter "*.png" -ErrorAction SilentlyContinue)) {
  Resize-ImageFile -SourcePath $icon128 -DestPath (Join-Path $shotsDir "screenshot-placeholder.png") -Width 1280 -Height 800
  "Replace screenshots with real 1280x800 captures before Partner Center submit." | Set-Content (Join-Path $shotsDir "README.txt") -Encoding UTF8
}

$submissionZip = Join-Path $root "dist/biliindex-edge-submission-$version.zip"
if (Test-Path $submissionZip) { Remove-Item $submissionZip -Force }
Compress-Archive -Path (Join-Path $outRoot "*") -DestinationPath $submissionZip -Force

Write-Host ""
Write-Host "Edge submission ready."
Write-Host "Upload to Partner Center: $storeZip"
Write-Host "Full bundle: $submissionZip"
Write-Host "Folder: $outRoot"
