<?php
/**
 * ITSWEBER CMS – Service Cards Section
 *
 * Cards with SVG icon, title, text and optional price.
 * Migriert aus plugins/service-cards-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 ServiceCardsSection extends AbstractSection
{
    public static function typeKey(): string    { return 'service_cards'; }
    public static function label(): string      { return 'Service-Karten'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Dienstleistungs-Karten mit Icon, Text und optionalem Preis'; }

    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="20" height="18" rx="2"/>'
             . '<line x1="6" y1="8" x2="18" y2="8"/>'
             . '<line x1="6" y1="12" x2="14" y2="12"/>'
             . '<line x1="6" y1="16" x2="10" y2="16"/>'
             . '<circle cx="17" cy="15" r="2"/>'
             . '</svg>';
    }

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

        $cardTitles = $request->input('card_title', []);
        $cardTexts  = $request->input('card_text', []);
        $cardPrices = $request->input('card_price', []);
        $cardIcons  = $request->input('card_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] ?? '')),
                    'price' => trim((string) ($cardPrices[$i] ?? '')),
                    'icon'  => $rawIcon,
                ];
            }
        }
        $cards = array_slice($cards, 0, 12);

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

        $data = [
            'title'              => trim((string) ($request->input('title', '') ?? '')),
            'subtitle'           => trim((string) ($request->input('subtitle', '') ?? '')),
            'description'        => trim((string) ($request->input('description', '') ?? '')),
            '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' => '', 'description' => '',
            'color_scheme' => '', 'section_text_color' => '',
            'columns' => '2', 'cards' => [],
        ];
    }

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