#!/usr/bin/env bash
# linuxlite-bench: benchmark and kernel profile recommendation tool
# Usage: linuxlite-bench [--profile desktop|gaming] [--compare]
set -euo pipefail

LOGDIR="${XDG_DATA_HOME:-$HOME/.local/share}/linuxlite"
mkdir -p "$LOGDIR"
LOG="$LOGDIR/bench-$(uname -r).log"

echo "Lite Kernel Manager - Benchmark"
echo "Kernel: $(uname -r)" | tee "$LOG"
echo "" | tee -a "$LOG"

# Scheduler latency via cyclictest (install rt-tests for this)
LATENCY=999
if command -v cyclictest &>/dev/null; then
  LATENCY=$(sudo cyclictest --mlockall -t1 -p80 -n -i250 -l10000 --quiet 2>/dev/null | \
    awk '/Max:/ {print $NF}' | head -1 || echo 999)
  echo "Scheduler latency (max):  ${LATENCY}us" | tee -a "$LOG"
else
  echo "Scheduler latency:  (install rt-tests for measurement)" | tee -a "$LOG"
fi

# GPU throughput via glmark2
FPS=0
if command -v glmark2 &>/dev/null; then
  FPS=$(glmark2 2>/dev/null | awk '/Score/ {print $NF}' | tail -1 || echo 0)
  echo "glmark2 score:            $FPS" | tee -a "$LOG"
else
  echo "glmark2:  not found" | tee -a "$LOG"
fi

# Profile recommendation
echo "" | tee -a "$LOG"
REC="linuxlite"
CONFIDENCE="medium"
if [ "${LATENCY}" -le 500 ] && [ "${FPS}" -gt 120 ]; then
  REC="linuxlite-gaming"
  CONFIDENCE="high"
elif [ "${FPS}" -gt 120 ]; then
  REC="linuxlite-gaming"
  CONFIDENCE="medium"
fi

echo "Recommended profile:  $REC" | tee -a "$LOG"
echo "Confidence:           $CONFIDENCE" | tee -a "$LOG"

cat > "$LOGDIR/recommendation.json" <<JSON
{
  "kernel":      "$(uname -r)",
  "recommended": "$REC",
  "confidence":  "$CONFIDENCE",
  "fps":         $FPS,
  "latency_us":  $LATENCY,
  "ts":          "$(date -Is)"
}
JSON

# Generate HTML benchmark report
REPORT_DATE=$(LC_ALL=C date '+%Y-%m-%d %H:%M')
REPORT_FILE="$HOME/lite-benchmark ${REPORT_DATE}.html"

# Rating badges
if [ "${FPS}" -gt 200 ]; then
  FPS_RATING="Excellent"; FPS_COLOR="#22c55e"
elif [ "${FPS}" -gt 120 ]; then
  FPS_RATING="Good"; FPS_COLOR="#3b82f6"
elif [ "${FPS}" -gt 60 ]; then
  FPS_RATING="Fair"; FPS_COLOR="#f59e0b"
else
  FPS_RATING="Low"; FPS_COLOR="#ef4444"
fi

if [ "${LATENCY}" -le 100 ]; then
  LAT_RATING="Excellent"; LAT_COLOR="#22c55e"
elif [ "${LATENCY}" -le 500 ]; then
  LAT_RATING="Good"; LAT_COLOR="#3b82f6"
elif [ "${LATENCY}" -le 2000 ]; then
  LAT_RATING="Fair"; LAT_COLOR="#f59e0b"
else
  LAT_RATING="Poor"; LAT_COLOR="#ef4444"
fi

if [ "$REC" = "linuxlite-gaming" ]; then
  REC_ICON="&#127918;"; REC_LABEL="Linux Lite Gaming"; REC_COLOR="#7c3aed"
else
  REC_ICON="&#128187;"; REC_LABEL="Linux Lite Desktop"; REC_COLOR="#0ea5e9"
fi

