$ErrorActionPreference = "SilentlyContinue"

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$dataRoot = Join-Path $env:ProgramData "FashionAIDesignStudio"
$statusPath = Join-Path $dataRoot "runtime-status.json"
$logDir = Join-Path $dataRoot "logs"
$serviceName = "FashionAIDesignStudio"

function Get-StatusText {
  if (!(Test-Path $statusPath)) { return "Fashion AI Design Studio: status unknown" }
  $status = Get-Content $statusPath -Raw | ConvertFrom-Json
  $urls = ($status.lanUrls -join "`n")
  return "Status: $($status.status)`nPort: $($status.port)`n$urls"
}

$notify = New-Object System.Windows.Forms.NotifyIcon
$notify.Text = "Fashion AI Design Studio"
$notify.Icon = [System.Drawing.SystemIcons]::Application
$notify.Visible = $true

$menu = New-Object System.Windows.Forms.ContextMenuStrip
$openItem = $menu.Items.Add("Open Admin")
$statusItem = $menu.Items.Add("Show Status")
$restartItem = $menu.Items.Add("Restart Service")
$logsItem = $menu.Items.Add("Open Logs")
$exitItem = $menu.Items.Add("Exit Tray")

$openItem.add_Click({
  if (Test-Path $statusPath) {
    $status = Get-Content $statusPath -Raw | ConvertFrom-Json
    Start-Process "http://127.0.0.1:$($status.port)"
  }
})
$statusItem.add_Click({ [System.Windows.Forms.MessageBox]::Show((Get-StatusText), "Fashion AI Design Studio") | Out-Null })
$restartItem.add_Click({ Restart-Service -Name $serviceName })
$logsItem.add_Click({ New-Item -ItemType Directory -Path $logDir -Force | Out-Null; Start-Process $logDir })
$exitItem.add_Click({ $notify.Visible = $false; [System.Windows.Forms.Application]::Exit() })

$notify.ContextMenuStrip = $menu
[System.Windows.Forms.Application]::Run()

