<?php
declare(strict_types=1);

ini_set('display_errors', '0');
ini_set('memory_limit', '256M');
session_name('YSPWEB');
session_set_cookie_params([
  'lifetime' => 0,
  'path' => '/',
  'secure' => !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off',
  'httponly' => true,
  'samesite' => 'Strict',
]);
session_start();

header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');

const YSP_CODE_ROOT = __DIR__ . '/../..';

function ysp_bootstrap_runtime_root(): string {
  $dir = YSP_CODE_ROOT . '/.data';
  if (!is_dir($dir)) @mkdir($dir, 0777, true);
  if (is_dir($dir)) {
    @chmod($dir, 0777);
    return $dir;
  }
  return YSP_CODE_ROOT;
}

define('YSP_RUNTIME_ROOT', ysp_bootstrap_runtime_root());

if (YSP_RUNTIME_ROOT !== YSP_CODE_ROOT) {
  $legacyState = YSP_CODE_ROOT . '/.state.json';
  $runtimeState = YSP_RUNTIME_ROOT . '/state.json';
  if (!is_file($runtimeState) && is_file($legacyState)) {
    @copy($legacyState, $runtimeState);
    @chmod($runtimeState, 0666);
  }
}

require_once YSP_CODE_ROOT . '/.lib/app_version.php';
require_once YSP_CODE_ROOT . '/.lib/http.php';

if (YSP_RUNTIME_ROOT === YSP_CODE_ROOT) {
  define('STATE_FILE', YSP_CODE_ROOT . '/.state.json');
  define('STATE_BACKUP_FILE', YSP_CODE_ROOT . '/.state.json.bak');
  define('STATE_LOCK_FILE', YSP_CODE_ROOT . '/.state.lock');
} else {
  define('STATE_FILE', YSP_RUNTIME_ROOT . '/state.json');
  define('STATE_BACKUP_FILE', YSP_RUNTIME_ROOT . '/state.json.bak');
  define('STATE_LOCK_FILE', YSP_RUNTIME_ROOT . '/state.lock');
}
const STATE_SCHEMA_VERSION = 2;
const MATCH_GAME_MINUTES = 90.0;
const MATCH_REAL_SECONDS = 180.0;
const BALL_GRAVITY = 0.65;
const BALL_BOUNCE = 0.22;
const BALL_AIR_DRAG = 0.28;
const BALL_ROLL_DECEL = 0.050;
const BALL_SHOT_ROLL_DECEL = 0.040;
const BALL_MAX_SPEED = 0.58;
const BALL_STOP_SPEED = 0.006;
const BALL_SHORT_LINE_OVER_MARGIN = 0.025;
const BALL_SHORT_LINE_VISIBLE_MARGIN = 0.090;
const KICKOFF_PAUSE_GAME_MINUTES = 1.75;
const PITCH_WIDTH_M = 68.0;
const GOAL_WIDTH_M = 7.32;
const SQUAD_SCALE_VERSION = 3;
const SQUAD_NAME_VERSION = 2;
const SQUAD_ID_VERSION = 1;
const CPU_FORMATION_VERSION = 1;
const MANAGER_HUMAN_SNAPSHOT_VERSION = 1;
const MANAGER_MARKET_VERSION = 5;
const MANAGER_CUP_VERSION = 3;
const MANAGER_MAX_SKILL_POINTS = 29000;
const MANAGER_MAX_BENCH_PLAYERS = 9;
const MANAGER_MAX_SQUAD_RESERVES = 34;
const MANAGER_MAX_MATCH_SUBS = 5;
const MANAGER_MAX_TRAINING_PLAYERS = 5;
const MANAGER_MARKET_TARGET_PLAYERS = 40;
const MANAGER_SIM_RESULT_REVEAL_SECONDS = 65;
const MANAGER_LIVE_PLAYBACK_SECONDS = 86;
const MANAGER_PLAYER_VALUE_BOOST_MAX = 200;
const MANAGER_SAVE_META_CACHE_VERSION = 4;
const MANAGER_SAVE_DIR = YSP_CODE_ROOT . '/.manager-saves';
const MANAGER_SAVE_BACKUP_DIR = YSP_CODE_ROOT . '/.manager-save-backups';
const MANAGER_SAVE_KEEP_PER_GROUP = 12;
const MANAGER_SAVE_BACKUP_KEEP_PER_GROUP = 30;
const MANAGER_SAVE_BACKUP_GZIP_LEVEL = 5;
const LEGACY_PLAYER_NAMES_FILE = YSP_CODE_ROOT . '/.legacy-player-names.json';
const TEAM_PLAYER_DIR = YSP_CODE_ROOT . '/.team-players';
const PLAYER_NATIONALITIES_FILE = YSP_CODE_ROOT . '/.player-nationalities.json';
const TEAM_FLAGS_FILE = YSP_CODE_ROOT . '/.team-flags.json';
const TEAM_LOGOS_FILE = YSP_CODE_ROOT . '/.team-logos.json';
const LEGACY_YSP_ENGLAND_DIR = '';
