<?php
/**
 * ITSWEBER CMS – High Score Section (Arcade Theme)
 *
 * Retro leaderboard table with rank, player codes, categories and scores.
 * Theme-bound: only available when the Arcade theme is active.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 */

namespace App\Sections\Theme\Arcade;

use App\Sections\AbstractSection;
use App\Core\Request;

class HighScoreSection extends AbstractSection
{
    public static function typeKey(): string    { return 'high_score'; }
    public static function label(): string      { return 'High Scores'; }
    public static function category(): string   { return 'content'; }
    public static function themeSlug(): ?string { return 'arcade'; }
    public static function description(): string { return 'Retro-Bestenliste mit Rang, Spieler und Punktzahl'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<path d="M12 15l-2 5l9-11h-5l2-5l-9 11z"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $codes      = $request->input('player_code', []);
        $categories = $request->input('player_category', []);
        $scores     = $request->input('player_score', []);

        $items = [];
        if (is_array($codes)) {
            foreach ($codes as $i => $code) {
                $c = trim((string) $code);
                if ($c === '') continue;

                $items[] = [
                    'player_code' => strtoupper(substr($c, 0, 3)),
                    'category'    => trim((string) ($categories[$i] ?? '')),
                    'score'       => trim((string) ($scores[$i] ?? '')),
                ];
            }
        }
        $items = array_slice($items, 0, 10);

        $data = [
            'title'              => trim((string) ($request->input('title', '') ?? '')),
            'subtitle'           => trim((string) ($request->input('subtitle', '') ?? '')),
            'footer_text'        => trim((string) ($request->input('footer_text', '') ?? '')),
            'color_scheme'       => self::sanitizeToken((string) ($request->input('color_scheme', '') ?? ''), self::allowedColorScheme()),
            'section_text_color' => self::sanitizeToken((string) ($request->input('section_text_color', '') ?? ''), self::allowedSectionTextColor()),
            'items'              => $items,
        ];

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

    public function defaultData(): array
    {
        return [
            'title'       => 'HIGH SCORES',
            'subtitle'    => 'Die ewige Bestenliste',
            'footer_text' => 'Stand: ' . date('d.m.Y'),
            'color_scheme' => '', 'section_text_color' => '',
            'items' => [
                ['player_code' => 'BNY', 'category' => 'Beitraege',  'score' => '12480'],
                ['player_code' => 'USR', 'category' => 'Besucher',   'score' => '98750'],
                ['player_code' => 'DWN', 'category' => 'Downloads',  'score' => '5320'],
                ['player_code' => 'CMT', 'category' => 'Kommentare', 'score' => '1842'],
                ['player_code' => 'SHR', 'category' => 'Shares',     'score' => '967'],
            ],
        ];
    }
}
