/**
 * Lego 09 / Lego 04 — Performance HUD
 *
 * Floating, draggable, collapsible overlay showing live CPU/RAM for all
 * backend processes (NestJS, n8n, Ollama). Reads from SurveillanceService
 * signals. Renders regardless of active route — mounted in app.html root.
 *
 * CDK DragDrop: user can reposition the HUD anywhere on screen.
 * The zombie socket feeding this HUD is never closed (Lego 06).
 */

import { Component, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CdkDrag, CdkDragHandle } from '@angular/cdk/drag-drop';
import { SurveillanceService, MonitorPayload } from '../../services/surveillance.service';

@Component({
  selector: 'app-performance-hud',
  standalone: true,
  imports: [CommonModule, CdkDrag, CdkDragHandle],
  templateUrl: './performance-hud.component.html',
  styleUrls: ['./performance-hud.component.scss'],
})
export class PerformanceHudComponent {
  collapsed = signal<boolean>(false);

  constructor(public surveillance: SurveillanceService) {}

  toggle(): void {
    this.collapsed.update(v => !v);
  }

  formatBytes(bytes: number): string {
    return this.surveillance.formatBytes(bytes);
  }

  cpuBarWidth(cpu: number): string {
    return `${Math.min(cpu, 100).toFixed(0)}%`;
  }

  cpuColor(cpu: number): string {
    if (cpu >= 80) return '#dc3545';
    if (cpu >= 50) return '#ffc107';
    return '#00a651';
  }

  trackByService(_: number, m: MonitorPayload): string {
    return m.service;
  }
}
