<?php
/**
 * ITSWEBER CMS – Code Showcase Section (Terminal Theme)
 *
 * Multi-tab code display with syntax highlighting and line numbers.
 * Theme-bound: only available when the Terminal theme is active.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 */

namespace App\Sections\Theme\Terminal;

use App\Sections\AbstractSection;
use App\Core\Request;

class CodeShowcaseSection extends AbstractSection
{
    public static function typeKey(): string    { return 'code_showcase'; }
    public static function label(): string      { return 'Code Showcase'; }
    public static function category(): string   { return 'content'; }
    public static function themeSlug(): ?string { return 'terminal'; }
    public static function description(): string { return 'Tabbed code display with syntax highlighting'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $filenames  = $request->input('tab_filename', []);
        $languages  = $request->input('tab_language', []);
        $codes      = $request->input('tab_code', []);

        $tabs = [];
        if (is_array($filenames)) {
            foreach ($filenames as $i => $fn) {
                $f = trim((string) $fn);
                if ($f === '') continue;

                $tabs[] = [
                    'filename' => $f,
                    'language' => self::sanitizeToken(
                        trim((string) ($languages[$i] ?? '')),
                        ['', 'css', 'php', 'json', 'js', 'html', 'sql', 'bash', 'python']
                    ),
                    'code'     => (string) ($codes[$i] ?? ''),
                ];
            }
        }
        $tabs = array_slice($tabs, 0, 5);

        $data = [
            'title'              => trim((string) ($request->input('title', '') ?? '')),
            'subtitle'           => trim((string) ($request->input('subtitle', '') ?? '')),
            'color_scheme'       => self::sanitizeToken((string) ($request->input('color_scheme', '') ?? ''), self::allowedColorScheme()),
            'section_text_color' => self::sanitizeToken((string) ($request->input('section_text_color', '') ?? ''), self::allowedSectionTextColor()),
            'tabs'               => $tabs,
        ];

        return $this->mergeThemeExtras($data, $request);
    }

    public function defaultData(): array
    {
        return [
            'title'    => 'Code Showcase',
            'subtitle' => 'Sauberer Code, durchdachte Architektur.',
            'color_scheme' => '', 'section_text_color' => '',
            'tabs' => [
                ['filename' => 'app.php',    'language' => 'php',        'code' => "<?php\n\nnamespace App\\Core;\n\nclass App\n{\n    private static ?self \$instance = null;\n\n    public static function getInstance(): self\n    {\n        return self::\$instance ??= new self();\n    }\n}"],
                ['filename' => 'style.css',  'language' => 'css',        'code' => ":root {\n    --color-primary: #00ff88;\n    --color-surface: #0a0a0a;\n    --font-mono: 'JetBrains Mono', monospace;\n}\n\n.terminal {\n    background: var(--color-surface);\n    color: var(--color-primary);\n    font-family: var(--font-mono);\n}"],
                ['filename' => 'config.js',  'language' => 'javascript', 'code' => "export default {\n    name: 'ITSWEBER CMS',\n    version: '3.4.0',\n    features: {\n        darkMode: true,\n        pageBuilder: true,\n        themeSystem: true,\n    },\n};"],
            ],
        ];
    }
}
