param(
    [switch]$SmokeTest,
    [switch]$Dashboard,
    [string]$File = ".\sample.log"
)

$ErrorActionPreference = 'Stop'
$target = if (Test-Path $File) { $File } else { Join-Path $PSScriptRoot 'sample.log' }
$raw = Get-Content -Path $target -Encoding UTF8

$entries = foreach ($line in $raw) {
    $parts = $line -split '\|', 3
    if ($parts.Count -eq 3) {
        [pscustomobject]@{
            Level   = $parts[0].Trim()
            Service = $parts[1].Trim()
            Message = $parts[2].Trim()
        }
    }
}

$total = @($entries).Count
$errorCount = @($entries | Where-Object { $_.Level -match '^(?i)ERROR$' }).Count
$warnCount = @($entries | Where-Object { $_.Level -match '^(?i)WARN$' }).Count

if ($SmokeTest) {
    [pscustomobject]@{
        app     = 'log-audit-console-rust'
        total   = $total
        error   = $errorCount
        warning = $warnCount
        mode    = 'powershell-fallback-viewer'
    } | ConvertTo-Json -Depth 4
    exit 0
}

if ($Dashboard) {
    $folder = Join-Path $PSScriptRoot 'data'
    if (!(Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null }
    $htmlPath = Join-Path $folder 'log-audit-dashboard.html'
    $rows = foreach ($item in $entries) {
        "<tr><td>$($item.Level)</td><td>$($item.Service)</td><td>$($item.Message)</td></tr>"
    }
    if (-not $rows) { $rows = "<tr><td colspan='3'>Tidak ada data log.</td></tr>" }

    $html = @"
<!doctype html>
<html lang="id">
<head>
  <meta charset="utf-8">
  <title>Log Audit Console Rust</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body { font-family: Segoe UI, Arial, sans-serif; margin: 0; background: #f8fafc; color: #0f172a; }
    header { padding: 22px 28px; background: linear-gradient(135deg, #111827, #7c3aed); color: white; }
    main { padding: 22px 28px; display: grid; gap: 18px; }
    .grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }
    .card { background: white; border: 1px solid #e2e8f0; border-radius: 16px; padding: 16px; box-shadow: 0 12px 24px rgba(15, 23, 42, 0.06); }
    .label { color: #475569; font-size: 13px; margin-bottom: 6px; }
    .value { font-size: 28px; font-weight: 700; }
    table { width: 100%; border-collapse: collapse; background: white; }
    th, td { padding: 10px 8px; text-align: left; border-bottom: 1px solid #e2e8f0; vertical-align: top; }
    th { color: #475569; font-weight: 600; }
    .note { color: #475569; font-size: 13px; }
  </style>
</head>
<body>
  <header>
    <h1>Log Audit Console Rust</h1>
    <p>Fallback dashboard agar demo tetap lancar walau linker Rust di Windows belum siap.</p>
  </header>
  <main>
    <section class="grid">
      <div class="card"><div class="label">Total Entry</div><div class="value">$total</div></div>
      <div class="card"><div class="label">Error</div><div class="value">$errorCount</div></div>
      <div class="card"><div class="label">Warning</div><div class="value">$warnCount</div></div>
    </section>
    <section class="card">
      <h2>Preview Log</h2>
      <table><thead><tr><th>Level</th><th>Service</th><th>Pesan</th></tr></thead><tbody>$($rows -join "`n")</tbody></table>
    </section>
    <p class="note">Tip presentasi: jelaskan berapa error kritis, service mana yang terdampak, dan tindakan mitigasi awal.</p>
  </main>
</body>
</html>
"@
    Set-Content -Path $htmlPath -Value $html -Encoding UTF8
    Write-Output $htmlPath
    exit 0
}

Write-Host "Log Audit Console Rust (Fallback Viewer)" -ForegroundColor Cyan
Write-Host "Total entry : $total"
Write-Host "Error       : $errorCount"
Write-Host "Warning     : $warnCount"
foreach ($item in $entries | Select-Object -First 5) {
    Write-Host "- [$($item.Level)] $($item.Service) -> $($item.Message)"
}
