<?php
/**
 * ITSWEBER CMS – Course Schedule Section (Pulse Theme)
 *
 * Fitness course cards with category badge, duration, level and schedule.
 * Theme-bound: only available when the Pulse theme is active.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 */

namespace App\Sections\Theme\Pulse;

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

class CourseScheduleSection extends AbstractSection
{
    public static function typeKey(): string    { return 'course_schedule'; }
    public static function label(): string      { return 'Kursplan'; }
    public static function category(): string   { return 'content'; }
    public static function themeSlug(): ?string { return 'pulse'; }
    public static function description(): string { return 'Fitness-Kurskarten mit Kategorie, Dauer und Zeitplan'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<rect x="3" y="4" width="18" height="18" rx="2"/>'
             . '<line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/>'
             . '<line x1="3" y1="10" x2="21" y2="10"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $titles       = $request->input('course_title', []);
        $descriptions = $request->input('course_description', []);
        $categories   = $request->input('course_category', []);
        $durations    = $request->input('course_duration', []);
        $levels       = $request->input('course_level', []);
        $sessions     = $request->input('course_next_session', []);
        $images       = $request->input('course_image', []);

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

                $items[] = [
                    'title'        => $t,
                    'description'  => trim((string) ($descriptions[$i] ?? '')),
                    'category'     => trim((string) ($categories[$i] ?? '')),
                    'duration'     => trim((string) ($durations[$i] ?? '')),
                    'level'        => trim((string) ($levels[$i] ?? '')),
                    'next_session' => trim((string) ($sessions[$i] ?? '')),
                    'image'        => trim((string) ($images[$i] ?? '')),
                ];
            }
        }
        $items = array_slice($items, 0, 6);

        $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'    => 'Kursplan',
            'subtitle' => 'Unsere beliebtesten Kurse – jeden Tag, fuer jedes Level.',
            'color_scheme' => '', 'section_text_color' => '',
            'items' => [
                ['title' => 'HIIT Power',        'duration' => '45 Min', 'level' => 'Fortgeschritten', 'trainer' => 'Lisa M.', 'next_session' => 'Mo 18:00', 'category' => 'Cardio',  'image' => ''],
                ['title' => 'Power Yoga',        'duration' => '60 Min', 'level' => 'Alle Level',      'trainer' => 'Sarah K.', 'next_session' => 'Di 10:00', 'category' => 'Balance', 'image' => ''],
                ['title' => 'CrossFit WOD',      'duration' => '50 Min', 'level' => 'Fortgeschritten', 'trainer' => 'Tom B.',   'next_session' => 'Mi 17:30', 'category' => 'Kraft',   'image' => ''],
                ['title' => 'Box Conditioning',  'duration' => '45 Min', 'level' => 'Anfaenger',       'trainer' => 'Max H.',   'next_session' => 'Do 19:00', 'category' => 'Combat',  'image' => ''],
            ],
        ];
    }
}
