<?php
/**
 * ITSWEBER CMS – Feature Showcase Section
 *
 * Card grid with SVG icons, title, text and optional link per card.
 * Migriert aus plugins/feature-showcase-section.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 * @contact   kontakt@itsweber.de
 */

namespace App\Sections\Extended;

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

class FeatureShowcaseSection extends AbstractSection
{
    public static function typeKey(): string    { return 'feature_showcase'; }
    public static function label(): string      { return 'Feature Showcase'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Karten-Grid mit SVG-Icons und optionalen Links'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<rect x="2" y="3" width="9" height="8" rx="1.5"/>'
             . '<rect x="13" y="3" width="9" height="8" rx="1.5"/>'
             . '<rect x="2" y="13" width="9" height="8" rx="1.5"/>'
             . '<path d="M16 16l2 2 4-4"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $allowedCardStyle = ['', 'bordered', 'flat', 'shadow'];
        $allowedColumns   = ['2', '3', '4'];

        $cardTitles    = $request->input('fsc_title', []);
        $cardTexts     = $request->input('fsc_text', []);
        $cardUrls      = $request->input('fsc_url', []);
        $cardIcons     = $request->input('fsc_icon', []);
        $cardLinkTexts = $request->input('fsc_link_text', []);

        $cards = [];
        if (is_array($cardTitles)) {
            foreach ($cardTitles as $i => $cardTitle) {
                $t = trim((string) $cardTitle);
                if ($t === '') {
                    continue;
                }

                // Validate & sanitize SVG icon via whitelist sanitizer (H5).
                $rawIcon = (string) ($cardIcons[$i] ?? '');
                $rawIcon = $rawIcon !== ''
                    ? (\App\Services\Security\SvgSanitizer::sanitize($rawIcon) ?? '')
                    : '';

                // Validate URL
                $url = trim((string) ($cardUrls[$i] ?? ''));
                if ($url !== '' && !preg_match('#^(https?://|/)#i', $url)) {
                    $url = '';
                }

                $cards[] = [
                    'title'     => $t,
                    'text'      => trim((string) ($cardTexts[$i] ?? '')),
                    'url'       => $url,
                    'icon'      => $rawIcon,
                    'link_text' => trim((string) ($cardLinkTexts[$i] ?? '')),
                ];
            }
        }
        $cards = array_slice($cards, 0, 12);

        $columns = (string) ($request->input('columns', '3') ?? '3');
        if (!in_array($columns, $allowedColumns, true)) {
            $columns = '3';
        }

        $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()),
            'columns'            => $columns,
            'card_style'         => self::sanitizeToken((string) ($request->input('card_style', '') ?? ''), $allowedCardStyle),
            'cards'              => $cards,
        ];

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

    public function defaultData(): array
    {
        return [
            'title' => 'Leistungen im Detail',
            'subtitle' => 'Was wir fuer Sie tun koennen',
            'color_scheme' => '', 'section_text_color' => '',
            'columns' => '3', 'card_style' => '',
            'cards' => [
                ['title' => 'Webdesign', 'text' => 'Moderne und responsive Webseiten, die begeistern und konvertieren.', 'url' => '', 'icon' => '', 'link_text' => ''],
                ['title' => 'Entwicklung', 'text' => 'Massgeschneiderte Softwareloesungen fuer Ihre individuellen Anforderungen.', 'url' => '', 'icon' => '', 'link_text' => ''],
                ['title' => 'Marketing', 'text' => 'Strategien die Ihre Reichweite erhoehen und neue Kunden gewinnen.', 'url' => '', 'icon' => '', 'link_text' => ''],
            ],
        ];
    }

    public function styles(): array
    {
        return ['/assets/css/sections/feature-showcase-section.css'];
    }
}
