<?php
/**
 * ITSWEBER CMS – Team Members Section
 *
 * Team member cards with photo, role, bio and social links.
 * Migriert aus plugins/team-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 TeamSection extends AbstractSection
{
    public static function typeKey(): string    { return 'team_section'; }
    public static function label(): string      { return 'Team-Mitglieder'; }
    public static function category(): string   { return 'content'; }
    public static function description(): string { return 'Team-Karten mit Foto, Rolle, Bio und Social-Links'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<circle cx="8" cy="7" r="3"/>'
             . '<circle cx="16" cy="7" r="3"/>'
             . '<path d="M2 21v-2a4 4 0 014-4h4a4 4 0 014 4v2"/>'
             . '<path d="M14 21v-2a4 4 0 014-4h0a4 4 0 014 4v2"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $allowedColumns    = ['2', '3', '4'];
        $allowedImageStyle = ['', 'circle', 'rounded', 'square'];

        $memberNames     = $request->input('member_name', []);
        $memberRoles     = $request->input('member_role', []);
        $memberImages    = $request->input('member_image', []);
        $memberBios      = $request->input('member_bio', []);
        $memberEmails    = $request->input('member_email', []);
        $memberLinkedins = $request->input('member_linkedin', []);
        $memberTwitters  = $request->input('member_twitter', []);
        $memberFacebooks = $request->input('member_facebook', []);
        $memberWebsites  = $request->input('member_website', []);

        $members = [];
        if (is_array($memberNames)) {
            foreach ($memberNames as $i => $name) {
                $n = trim((string) $name);
                if ($n === '') {
                    continue;
                }
                $members[] = [
                    'name'     => $n,
                    'role'     => trim((string) ($memberRoles[$i] ?? '')),
                    'image'    => trim((string) ($memberImages[$i] ?? '')),
                    'bio'      => trim((string) ($memberBios[$i] ?? '')),
                    'email'    => trim((string) ($memberEmails[$i] ?? '')),
                    'linkedin' => trim((string) ($memberLinkedins[$i] ?? '')),
                    'twitter'  => trim((string) ($memberTwitters[$i] ?? '')),
                    'facebook' => trim((string) ($memberFacebooks[$i] ?? '')),
                    'website'  => trim((string) ($memberWebsites[$i] ?? '')),
                ];
            }
        }

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

        $data = [
            'title'              => trim((string) ($request->input('title', '') ?? '')),
            'subtitle'           => trim((string) ($request->input('subtitle', '') ?? '')),
            'columns'            => $columns,
            'color_scheme'       => self::sanitizeToken((string) ($request->input('color_scheme', '') ?? ''), self::allowedColorScheme()),
            'card_style'         => self::sanitizeToken((string) ($request->input('card_style', '') ?? ''), self::allowedCardStyle()),
            'image_style'        => self::sanitizeToken((string) ($request->input('image_style', '') ?? ''), $allowedImageStyle),
            'section_text_color' => self::sanitizeToken((string) ($request->input('section_text_color', '') ?? ''), self::allowedSectionTextColor()),
            'members'            => $members,
        ];

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

    public function defaultData(): array
    {
        return [
            'title' => '', 'subtitle' => '',
            'columns' => '3', 'color_scheme' => '', 'card_style' => '',
            'image_style' => '', 'section_text_color' => '',
            'members' => [],
        ];
    }

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