<?php

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

use MintyPHP\Session;

$values = $values ?? [];
$settings = $settings ?? [];
$sessionIdleTimeoutMinutes = (int) ($values['session_idle_timeout_minutes'] ?? 30);
$sessionAbsoluteTimeoutHours = (int) ($values['session_absolute_timeout_hours'] ?? 8);
$rememberTokenLifetimeDays = (int) ($values['remember_token_lifetime_days'] ?? 30);
$microsoftAutoRememberDefault = !empty($values['microsoft_auto_remember_default']);
$sessionIdleTimeoutMinutesDesc = $settings['session_idle_timeout_minutes']['description'] ?? 'setting.session_idle_timeout_minutes';
$sessionAbsoluteTimeoutHoursDesc = $settings['session_absolute_timeout_hours']['description'] ?? 'setting.session_absolute_timeout_hours';
$rememberTokenLifetimeDaysDesc = $settings['remember_token_lifetime_days']['description'] ?? 'setting.remember_token_lifetime_days';
$activeLoginTokens = (int) ($activeLoginTokens ?? 0);
$canUpdateSettings = (bool) ($canUpdateSettings ?? false);
$isReadOnly = !$canUpdateSettings;
$readonlyAttr = $isReadOnly ? 'readonly' : '';
$disabledAttr = $isReadOnly ? 'disabled' : '';
?>

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

    <form id="settings-account-access-form" method="post" data-details-storage="settings-account-access-details-v1" data-standard-detail-form="1">
      <details class="app-details-card" name="settings-account-access-session" data-details-key="settings-account-access-session" open>
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('Session policy')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="neutral"><?php e((string) $sessionIdleTimeoutMinutes); ?></span>
            <span class="badge" data-variant="neutral"><?php e((string) $sessionAbsoluteTimeoutHours); ?></span>
          </span>
        </summary>
        <div class="app-details-card-container">
          <div class="grid">
            <label class="app-field">
              <span><?php e(t('Session idle timeout (minutes)')); ?></span>
              <input type="number" name="session_idle_timeout_minutes"
                value="<?php e((string) $sessionIdleTimeoutMinutes); ?>" min="5" max="1440" step="1"
                <?php e($readonlyAttr); ?>>
              <small><?php e(t($sessionIdleTimeoutMinutesDesc)); ?></small>
            </label>
            <label class="app-field">
              <span><?php e(t('Session absolute timeout (hours)')); ?></span>
              <input type="number" name="session_absolute_timeout_hours"
                value="<?php e((string) $sessionAbsoluteTimeoutHours); ?>" min="1" max="72" step="1"
                <?php e($readonlyAttr); ?>>
              <small><?php e(t($sessionAbsoluteTimeoutHoursDesc)); ?></small>
            </label>
          </div>
        </div>
      </details>

      <details class="app-details-card" name="settings-account-access-login-persistence" data-details-key="settings-account-access-login-persistence">
        <summary>
          <span class="app-details-card-summary-title"><?php e(t('Login persistence')); ?></span>
          <span class="app-details-card-summary-meta">
            <span class="badge" data-variant="neutral"><?php e((string) $rememberTokenLifetimeDays); ?></span>
          </span>
        </summary>
        <div class="app-details-card-container">
          <label class="app-field">
            <span><?php e(t('Remember token lifetime (days)')); ?></span>
            <input type="number" name="remember_token_lifetime_days"
              value="<?php e((string) $rememberTokenLifetimeDays); ?>" min="1" max="365" step="1"
              <?php e($readonlyAttr); ?>>
            <small><?php e(t($rememberTokenLifetimeDaysDesc)); ?></small>
          </label>
          <hr>
          <label class="app-field">
            <input type="checkbox" role="switch" name="microsoft_auto_remember_default" value="1"
              <?php e($microsoftAutoRememberDefault ? 'checked' : ''); ?>
              <?php e($disabledAttr); ?>>
            <span><?php e(t('Microsoft Auto-Remember default')); ?></span>
          </label>
          <small class="muted"><?php e(t('When enabled, successful Microsoft logins automatically persist a remember-me token (unless overridden per tenant).')); ?></small>
        </div>
      </details>

      <?php if ($canUpdateSettings): ?>
        <details class="app-details-card" name="settings-account-access-login-tokens" data-details-key="settings-account-access-login-tokens">
          <summary>
            <span class="app-details-card-summary-title"><?php e(t('Login tokens')); ?></span>
            <span class="app-details-card-summary-meta">
              <span class="badge" data-variant="neutral"><?php e(sprintf(t('%d active login tokens'), $activeLoginTokens)); ?></span>
            </span>
          </summary>
          <div class="app-details-card-container">
            <blockquote data-variant="warning">
              <small><?php e(t('This will expire all remember-me tokens immediately and force users to sign in again.')); ?></small>
            </blockquote>
            <button type="submit" class="danger" formnovalidate
              formaction="admin/settings/expire-remember-tokens"
              formmethod="post"
              data-detail-confirm-message="<?php e(t('Expire all login tokens?')); ?>"
              data-detail-action-kind="danger">
              <?php e(t('Expire all login tokens')); ?>
            </button>
          </div>
        </details>
      <?php endif; ?>

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