<?php
require_once dirname(__DIR__) . '/app_runtime.php';

$cases = [
  [
    'config' => [
      'install_type' => 'server',
      'security' => ['idle_timeout_enabled' => true, 'idle_timeout_minutes' => 5],
    ],
    'enabled' => true,
    'minutes' => 5,
  ],
  [
    'config' => [
      'install_type' => 'branch',
      'security' => ['idle_timeout_enabled' => true, 'idle_timeout_minutes' => 45],
    ],
    'enabled' => true,
    'minutes' => 45,
  ],
  [
    'config' => [
      'install_type' => 'agent',
      'security' => ['idle_timeout_enabled' => false, 'idle_timeout_minutes' => 90],
    ],
    'enabled' => false,
    'minutes' => 90,
  ],
  [
    'config' => [
      'install_type' => 'branch',
      'security' => ['idle_timeout_enabled' => true, 'idle_timeout_minutes' => 1],
    ],
    'enabled' => true,
    'minutes' => 5,
  ],
];

foreach ($cases as $index => $case) {
  $GLOBALS['UA_INSTALL_CONFIG_OVERRIDE'] = $case['config'];
  $policy = ua_idle_timeout_policy();
  if ((bool)$policy['enabled'] !== $case['enabled']) {
    throw new RuntimeException('Unexpected enabled state in case ' . $index);
  }
  if ((int)$policy['minutes'] !== $case['minutes']) {
    throw new RuntimeException('Unexpected timeout minutes in case ' . $index);
  }
  if ((int)$policy['seconds'] !== $case['minutes'] * 60) {
    throw new RuntimeException('Unexpected timeout seconds in case ' . $index);
  }
}

$GLOBALS['UA_INSTALL_CONFIG_OVERRIDE'] = ['install_type' => 'server'];
$defaultPolicy = ua_idle_timeout_policy();
if (!empty($defaultPolicy['enabled'])) {
  throw new RuntimeException('Inactivity protection must remain disabled by default until explicitly enabled.');
}

unset($GLOBALS['UA_INSTALL_CONFIG_OVERRIDE']);
echo "idle_timeout_policy_test: OK\n";
