<?php

declare(strict_types=1);

/**
 * Team-Dashboard als Bento-Grid: kombiniert Bild, Einleitung, Trainingszeiten,
 * Spielklasse und Spielort in einem Kachel-Layout. Props:
 *   $team   array — Eintrag aus data/teams.json.
 *   $anchor string (optional, Default 'team-info')
 * Spielort kommt aus data/club.json (NAP-Single-Source). Deko-Icons sind dekorativ.
 */
$anchor = $anchor ?? 'team-info';
$about = $team['about'] ?? [];
$sessions = $team['training']['sessions'] ?? [];
$venueOverride = $team['training']['venue'] ?? null;   // mannschaftsspezifischer Spielort (z. B. SG-Heimat)
$showVenue = !empty($team['training']['venue_from_club']) || !empty($venueOverride);
$highlight = !empty($team['league'])
    ? ['label' => 'Spielklasse', 'value' => $team['league']]
    : ['label' => 'Bereich', 'value' => $team['category'] ?? ''];

$club = json_load('club');
$addr = $club['address'] ?? [];
$mapsQuery = !empty($venueOverride['maps_query'])
    ? rawurlencode($venueOverride['maps_query'])
    : rawurlencode(trim(($addr['venue'] ?? '') . ', ' . ($addr['street'] ?? '') . ', ' . ($addr['zip'] ?? '') . ' ' . ($addr['city'] ?? ''), ', '));
?>
        <section class="section team-bento-section" id="<?= e($anchor) ?>">
            <div class="container">
                <div class="team-bento">
                    <article class="bento__tile team-bento__about">
                        <h2><?= e($about['title'] ?? 'Über die Mannschaft') ?></h2>
                        <p><?= e($about['text'] ?? '') ?></p>
                    </article>

                    <article class="bento__tile team-bento__training">
                        <h2 class="bento__heading"><?= icon('clock', 'bento__hicon') ?>Trainingszeiten</h2>
<?php if ($sessions !== []): ?>
                        <ul class="bento__sessions">
<?php foreach ($sessions as $s): ?>
                            <li>
                                <span class="bento__day"><?= e($s['day']) ?></span>
                                <span class="bento__time"><?= e($s['time']) ?></span>
                            </li>
<?php endforeach; ?>
                        </ul>
<?php else: ?>
                        <p class="text-muted">Die genauen Trainingszeiten erfährst du direkt bei unserem Ansprechpartner.</p>
<?php endif; ?>
<?php if (!empty($team['training']['note'])): ?>
                        <p class="bento__note text-muted"><?= e($team['training']['note']) ?></p>
<?php endif; ?>
                        <span class="bento__deco" aria-hidden="true"><?= icon('clock') ?></span>
                    </article>

<?php if (($highlight['value'] ?? '') !== ''): ?>
                    <article class="bento__tile bento__tile--accent team-bento__klasse">
                        <span class="bento__stat-label"><?= e($highlight['label']) ?></span>
                        <span class="bento__stat-value"><?= e($highlight['value']) ?></span>
                    </article>
<?php endif; ?>

<?php if ($showVenue && ($venueOverride || $addr !== [])): ?>
                    <article class="bento__tile team-bento__venue">
                        <div class="team-bento__venue-main">
                            <h2 class="bento__heading"><?= icon('geo-alt', 'bento__hicon') ?>Spielort</h2>
<?php if ($venueOverride): ?>
                            <p class="bento__address">
<?php if (!empty($venueOverride['name'])): ?><strong><?= e($venueOverride['name']) ?></strong><?php endif; ?><?php if (!empty($venueOverride['locality'])): ?> · <?= e($venueOverride['locality']) ?><?php endif; ?>
                            </p>
<?php else: ?>
                            <p class="bento__address">
<?php if (!empty($addr['venue'])): ?><strong><?= e($addr['venue']) ?></strong> · <?php endif; ?><?= e($addr['street']) ?>, <?= e($addr['zip']) ?> <?= e($addr['city']) ?>
                            </p>
<?php endif; ?>
                        </div>
                        <a class="btn btn--outline team-bento__route" href="https://www.google.com/maps/search/?api=1&amp;query=<?= e($mapsQuery) ?>" target="_blank" rel="noopener">Route planen<span class="visually-hidden"> (öffnet Google Maps in neuem Tab)</span></a>
                        <span class="bento__deco" aria-hidden="true"><?= icon('geo-alt') ?></span>
                    </article>
<?php endif; ?>
                </div>
            </div>
        </section>