cat > "$REPORT_FILE" <<HTMLEOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lite Benchmark Report - ${REPORT_DATE}</title>
<style>
  * { margin: 0; padding: 0; box-sizing: border-box; }
  body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: #0f172a;
    color: #e2e8f0;
    min-height: 100vh;
    padding: 2rem;
  }
  .container { max-width: 720px; margin: 0 auto; }
  .header {
    text-align: center;
    margin-bottom: 2rem;
    padding: 2rem;
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
    border-radius: 16px;
    border: 1px solid #475569;
  }
  .header h1 {
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    background: linear-gradient(90deg, #60a5fa, #a78bfa);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }
  .header .date { color: #94a3b8; font-size: 0.9rem; }
  .header .kernel {
    display: inline-block;
    margin-top: 0.75rem;
    padding: 0.3rem 1rem;
    background: #1e293b;
    border: 1px solid #475569;
    border-radius: 20px;
    font-family: monospace;
    font-size: 0.85rem;
    color: #cbd5e1;
  }
  .cards { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1.5rem; }
  .card {
    background: #1e293b;
    border-radius: 12px;
    padding: 1.5rem;
    border: 1px solid #334155;
    transition: transform 0.2s;
  }
  .card:hover { transform: translateY(-2px); }
  .card .label { font-size: 0.8rem; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.05em; }
  .card .value { font-size: 2.2rem; font-weight: 700; margin: 0.5rem 0; }
  .card .unit { font-size: 0.9rem; color: #64748b; }
  .badge {
    display: inline-block;
    padding: 0.2rem 0.7rem;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 600;
    color: #fff;
  }
  .recommendation {
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
    border-radius: 12px;
    padding: 1.5rem;
    border: 1px solid #475569;
    text-align: center;
    margin-bottom: 1.5rem;
  }
  .recommendation .icon { font-size: 2.5rem; margin-bottom: 0.5rem; }
  .recommendation .rec-label {
    font-size: 1.3rem;
    font-weight: 700;
    margin-bottom: 0.3rem;
  }
  .recommendation .confidence { color: #94a3b8; font-size: 0.85rem; }
  .bar-container {
    background: #1e293b;
    border-radius: 12px;
    padding: 1.5rem;
    border: 1px solid #334155;
    margin-bottom: 1.5rem;
  }
  .bar-label { display: flex; justify-content: space-between; margin-bottom: 0.5rem; font-size: 0.85rem; }
  .bar-track {
    width: 100%;
    height: 10px;
    background: #334155;
    border-radius: 5px;
    overflow: hidden;
    margin-bottom: 1rem;
  }
  .bar-fill {
    height: 100%;
    border-radius: 5px;
    transition: width 0.5s ease;
  }
  .footer {
    text-align: center;
    color: #475569;
    font-size: 0.75rem;
    padding-top: 1rem;
  }
</style>
</head>
<body>
<div class="container">
  <div class="header">
    <h1>Lite Benchmark Report</h1>
    <div class="date">${REPORT_DATE}</div>
    <div class="kernel">$(uname -r)</div>
  </div>

  <div class="cards">
    <div class="card">
      <div class="label">GPU Score</div>
      <div class="value" style="color: ${FPS_COLOR}">${FPS}</div>
      <div class="unit">glmark2 points</div>
      <span class="badge" style="background: ${FPS_COLOR}">${FPS_RATING}</span>
    </div>
    <div class="card">
      <div class="label">Scheduler Latency</div>
      <div class="value" style="color: ${LAT_COLOR}">${LATENCY}</div>
      <div class="unit">microseconds (max)</div>
      <span class="badge" style="background: ${LAT_COLOR}">${LAT_RATING}</span>
    </div>
  </div>

  <div class="bar-container">
    <div class="bar-label">
      <span>GPU Performance</span>
      <span>${FPS} / 300</span>
    </div>
    <div class="bar-track">
      <div class="bar-fill" style="width: $(( FPS > 300 ? 100 : FPS * 100 / 300 ))%; background: linear-gradient(90deg, #3b82f6, #22c55e);"></div>
    </div>
    <div class="bar-label">
      <span>Scheduler Responsiveness</span>
      <span>$(( LATENCY < 10 ? 100 : LATENCY < 5000 ? (5000 - LATENCY) * 100 / 5000 : 0 ))%</span>
    </div>
    <div class="bar-track">
      <div class="bar-fill" style="width: $(( LATENCY < 10 ? 100 : LATENCY < 5000 ? (5000 - LATENCY) * 100 / 5000 : 0 ))%; background: linear-gradient(90deg, #a78bfa, #22c55e);"></div>
    </div>
  </div>

  <div class="recommendation">
    <div class="icon">${REC_ICON}</div>
    <div class="rec-label" style="color: ${REC_COLOR}">Recommended: ${REC_LABEL}</div>
    <div class="confidence">Confidence: ${CONFIDENCE}</div>
  </div>

  <div class="footer">
    Generated by Lite Kernel Manager &mdash; Linux Lite
  </div>
</div>
</body>
</html>
HTMLEOF

echo ""
echo "Benchmark report saved to: $REPORT_FILE"
