<?php

declare(strict_types=1);

namespace App\Framework\Debug\Formatters;

use App\Framework\Debug\DebugEntry;
use App\Framework\Debug\EntryType;

/**
 * Formatiert Debug-Einträge für HTML-Ausgabe.
 */
final class HtmlFormatter implements FormatterInterface
{
    /** @var bool */
    private bool $stylesInjected = false;

    /**
     * {@inheritdoc}
     */
    public function format(DebugEntry $entry): string
    {
        $styles = $this->stylesInjected ? '' : $this->getStyles();
        $this->stylesInjected = true;

        $html = $styles;
        $html .= '<div class="debug-output">';
        $html .= '<div class="debug-header">';
        $html .= '<span class="debug-label">' . htmlspecialchars($entry->getLabel() ?: 'Debug') . '</span>';
        $html .= '<span class="debug-location">' . $entry->getFilename() . ':' . $entry->getLine() . '</span>';
        $html .= '<span class="debug-time">' . $entry->getFormattedTimestamp() . '</span>';
        $html .= '</div>';

        $html .= '<div class="debug-content">';
        $html .= '<pre>' . htmlspecialchars($this->formatValue($entry->getData())) . '</pre>';
        $html .= '</div>';

        if ($entry->getType() === EntryType::ARRAY && is_array($entry->getData())) {
            $data = $entry->getData();
            $html .= '<div class="debug-array-info">';
            $html .= '<small>Anzahl Elemente: ' . count($data) . '</small>';
            if (! empty($data)) {
                $html .= '<small> | Schlüssel: ' . htmlspecialchars(implode(', ', array_map('strval', array_keys($data)))) . '</small>';
            }
            $html .= '</div>';
        }

        $html .= '<div class="debug-footer">';
        $html .= '<small>Memory: ' . $entry->getMemory() . ' | Peak: ' . $entry->getPeakMemory() . '</small>';
        $html .= '</div>';
        $html .= '</div>';

        return $html;
    }

    /**
     * {@inheritdoc}
     */
    public function formatHistory(array $entries): string
    {
        $styles = $this->stylesInjected ? '' : $this->getStyles();
        $this->stylesInjected = true;

        $html = $styles;
        $html .= '<div class="debug-history">';
        $html .= '<h3>Debug Historie (' . count($entries) . ' Einträge)</h3>';
        $html .= '<div class="debug-history-entries">';

        foreach ($entries as $index => $entry) {
            $html .= '<div class="debug-history-entry">';
            $html .= '<div class="debug-history-header">';
            $html .= '<span class="debug-history-index">#' . ($index + 1) . '</span> ';
            $html .= '<span class="debug-history-label">' . htmlspecialchars($entry->getLabel()) . '</span> | ';
            $html .= '<span class="debug-history-file">' . $entry->getFilename() . ':' . $entry->getLine() . '</span> | ';
            $html .= '<span class="debug-history-time">' . $entry->getFormattedTimestamp() . '</span> | ';
            $html .= '<span class="debug-history-memory">' . $entry->getMemory() . '</span>';
            $html .= '</div>';
            $html .= '</div>';
        }

        $html .= '</div>';
        $html .= '</div>';

        return $html;
    }

    /**
     * {@inheritdoc}
     */
    public function formatValue(mixed $value): string
    {
        return print_r($value, true);
    }

    /**
     * Gibt die CSS-Styles zurück
     */
    private function getStyles(): string
    {
        return '<style>
        .debug-output {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            margin: 10px 0;
            font-family: "Courier New", monospace;
            font-size: 12px;
            color: #212529;
        }
        .debug-header {
            background: #343a40;
            color: white;
            padding: 8px 12px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }
        .debug-label {
            font-weight: bold;
            color: #28a745;
        }
        .debug-location {
            color: #ffc107;
        }
        .debug-time {
            color: #17a2b8;
        }
        .debug-content {
            padding: 12px;
            max-height: 400px;
            overflow-y: auto;
            background: #fff;
        }
        .debug-content pre {
            margin: 0;
            white-space: pre-wrap;
            word-wrap: break-word;
            font-size: 11px;
            line-height: 1.5;
        }
        .debug-array-info {
            background: #f1f3f5;
            padding: 4px 12px;
            border-top: 1px dashed #dee2e6;
            color: #6c757d;
        }
        .debug-footer {
            background: #e9ecef;
            padding: 4px 12px;
            border-top: 1px solid #dee2e6;
            color: #6c757d;
            border-bottom-left-radius: 4px;
            border-bottom-right-radius: 4px;
        }
        .debug-history {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            margin: 10px 0;
            padding: 10px;
            font-family: "Courier New", monospace;
        }
        .debug-history h3 {
            margin: 0 0 10px 0;
            color: #343a40;
            font-size: 14px;
        }
        .debug-history-entries {
            max-height: 500px;
            overflow-y: auto;
        }
        .debug-history-entry {
            margin-bottom: 4px;
            padding: 4px 8px;
            background: #fff;
            border-left: 3px solid #6c757d;
            font-size: 11px;
        }
        .debug-history-header {
            display: flex;
            flex-wrap: wrap;
            gap: 5px;
        }
        .debug-history-index {
            font-weight: bold;
            color: #007bff;
        }
        .debug-history-label {
            color: #28a745;
        }
        .debug-history-file {
            color: #fd7e14;
        }
        .debug-history-time {
            color: #6c757d;
        }
        .debug-history-memory {
            color: #6610f2;
        }
        </style>';
    }
}
