<?php
/**
 * ITSWEBER CMS – Before/After Comparison Section
 *
 * Interaktiver Bildvergleich mit Slider.
 * Migriert aus plugins/before-after-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 BeforeAfterSection extends AbstractSection
{
    public static function typeKey(): string    { return 'before_after'; }
    public static function label(): string      { return 'Before/After Slider'; }
    public static function category(): string   { return 'media'; }
    public static function description(): string { return 'Interaktiver Bildvergleich mit Schieberegler'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<rect x="3" y="3" width="18" height="18" rx="2"/>'
             . '<line x1="12" y1="3" x2="12" y2="21"/>'
             . '<polyline points="9 10 12 7 15 10"/>'
             . '<polyline points="9 14 12 17 15 14"/>'
             . '</svg>';
    }

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

        $itemTitles       = $request->input('bas_title', []);
        $itemDescs        = $request->input('bas_description', []);
        $itemBeforeImages = $request->input('bas_before_image', []);
        $itemAfterImages  = $request->input('bas_after_image', []);
        $itemBeforeLabels = $request->input('bas_before_label', []);
        $itemAfterLabels  = $request->input('bas_after_label', []);

        $items = [];
        if (is_array($itemTitles)) {
            foreach ($itemTitles as $i => $itemTitle) {
                $t = trim((string) $itemTitle);
                if ($t === '' && trim((string) ($itemBeforeImages[$i] ?? '')) === '') {
                    continue;
                }

                $beforeImage = trim((string) ($itemBeforeImages[$i] ?? ''));
                $afterImage  = trim((string) ($itemAfterImages[$i] ?? ''));

                // Validate image paths
                if ($beforeImage !== '' && !preg_match('#^[a-zA-Z0-9\-_/\.]+$#', $beforeImage)) {
                    $beforeImage = '';
                }
                if ($afterImage !== '' && !preg_match('#^[a-zA-Z0-9\-_/\.]+$#', $afterImage)) {
                    $afterImage = '';
                }

                $items[] = [
                    'title'        => $t,
                    'description'  => trim((string) ($itemDescs[$i] ?? '')),
                    'before_image' => $beforeImage,
                    'after_image'  => $afterImage,
                    'before_label' => trim((string) ($itemBeforeLabels[$i] ?? 'Before')),
                    'after_label'  => trim((string) ($itemAfterLabels[$i] ?? 'After')),
                ];
            }
        }
        $items = array_slice($items, 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,
            'items'              => $items,
        ];

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

    public function defaultData(): array
    {
        return [
            'title' => 'Vorher & Nachher',
            'subtitle' => 'Sehen Sie den Unterschied',
            'description' => '',
            'color_scheme' => '', 'section_text_color' => '',
            'columns' => '2',
            'items' => [
                [
                    'title'        => 'Beispiel-Vergleich',
                    'description'  => '',
                    'before_image' => '',
                    'after_image'  => '',
                    'before_label' => 'Vorher',
                    'after_label'  => 'Nachher',
                ],
            ],
        ];
    }

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

    public function scripts(): array
    {
        return ['/assets/js/sections/before-after-section.js'];
    }
}
