<?php
/**
 * ITSWEBER CMS – Rating Highlight Section
 *
 * Displays a prominent rating/review score from an external source.
 * Migriert aus plugins/rating-highlight-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 RatingHighlightSection extends AbstractSection
{
    public static function typeKey(): string    { return 'rating_highlight'; }
    public static function label(): string      { return 'Bewertungs-Highlight'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Prominente Bewertung/Score-Anzeige von einer externen Quelle'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $linkUrl = trim((string) ($request->input('link_url', '') ?? ''));
        if ($linkUrl !== '' && !preg_match('~^(https?://|/|\./)~i', $linkUrl)) {
            $linkUrl = '';
        }

        $data = [
            'title'              => trim((string) ($request->input('title', '') ?? '')),
            'subtitle'           => trim((string) ($request->input('subtitle', '') ?? '')),
            'source_name'        => trim((string) ($request->input('source_name', '') ?? '')),
            'score'              => trim((string) ($request->input('score', '') ?? '')),
            'max_score'          => trim((string) ($request->input('max_score', '') ?? '')),
            'review_count'       => trim((string) ($request->input('review_count', '') ?? '')),
            'review_label'       => trim((string) ($request->input('review_label', '') ?? '')),
            'summary_text'       => trim((string) ($request->input('summary_text', '') ?? '')),
            'summary_text_2'     => trim((string) ($request->input('summary_text_2', '') ?? '')),
            'as_of_date'         => trim((string) ($request->input('as_of_date', '') ?? '')),
            'link_url'           => $linkUrl,
            'link_label'         => trim((string) ($request->input('link_label', '') ?? '')),
            'color_scheme'       => self::sanitizeToken((string) ($request->input('color_scheme', '') ?? ''), self::allowedColorScheme()),
            'section_text_color' => self::sanitizeToken((string) ($request->input('section_text_color', '') ?? ''), self::allowedSectionTextColor()),
        ];

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

    public function defaultData(): array
    {
        return [
            'title' => '', 'subtitle' => '',
            'source_name' => '', 'score' => '', 'max_score' => '5',
            'review_count' => '', 'review_label' => 'Reviews',
            'summary_text' => '', 'summary_text_2' => '', 'as_of_date' => '',
            'link_url' => '', 'link_label' => 'View all reviews',
            'color_scheme' => '', 'section_text_color' => '',
        ];
    }

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