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

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest

$proj = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $proj | Out-Null

$json = & powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $proj 'demo.ps1')
$data = $json | ConvertFrom-Json

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Incident Console Elixir'
$form.StartPosition = 'CenterScreen'
$form.Size = New-Object System.Drawing.Size(980, 680)
$form.MinimumSize = New-Object System.Drawing.Size(900, 620)
$form.BackColor = [System.Drawing.Color]::FromArgb(245,248,252)

$title = New-Object System.Windows.Forms.Label
$title.Text = 'Incident Console Elixir'
$title.Font = New-Object System.Drawing.Font('Segoe UI', 20, [System.Drawing.FontStyle]::Bold)
$title.AutoSize = $true
$title.Location = New-Object System.Drawing.Point(20, 16)
$form.Controls.Add($title)

$sub = New-Object System.Windows.Forms.Label
$sub.Text = 'Aplikasi insiden ringan dengan data status, owner, dan tindak lanjut.'
$sub.Font = New-Object System.Drawing.Font('Segoe UI', 10)
$sub.AutoSize = $false
$sub.Size = New-Object System.Drawing.Size(920, 40)
$sub.Location = New-Object System.Drawing.Point(22, 56)
$sub.ForeColor = [System.Drawing.Color]::FromArgb(71,85,105)
$form.Controls.Add($sub)

$stack = New-Object System.Windows.Forms.Label
$stack.Text = 'Stack: Elixir Script, Native Source, PowerShell WinForms App'
$stack.Font = New-Object System.Drawing.Font('Segoe UI', 9, [System.Drawing.FontStyle]::Bold)
$stack.AutoSize = $false
$stack.Size = New-Object System.Drawing.Size(920, 22)
$stack.Location = New-Object System.Drawing.Point(22, 96)
$stack.ForeColor = [System.Drawing.Color]::FromArgb(37,99,235)
$form.Controls.Add($stack)

$leftBox = New-Object System.Windows.Forms.GroupBox
$leftBox.Text = 'Ringkasan'
$leftBox.Location = New-Object System.Drawing.Point(20, 130)
$leftBox.Size = New-Object System.Drawing.Size(300, 460)
$form.Controls.Add($leftBox)

$summaryGrid = New-Object System.Windows.Forms.DataGridView
$summaryGrid.Dock = 'Fill'
$summaryGrid.ReadOnly = $true
$summaryGrid.AllowUserToAddRows = $false
$summaryGrid.AllowUserToDeleteRows = $false
$summaryGrid.RowHeadersVisible = $false
$summaryGrid.AutoSizeColumnsMode = 'Fill'
[void]$summaryGrid.Columns.Add('Metric','Metrik')
[void]$summaryGrid.Columns.Add('Value','Nilai')
foreach ($prop in $data.summary.PSObject.Properties) {
    [void]$summaryGrid.Rows.Add($prop.Name, [string]$prop.Value)
}
$leftBox.Controls.Add($summaryGrid)

$rightBox = New-Object System.Windows.Forms.GroupBox
$rightBox.Text = 'Data Demo'
$rightBox.Location = New-Object System.Drawing.Point(340, 130)
$rightBox.Size = New-Object System.Drawing.Size(620, 300)
$form.Controls.Add($rightBox)

$itemsGrid = New-Object System.Windows.Forms.DataGridView
$itemsGrid.Dock = 'Fill'
$itemsGrid.ReadOnly = $true
$itemsGrid.AllowUserToAddRows = $false
$itemsGrid.AllowUserToDeleteRows = $false
$itemsGrid.RowHeadersVisible = $false
$itemsGrid.AutoSizeColumnsMode = 'Fill'
if ($data.items.Count -gt 0) {
    $first = $data.items[0]
    foreach ($prop in $first.PSObject.Properties) {
        [void]$itemsGrid.Columns.Add($prop.Name, $prop.Name)
    }
    foreach ($row in $data.items) {
        $vals = @()
        foreach ($prop in $row.PSObject.Properties) { $vals += [string]$prop.Value }
        [void]$itemsGrid.Rows.Add($vals)
    }
}
$rightBox.Controls.Add($itemsGrid)

$notesBox = New-Object System.Windows.Forms.GroupBox
$notesBox.Text = 'Catatan & JSON'
$notesBox.Location = New-Object System.Drawing.Point(340, 440)
$notesBox.Size = New-Object System.Drawing.Size(620, 150)
$form.Controls.Add($notesBox)

$txt = New-Object System.Windows.Forms.TextBox
$txt.Multiline = $true
$txt.ScrollBars = 'Vertical'
$txt.ReadOnly = $true
$txt.Dock = 'Fill'
$txt.Font = New-Object System.Drawing.Font('Consolas', 9)
$notesText = ($data.notes -join [Environment]::NewLine) + [Environment]::NewLine + [Environment]::NewLine + ($json.Trim())
$txt.Text = $notesText
$notesBox.Controls.Add($txt)

$btnRefresh = New-Object System.Windows.Forms.Button
$btnRefresh.Text = 'Muat Ulang'
$btnRefresh.Size = New-Object System.Drawing.Size(120, 34)
$btnRefresh.Location = New-Object System.Drawing.Point(20, 602)
$btnRefresh.Add_Click({
    $form.Close()
    & powershell -NoProfile -ExecutionPolicy Bypass -File (Join-Path $proj 'app.ps1')
})
$form.Controls.Add($btnRefresh)

$btnCopy = New-Object System.Windows.Forms.Button
$btnCopy.Text = 'Salin JSON'
$btnCopy.Size = New-Object System.Drawing.Size(120, 34)
$btnCopy.Location = New-Object System.Drawing.Point(150, 602)
$btnCopy.Add_Click({
    [System.Windows.Forms.MessageBox]::Show('JSON berhasil disalin.', 'Incident Console Elixir') | Out-Null
    [System.Windows.Forms.Clipboard]::SetText($json)
})
$form.Controls.Add($btnCopy)

$btnClose = New-Object System.Windows.Forms.Button
$btnClose.Text = 'Tutup'
$btnClose.Size = New-Object System.Drawing.Size(120, 34)
$btnClose.Location = New-Object System.Drawing.Point(840, 602)
$btnClose.Add_Click({ $form.Close() })
$form.Controls.Add($btnClose)

[void]$form.ShowDialog()
