<?php
/**
 * ITSWEBER CMS – Tournament Section (Nexus Theme)
 *
 * Esports tournament/event list with game tags, prize pools and status badges.
 * Theme-bound: only available when the Nexus theme is active.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 */

namespace App\Sections\Theme\Nexus;

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

class TournamentSection extends AbstractSection
{
    public static function typeKey(): string    { return 'tournament'; }
    public static function label(): string      { return 'Turniere'; }
    public static function category(): string   { return 'content'; }
    public static function themeSlug(): ?string { return 'nexus'; }
    public static function description(): string { return 'Turnierliste mit Game-Tags, Preisgeld und Status'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<path d="M6 9H4.5a2.5 2.5 0 0 1 0-5H6"/>'
             . '<path d="M18 9h1.5a2.5 2.5 0 0 0 0-5H18"/>'
             . '<path d="M4 22h16"/><path d="M10 14.66V17c0 .55-.47.98-.97 1.21C7.85 18.75 7 20 7 22"/>'
             . '<path d="M14 14.66V17c0 .55.47.98.97 1.21C16.15 18.75 17 20 17 22"/>'
             . '<path d="M18 2H6v7a6 6 0 0 0 12 0V2Z"/>'
             . '</svg>';
    }

    private static function allowedStatus(): array
    {
        return ['', 'open', 'full', 'live', 'completed'];
    }

    public function buildData(Request $request): array
    {
        $titles  = $request->input('event_title', []);
        $details = $request->input('event_detail', []);
        $games   = $request->input('event_game', []);
        $dates   = $request->input('event_date', []);
        $prizes  = $request->input('event_prize', []);
        $statuses = $request->input('event_status', []);
        $labels  = $request->input('event_status_label', []);
        $urls    = $request->input('event_url', []);

        $items = [];
        if (is_array($titles)) {
            foreach ($titles as $i => $title) {
                $t = trim((string) $title);
                if ($t === '') continue;

                $items[] = [
                    'title'        => $t,
                    'detail'       => trim((string) ($details[$i] ?? '')),
                    'game'         => trim((string) ($games[$i] ?? '')),
                    'date'         => trim((string) ($dates[$i] ?? '')),
                    'prize'        => trim((string) ($prizes[$i] ?? '')),
                    'status'       => self::sanitizeToken(trim((string) ($statuses[$i] ?? '')), self::allowedStatus()),
                    'status_label' => trim((string) ($labels[$i] ?? '')),
                    'url'          => trim((string) ($urls[$i] ?? '')),
                ];
            }
        }
        $items = array_slice($items, 0, 8);

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

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

    public function defaultData(): array
    {
        return [
            'title'    => 'Aktive Turniere',
            'subtitle' => 'Melde dich an und kaempfe um den Titel.',
            'color_scheme' => '', 'section_text_color' => '',
            'items' => [
                ['game' => 'Valorant', 'title' => 'Nexus Valorant Cup',    'date' => date('Y-m-d', strtotime('+14 days')), 'prize' => '500',  'teams' => '16', 'status' => 'open', 'format' => '5v5 Best-of-3', 'detail' => 'Offener Community-Cup fuer alle Skill-Level.'],
                ['game' => 'CS2',      'title' => 'Counter-Strike League', 'date' => date('Y-m-d', strtotime('+21 days')), 'prize' => '1000', 'teams' => '32', 'status' => 'open', 'format' => '5v5 Double Elimination', 'detail' => 'Ligaspiel mit woechtlichen Matches.'],
                ['game' => 'LoL',      'title' => 'LoL Championship',      'date' => date('Y-m-d', strtotime('+7 days')),  'prize' => '2500', 'teams' => '8',  'status' => 'full', 'format' => '5v5 Best-of-5 Finale', 'detail' => 'Das grosse Saisonfinale.'],
            ],
        ];
    }
}
