<?php

declare(strict_types=1);

namespace App\Framework\Process\Console;

use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\ConsoleInput;
use App\Framework\Console\ConsoleOutput;
use App\Framework\Console\ExitCode;
use App\Framework\Process\Services\SystemInfoService;

/**
 * Zeigt System-Informationen an.
 */
final readonly class SystemInfoCommand
{
    public function __construct(
        private SystemInfoService $systemInfo
    ) {
    }

    #[ConsoleCommand('system:info', 'Display system information (uptime, load, memory, disk, CPU)')]
    public function execute(ConsoleInput $input, ConsoleOutput $output): int
    {
        $info = ($this->systemInfo)();

        echo "╔════════════════════════════════════════════════════════════╗\n";
        echo "║                   SYSTEM INFORMATION                       ║\n";
        echo "╚════════════════════════════════════════════════════════════╝\n\n";

        // Uptime
        echo "┌─ UPTIME ────────────────────────────────────────────────┐\n";
        echo "│ Boot Time:  {$info->uptime->getBootTimeFormatted()}\n";
        echo "│ Uptime:     {$info->uptime->uptime->toHumanReadable()} ({$info->uptime->getUptimeDays()} days)\n";
        echo "└─────────────────────────────────────────────────────────┘\n\n";

        // Load Average
        echo "┌─ LOAD AVERAGE ──────────────────────────────────────────┐\n";
        $util = round($info->load->getUtilization($info->cpu->cores) * 100, 1);
        echo "│ 1 min:      {$info->load->oneMinute}\n";
        echo "│ 5 min:      {$info->load->fiveMinutes}\n";
        echo "│ 15 min:     {$info->load->fifteenMinutes}\n";
        echo "│ CPU Usage:  {$util}%\n";
        echo "└─────────────────────────────────────────────────────────┘\n\n";

        // CPU
        echo "┌─ CPU ───────────────────────────────────────────────────┐\n";
        echo "│ Cores:      {$info->cpu->cores}\n";
        echo "│ Model:      {$info->cpu->getShortModel()}\n";
        echo "└─────────────────────────────────────────────────────────┘\n\n";

        // Memory
        echo "┌─ MEMORY ────────────────────────────────────────────────┐\n";
        echo "│ Total:      {$info->memory->getTotal()->toHumanReadable()}\n";
        echo "│ Used:       {$info->memory->getUsed()->toHumanReadable()} ({$info->memory->getUsagePercentage()}%)\n";
        echo "│ Free:       {$info->memory->getFree()->toHumanReadable()}\n";
        echo "│ Available:  {$info->memory->getAvailable()->toHumanReadable()}\n";
        echo "└─────────────────────────────────────────────────────────┘\n\n";

        // Disk
        echo "┌─ DISK ({$info->disk->mountPoint}) ─────────────────────────────────────────────┐\n";
        echo "│ Total:      {$info->disk->getTotal()->toHumanReadable()}\n";
        echo "│ Used:       {$info->disk->getUsed()->toHumanReadable()} ({$info->disk->getUsagePercentage()}%)\n";
        echo "│ Available:  {$info->disk->getAvailable()->toHumanReadable()}\n";

        if ($info->disk->isAlmostFull()) {
            echo "│ ⚠️  WARNING: Disk is almost full!\n";
        }

        echo "└─────────────────────────────────────────────────────────┘\n\n";

        // Processes
        echo "┌─ PROCESSES ─────────────────────────────────────────────┐\n";
        echo "│ Total:      {$info->processes->total}\n";
        echo "│ Running:    {$info->processes->running}\n";
        echo "│ Sleeping:   {$info->processes->sleeping}\n";
        echo "│ Other:      {$info->processes->getOther()}\n";
        echo "└─────────────────────────────────────────────────────────┘\n";

        return ExitCode::SUCCESS->value;
    }
}
