<?php
/**
 * ITSWEBER CMS – Testimonials Section
 *
 * Customer quotes with author info, optional avatar and rating.
 * Supports staggered and grid layouts.
 *
 * @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 TestimonialsSection extends AbstractSection
{
    public static function typeKey(): string    { return 'testimonials'; }
    public static function label(): string      { return 'Testimonials'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Kundenstimmen mit Zitat, Autor und optionalem Avatar'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $quotes  = $request->input('testimonial_quote', []);
        $authors = $request->input('testimonial_author', []);
        $roles   = $request->input('testimonial_role', []);
        $avatars = $request->input('testimonial_avatar', []);
        $ratings = $request->input('testimonial_rating', []);

        $items = [];
        if (is_array($quotes)) {
            foreach ($quotes as $i => $quote) {
                $q = trim((string) $quote);
                if ($q === '') continue;

                $rating = (int) ($ratings[$i] ?? 0);
                if ($rating < 0 || $rating > 5) $rating = 0;

                $items[] = [
                    'quote'  => $q,
                    'author' => trim((string) ($authors[$i] ?? '')),
                    'role'   => trim((string) ($roles[$i] ?? '')),
                    'avatar' => trim((string) ($avatars[$i] ?? '')),
                    'rating' => $rating,
                ];
            }
        }
        $items = array_slice($items, 0, 6);

        $columns = (string) ($request->input('columns', '2') ?? '2');
        if (!in_array($columns, ['1', '2', '3'], true)) {
            $columns = '2';
        }

        $layout = (string) ($request->input('layout', 'staggered') ?? 'staggered');
        if (!in_array($layout, ['staggered', 'grid', 'single'], true)) {
            $layout = 'staggered';
        }

        $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,
            'layout'             => $layout,
            'items'              => $items,
        ];

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

    public function defaultData(): array
    {
        return [
            'title' => 'Das sagen unsere Kunden',
            'subtitle' => 'Erfahrungsberichte',
            'color_scheme' => '', 'section_text_color' => '',
            'section_surface' => '', 'text_align' => '',
            'columns' => '2', 'layout' => 'staggered',
            'items' => [
                ['quote' => 'Die Zusammenarbeit war hervorragend. Das Ergebnis hat unsere Erwartungen uebertroffen.', 'author' => 'Maria Mueller', 'role' => 'Mueller GmbH', 'avatar' => '', 'rating' => 5],
                ['quote' => 'Professionell, zuverlaessig und kreativ. Genau der Partner, den wir gesucht haben.', 'author' => 'Stefan Koch', 'role' => 'Koch & Partner', 'avatar' => '', 'rating' => 5],
                ['quote' => 'Schnelle Umsetzung, faire Preise und ein tolles Endergebnis. Klare Empfehlung!', 'author' => 'Laura Fischer', 'role' => 'Fischer Design', 'avatar' => '', 'rating' => 4],
            ],
        ];
    }
}
