<?php
/**
 * ITSWEBER CMS – Topic Cards Section
 *
 * Themed cards with icon, title, text and optional link.
 *
 * @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 TopicCardsSection extends AbstractSection
{
    public static function typeKey(): string    { return 'topic_cards'; }
    public static function label(): string      { return 'Themenkarten'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Karten 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="6" height="18" rx="1.5"/>'
             . '<rect x="9" y="3" width="6" height="18" rx="1.5"/>'
             . '<rect x="16" y="3" width="6" height="18" rx="1.5"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $tcTitles = $request->input('tc_title', []);
        $tcIcons  = $request->input('tc_icon', []);
        $tcTexts  = $request->input('tc_text', []);
        $tcUrls   = $request->input('tc_url', []);
        $tcItems  = [];
        if (is_array($tcTitles)) {
            foreach ($tcTitles as $i => $tcTitle) {
                $t = trim($tcTitle);
                if ($t !== '') {
                    $tcItems[] = [
                        'icon'  => trim($tcIcons[$i] ?? ''),
                        'title' => $t,
                        'text'  => trim($tcTexts[$i] ?? ''),
                        'url'   => trim($tcUrls[$i] ?? ''),
                    ];
                }
            }
        }

        $data = [
            'title'              => trim($request->input('title', '')),
            'subtitle'           => trim($request->input('subtitle', '')),
            'items'              => $tcItems,
            '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' => '',
        ];
    }
}
