<?php
/**
 * ITSWEBER CMS – Trust / USP Showcase Section
 *
 * Trust/USP cards with SVG icon, title and text.
 * Migriert aus plugins/trust-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 TrustShowcaseSection extends AbstractSection
{
    public static function typeKey(): string    { return 'trust_showcase'; }
    public static function label(): string      { return 'Trust / USP'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Vertrauens- und USP-Karten mit SVG-Icon'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>'
             . '</svg>';
    }

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

        $cardTitles = $request->input('tsc_title', []);
        $cardTexts  = $request->input('tsc_text', []);
        $cardIcons  = $request->input('tsc_icon', []);

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

                // Validate & sanitize SVG icon
                $rawIcon = (string) ($cardIcons[$i] ?? '');
                if ($rawIcon !== '' && preg_match('/<svg[^>]*>.*?<\/svg>/is', $rawIcon, $match)) {
                    $rawIcon = $match[0];
                    // Remove event handlers and script tags
                    $rawIcon = preg_replace('/\s+on\w+\s*=\s*["\'][^"\']*["\']/i', '', $rawIcon);
                    $rawIcon = preg_replace('/<script\b[^>]*>.*?<\/script>/is', '', $rawIcon);
                } else {
                    $rawIcon = '';
                }

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

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

        $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,
            'cards'              => $cards,
        ];

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

    public function defaultData(): array
    {
        return [
            'title' => '', 'subtitle' => '',
            'color_scheme' => '', 'section_text_color' => '',
            'columns' => '4', 'cards' => [],
        ];
    }

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