<?php

namespace Core\System;

/**
 * Very small, safe Editor.js renderer (server-side).
 * Supports: paragraph, header, list, quote, delimiter, image
 */
class EditorJsRenderer
{
    public static function render(array $data): string
    {
        $blocks = $data['blocks'] ?? [];
        if (!is_array($blocks)) return '';

        $html = '';

        foreach ($blocks as $block) {
            if (!is_array($block)) continue;

            $type = $block['type'] ?? '';
            $d    = $block['data'] ?? [];

            switch ($type) {

                case 'paragraph':
                    $text = self::cleanText($d['text'] ?? '');
                    if ($text !== '') {
                        $html .= '<p>' . $text . '</p>';
                    }
                    break;

                case 'header':
                    $level = (int)($d['level'] ?? 3);
                    if ($level < 2 || $level > 5) $level = 3;
                    $text = self::cleanText($d['text'] ?? '');
                    if ($text !== '') {
                        $html .= '<h' . $level . '>' . $text . '</h' . $level . '>';
                    }
                    break;

                case 'list':
                    $style = ($d['style'] ?? 'unordered') === 'ordered' ? 'ol' : 'ul';
                    $items = $d['items'] ?? [];
                    if (is_array($items) && !empty($items)) {
                        $html .= '<' . $style . '>';
                        foreach ($items as $it) {
                            $html .= '<li>' . self::cleanText($it) . '</li>';
                        }
                        $html .= '</' . $style . '>';
                    }
                    break;

                case 'quote':
                    $text = self::cleanText($d['text'] ?? '');
                    $cap  = self::cleanText($d['caption'] ?? '');
                    if ($text !== '') {
                        $html .= '<blockquote><p>' . $text . '</p>';
                        if ($cap !== '') $html .= '<footer>' . $cap . '</footer>';
                        $html .= '</blockquote>';
                    }
                    break;

                case 'delimiter':
                    $html .= '<hr>';
                    break;

                case 'image':
                    $file = $d['file']['url'] ?? ($d['url'] ?? '');
                    $cap  = self::cleanText($d['caption'] ?? '');
                    if ($file) {
                        $src = htmlspecialchars($file, ENT_QUOTES, 'UTF-8');
                        $html .= '<figure class="editorjs-image">';
                        $html .= '<img src="' . $src . '" alt="">';
                        if ($cap !== '') $html .= '<figcaption>' . $cap . '</figcaption>';
                        $html .= '</figure>';
                    }
                    break;

                default:
                    // ignore unsupported blocks safely
                    break;
            }
        }

        return $html;
    }

    private static function cleanText($text): string
    {
        $text = (string)$text;
        $text = trim($text);
        if ($text === '') return '';

        // Allow very limited inline tags from Editor.js (bold, italic, links)
        $allowed = '<b><strong><i><em><a><br>';
        $text = strip_tags($text, $allowed);

        return $text;
    }
}
