<?php
/**
 * ITSWEBER CMS – Highlight Grid Section
 *
 * Grid of highlight cards with icon, title, text, source and 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 HighlightGridSection extends AbstractSection
{
    public static function typeKey(): string    { return 'highlight_grid'; }
    public static function label(): string      { return 'Highlight-Grid'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Grid mit Highlight-Karten inkl. Quelle und 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"/>'
             . '<circle cx="5" cy="6" r="1" fill="currentColor"/>'
             . '<circle cx="16" cy="6" r="1" fill="currentColor"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $hgTitles  = $request->input('hg_title', []);
        $hgIcons   = $request->input('hg_icon', []);
        $hgTexts   = $request->input('hg_text', []);
        $hgSources = $request->input('hg_source', []);
        $hgUrls    = $request->input('hg_url', []);
        $hgItems   = [];
        if (is_array($hgTitles)) {
            foreach ($hgTitles as $i => $hgTitle) {
                $t = trim($hgTitle);
                if ($t !== '') {
                    $hgItems[] = [
                        'icon'   => trim($hgIcons[$i] ?? ''),
                        'title'  => $t,
                        'text'   => trim($hgTexts[$i] ?? ''),
                        'source' => trim($hgSources[$i] ?? ''),
                        'url'    => trim($hgUrls[$i] ?? ''),
                    ];
                }
            }
        }

        $data = [
            'title'              => trim($request->input('title', '')),
            'subtitle'           => trim($request->input('subtitle', '')),
            'items'              => $hgItems,
            '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'              => 'Highlights',
            'subtitle'           => 'Das Beste auf einen Blick',
            'items'              => [
                ['title' => 'Erfahrung', 'text' => 'Ueber 10 Jahre Branchenerfahrung', 'icon' => 'award', 'source' => '', 'url' => ''],
                ['title' => 'Kunden', 'text' => 'Hunderte zufriedene Kunden vertrauen uns', 'icon' => 'users', 'source' => '', 'url' => ''],
                ['title' => 'Projekte', 'text' => 'Erfolgreiche Projekte in verschiedenen Branchen', 'icon' => 'briefcase', 'source' => '', 'url' => ''],
            ],
            'color_scheme'       => '',
            'text_align'         => '',
            'card_style'         => '',
            'columns'            => '',
            'section_text_color' => '',
        ];
    }
}
