<?php
declare(strict_types=1);

function manager_continue_group_users(array $state, array $finishedSeason, string $user): array {
  $targetLevel = manager_user_division_level($state, $user);
  $nextYear = manager_season_year($finishedSeason, $state) + 1;
  $rootId = manager_season_root_id($state, $finishedSeason);
  $group = [$user];
  foreach (array_keys($state['users'] ?? []) as $name) {
    $name = clean_name((string)$name);
    if ($name === '' || $name === $user) continue;
    if (!isset($state['users'][$name]) || !has_team($state['users'][$name])) continue;
    if (manager_user_division_level($state, $name) !== $targetLevel) continue;
    $seasonId = current_manager_season_id($state, $name);
    if ($seasonId === null || empty($state['managerSeasons'][$seasonId])) continue;
    $season = $state['managerSeasons'][$seasonId];
    if (manager_season_root_id($state, $season) !== $rootId) continue;
    if (($season['status'] ?? '') !== 'finished') continue;
    if (manager_season_year($season, $state) + 1 !== $nextYear) continue;
    $group[] = $name;
  }
  return array_values(array_unique($group));
}

function manager_season_root_id(array $state, array $season): string {
  $id = (string)($season['id'] ?? '');
  $root = $id;
  $seen = [];
  while (true) {
    $from = (string)($season['continuedFrom'] ?? '');
    if ($from === '' || isset($seen[$from])) break;
    $seen[$from] = true;
    $root = $from;
    if (empty($state['managerSeasons'][$from]) || !is_array($state['managerSeasons'][$from])) break;
    $season = $state['managerSeasons'][$from];
  }
  return $root !== '' ? $root : $id;
}

function manager_continue_waiting_users(array $state, array $finishedSeason, string $user): array {
  if (($finishedSeason['status'] ?? '') !== 'finished') return [];
  $year = manager_season_year($finishedSeason, $state);
  $rootId = manager_season_root_id($state, $finishedSeason);
  $seen = [];
  foreach (array_keys($state['managerCurrentByUser'] ?? []) as $name) $seen[clean_name((string)$name)] = true;
  foreach (($finishedSeason['humanManagers'] ?? []) as $name) $seen[clean_name((string)$name)] = true;
  unset($seen[$user], $seen['']);
  $waiting = [];
  foreach (array_keys($seen) as $name) {
    if (!isset($state['users'][$name])) continue;
    $seasonId = current_manager_season_id($state, $name);
    if ($seasonId === null || empty($state['managerSeasons'][$seasonId])) continue;
    $season = $state['managerSeasons'][$seasonId];
    if (manager_season_root_id($state, $season) !== $rootId) continue;
    if (manager_season_year($season, $state) !== $year) continue;
    if (($season['status'] ?? '') !== 'finished') $waiting[] = $name;
  }
  return array_values(array_unique($waiting));
}

function continued_manager_season_id(array $state, string $fromSeasonId, string $user, int $targetLevel): ?string {
  foreach (($state['managerSeasons'] ?? []) as $id => $season) {
    if (!is_array($season)) continue;
    if (($season['status'] ?? '') === 'aborted') continue;
    if ((string)($season['continuedFrom'] ?? '') !== $fromSeasonId) continue;
    if ((int)($season['continuedGroupLevel'] ?? $season['divisionLevel'] ?? 0) !== $targetLevel) continue;
    if (!manager_season_visible_to_user($season, $user)) continue;
    return (string)$id;
  }
  return null;
}
