<?php
/**
 * ITSWEBER CMS – Feature-Items Section
 *
 * Card grid with icon, title, text and optional link per item.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 * @contact   kontakt@itsweber.de
 */

namespace App\Sections\Core;

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

class FeaturesSection extends AbstractSection
{
    public static function typeKey(): string    { return 'features'; }
    public static function label(): string      { return 'Feature-Items'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Karten-Grid mit Icon, Titel, Text und optionalem Link'; }

    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"/>'
             . '<rect x="13" y="13" width="9" height="8" rx="1.5"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $itemIcons  = $request->input('item_icon', []);
        $itemTitles = $request->input('item_title', []);
        $itemTexts  = $request->input('item_text', []);
        $itemUrls   = $request->input('item_url', []);
        $items = [];
        if (is_array($itemTitles)) {
            foreach ($itemTitles as $i => $itemTitle) {
                $t = trim($itemTitle);
                if ($t !== '') {
                    $items[] = [
                        'icon'  => trim($itemIcons[$i] ?? ''),
                        'title' => $t,
                        'text'  => trim($itemTexts[$i] ?? ''),
                        'url'   => trim($itemUrls[$i] ?? ''),
                    ];
                }
            }
        }

        $data = [
            'title'              => trim($request->input('title', '')),
            'subtitle'           => trim($request->input('subtitle', '')),
            'items'              => $items,
            'color_scheme'       => self::sanitizeToken(trim($request->input('color_scheme', '')), self::allowedColorScheme()),
            'text_align'         => self::sanitizeToken(trim($request->input('text_align', '')),   self::allowedSectionAlign()),
            'card_style'         => self::sanitizeToken(trim($request->input('card_style', '')),   self::allowedCardStyle()),
            'columns'            => self::sanitizeToken(trim($request->input('columns', '')),       self::allowedColumns()),
            'section_text_color' => self::sanitizeToken(trim($request->input('section_text_color', '')), self::allowedSectionTextColor()),
        ];

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

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