<?php

/**
 * @var array $values
 * @var array $settings
 * @var bool  $canUpdateSettings
 */

use MintyPHP\Session;

$values = $values ?? [];
$settings = $settings ?? [];
$frontendTelemetryEnabled = !empty($values['frontend_telemetry_enabled']);
$frontendTelemetrySampleRate = (string) ($values['frontend_telemetry_sample_rate'] ?? '0.2');
$frontendTelemetrySampleRateOptions = ['0.1', '0.2', '0.5', '1'];
if (!in_array($frontendTelemetrySampleRate, $frontendTelemetrySampleRateOptions, true)) {
  $frontendTelemetrySampleRate = '0.2';
}
$frontendTelemetryAllowedEvents = $values['frontend_telemetry_allowed_events'] ?? ['warn_once', 'ajax_error'];
if (!is_array($frontendTelemetryAllowedEvents)) {
  $frontendTelemetryAllowedEvents = preg_split('/[\s,]+/', (string) $frontendTelemetryAllowedEvents) ?: [];
}
$frontendTelemetryAllowedEvents = array_values(array_unique(array_filter(array_map(
  static fn($entry): string => strtolower(trim((string) $entry)),
  $frontendTelemetryAllowedEvents
))));
if ($frontendTelemetryAllowedEvents === []) {
  $frontendTelemetryAllowedEvents = ['warn_once', 'ajax_error'];
}
$canUpdateSettings = (bool) ($canUpdateSettings ?? false);
$isReadOnly = !$canUpdateSettings;
$disabledAttr = $isReadOnly ? 'disabled' : '';
?>

<div class="app-details-container">
  <section>
    <?php
    $titlebar = [
      'title' => t('Telemetry settings'),
      'backHref' => 'admin/settings',
      'backTitle' => t('Cancel'),
      'actions' => $canUpdateSettings ? [
        [
          'form' => 'settings-telemetry-form',
          'name' => 'action',
          'value' => 'save',
          'class' => 'secondary outline',
          'label' => t('Save'),
        ],
        [
          'form' => 'settings-telemetry-form',
          'name' => 'action',
          'value' => 'save_close',
          'class' => 'primary',
          'label' => t('Save & close'),
        ],
      ] : [],
    ];
    require templatePath('partials/app-details-titlebar.phtml');
    ?>

    <form id="settings-telemetry-form" method="post" data-details-storage="settings-telemetry-details-v1" data-standard-detail-form="1">
      <details class="app-details-card" name="settings-telemetry" data-details-key="settings-telemetry" open>
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('Frontend telemetry')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="neutral"><?php e($frontendTelemetryEnabled ? t('Enabled') : t('Disabled')); ?></span>
            <?php if ($frontendTelemetryEnabled): ?>
              <span class="badge" data-variant="neutral"><?php e($frontendTelemetrySampleRate); ?></span>
            <?php endif; ?>
          </span>
        </summary>
        <div class="app-details-card-container">
          <label class="app-field">
            <input
              type="checkbox"
              role="switch"
              name="frontend_telemetry_enabled"
              value="1"
              data-telemetry-enabled-toggle
              <?php e($frontendTelemetryEnabled ? 'checked' : ''); ?>
              <?php e($disabledAttr); ?>>
            <span><?php e(t('Enable frontend telemetry')); ?></span>
          </label>
          <small class="muted"><?php e(t('Telemetry helps detect recurring UI issues and failed requests in production.')); ?></small>

          <div data-telemetry-when-enabled <?php e($frontendTelemetryEnabled ? '' : 'hidden'); ?>>
            <hr>
            <div class="grid">
              <label class="app-field">
                <span><?php e(t('Sampling rate')); ?></span>
                <select name="frontend_telemetry_sample_rate" <?php e($disabledAttr); ?>>
                  <option value="0.1" <?php e($frontendTelemetrySampleRate === '0.1' ? 'selected' : ''); ?>>10%</option>
                  <option value="0.2" <?php e($frontendTelemetrySampleRate === '0.2' ? 'selected' : ''); ?>>20%</option>
                  <option value="0.5" <?php e($frontendTelemetrySampleRate === '0.5' ? 'selected' : ''); ?>>50%</option>
                  <option value="1" <?php e($frontendTelemetrySampleRate === '1' ? 'selected' : ''); ?>>100%</option>
                </select>
                <small><?php e(t('The percentage of users who will have telemetry enabled.')); ?></small>
              </label>
              <div></div>
            </div>

            <fieldset>
              <legend><small><?php e(t('Allowed events')); ?></small></legend>
              <label class="app-field">
                <input type="checkbox" name="frontend_telemetry_allowed_events[]" value="warn_once" <?php e(in_array('warn_once', $frontendTelemetryAllowedEvents, true) ? 'checked' : ''); ?> <?php e($disabledAttr); ?>>
                <span><?php e(t('warnOnce warnings')); ?></span>
              </label>
              <label class="app-field">
                <input type="checkbox" name="frontend_telemetry_allowed_events[]" value="ajax_error" <?php e(in_array('ajax_error', $frontendTelemetryAllowedEvents, true) ? 'checked' : ''); ?> <?php e($disabledAttr); ?>>
                <span><?php e(t('AJAX errors')); ?></span>
              </label>
            </fieldset>
            <small class="muted"><?php e(t('Limit which event types are collected to match your privacy and operations requirements.')); ?></small>
          </div>
        </div>
      </details>

      <?php Session::getCsrfInput(); ?>
    </form>
  </section>
  <aside id="app-details-aside-section">
    <div class="app-details-aside-section"></div>
  </aside>
</div>
