<?php
declare(strict_types=1);

require __DIR__ . '/includes/bootstrap.php';
require __DIR__ . '/includes/layout.php';

$locale = detect_locale();
$messages = load_messages($locale);

$inviteUrl = bot_invite_url($config);
$dashboardUrl = rtrim((string) $config['dashboard_url'], '/');
$loginUrl = $dashboardUrl . '/login';
$supportUrl = isset($config['support_url']) && $config['support_url'] !== ''
    ? (string) $config['support_url']
    : null;

$guildCount = 0;
$userCount = 0;
$statsApi = (string) ($config['stats_api_url'] ?? '');
if ($statsApi !== '') {
    $ctx = stream_context_create([
        'http' => [
            'timeout' => 3,
            'header' => "Accept: application/json\r\n",
        ],
    ]);
    $raw = @file_get_contents($statsApi, false, $ctx);
    if ($raw !== false) {
        $json = json_decode($raw, true);
        if (is_array($json)) {
            $guildCount = (int) ($json['guildCount'] ?? 0);
            $userCount = (int) ($json['approximateUserCount'] ?? 0);
        }
    }
}

$numberLocale = $locale === 'de' ? 'de_DE' : 'en_US';
$formatNumber = static function (int $value) use ($numberLocale): string {
    if (class_exists(NumberFormatter::class)) {
        $formatter = new NumberFormatter($numberLocale, NumberFormatter::DECIMAL);
        $formatted = $formatter->format($value);
        if ($formatted !== false) {
            return $formatted;
        }
    }
    $decimal = $numberLocale === 'de_DE' ? ',' : '.';
    $thousands = $numberLocale === 'de_DE' ? '.' : ',';
    return number_format($value, 0, $decimal, $thousands);
};

/** @var array<string, array{label: string, modules: list<array{label: string, description: string}>}> $groups */
$groups = $messages['module_groups'];

render_header(
    $config,
    $messages,
    $locale,
    'Nexumi',
    t($messages, 'meta.description')
);
?>
      <section class="hero-section">
        <div class="container hero">
          <div class="hero-brand">
            <img src="assets/nexumi-logo.svg" alt="" width="56" height="56">
            <h1>Nexumi</h1>
          </div>
          <p class="hero-headline"><?= h(t($messages, 'landing.headline')) ?></p>
          <p class="hero-subtitle"><?= h(t($messages, 'landing.subtitle')) ?></p>
          <div class="actions">
            <a class="btn btn-lg btn-primary" href="<?= h($inviteUrl) ?>" target="_blank" rel="noreferrer"><?= h(t($messages, 'landing.invite')) ?></a>
            <a class="btn btn-lg btn-secondary" href="<?= h($loginUrl) ?>"><?= h(t($messages, 'landing.login')) ?></a>
            <?php if ($supportUrl): ?>
              <a class="btn btn-lg btn-outline" href="<?= h($supportUrl) ?>" target="_blank" rel="noreferrer"><?= h(t($messages, 'landing.support')) ?></a>
            <?php endif; ?>
          </div>
        </div>
      </section>

      <section class="border-b">
        <div class="container stats">
          <div class="stat-card">
            <p class="card-label"><?= h(t($messages, 'landing.stats_guilds')) ?></p>
            <p class="card-value"><?= h($formatNumber($guildCount)) ?></p>
          </div>
          <div class="stat-card">
            <p class="card-label"><?= h(t($messages, 'landing.stats_users')) ?></p>
            <p class="card-value"><?= h($formatNumber($userCount)) ?></p>
          </div>
        </div>
      </section>

      <section>
        <div class="container features">
          <div class="intro">
            <h2><?= h(t($messages, 'landing.features_title')) ?></h2>
            <p><?= h(t($messages, 'landing.features_subtitle')) ?></p>
          </div>
          <?php foreach ($groups as $group): ?>
            <div class="group">
              <h3><?= h($group['label']) ?></h3>
              <div class="grid">
                <?php foreach ($group['modules'] as $module): ?>
                  <div class="module-card">
                    <p class="module-title"><?= h($module['label']) ?></p>
                    <p class="module-desc"><?= h($module['description']) ?></p>
                  </div>
                <?php endforeach; ?>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
      </section>
<?php
render_footer($config, $messages);
