<?php
/**
 * ITSWEBER CMS – Stats / Key Figures Section
 *
 * Large numbers/metrics with labels and optional icons.
 * Ideal for company statistics, achievements, key figures.
 *
 * @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 StatsSection extends AbstractSection
{
    public static function typeKey(): string    { return 'stats'; }
    public static function label(): string      { return 'Kennzahlen'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Grosse Zahlen mit Labels – ideal fuer Statistiken und Erfolge'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<path d="M18 20V10M12 20V4M6 20v-6"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $values  = $request->input('stat_value', []);
        $labels  = $request->input('stat_label', []);
        $icons   = $request->input('stat_icon', []);
        $prefixes = $request->input('stat_prefix', []);
        $suffixes = $request->input('stat_suffix', []);

        $items = [];
        if (is_array($values)) {
            foreach ($values as $i => $val) {
                $v = trim((string) $val);
                if ($v === '') continue;

                $items[] = [
                    'value'  => $v,
                    'label'  => trim((string) ($labels[$i] ?? '')),
                    'icon'   => trim((string) ($icons[$i] ?? '')),
                    'prefix' => trim((string) ($prefixes[$i] ?? '')),
                    'suffix' => trim((string) ($suffixes[$i] ?? '')),
                ];
            }
        }
        $items = array_slice($items, 0, 8);

        $columns = (string) ($request->input('columns', '4') ?? '4');
        if (!in_array($columns, self::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()),
            'section_surface'    => self::sanitizeToken((string) ($request->input('section_surface', '') ?? ''), ['', 'subtle', 'elevated']),
            'text_align'         => self::sanitizeToken((string) ($request->input('text_align', '') ?? ''), ['', 'left', 'center']),
            'columns'            => $columns,
            'items'              => $items,
        ];

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

    public function defaultData(): array
    {
        return [
            'title' => 'Zahlen & Fakten',
            'subtitle' => 'Unsere Erfolge sprechen fuer sich',
            'color_scheme' => '', 'section_text_color' => '',
            'section_surface' => '', 'text_align' => '',
            'columns' => '4',
            'items' => [
                ['value' => '500', 'label' => 'Zufriedene Kunden', 'icon' => '', 'prefix' => '', 'suffix' => '+'],
                ['value' => '10', 'label' => 'Jahre Erfahrung', 'icon' => '', 'prefix' => '', 'suffix' => '+'],
                ['value' => '99', 'label' => 'Kundenzufriedenheit', 'icon' => '', 'prefix' => '', 'suffix' => '%'],
                ['value' => '1200', 'label' => 'Abgeschlossene Projekte', 'icon' => '', 'prefix' => '', 'suffix' => '+'],
            ],
        ];
    }
}
