<?php
namespace nur;

use nur\config\ConfigManager;
use nur\config\IConfigManager;

/**
 * Class config: gestion de la configuration de l'application
 */
class config {
  /** @var IConfigManager */
  protected static $manager;

  static final function set_manager(IConfigManager $manager) {
    self::$manager = $manager;
  }

  static final function __callStatic($name, $args) {
    $name = str::us2camel($name);
    return call_user_func_array([self::$manager, $name], $args);
  }

  #############################################################################

  static final function init_configurator($configurators): void { self::$manager->addConfigurator(A::with($configurators)); }
  static final function configure(?array $options=null): void { self::$manager->configure($options); }

  # certains types de configurations sont normalisés
  /** ne configurer que le minimum pour que l'application puisse s'initialiser */
  const CONFIGURE_INITIAL_ONLY = ["include" => "initial"];
  /** ne configurer que les routes */
  const CONFIGURE_ROUTES_ONLY = ["include" => "routes"];
  /** configurer uniquement ce qui ne nécessite pas d'avoir une session */
  const CONFIGURE_NO_SESSION = ["exclude" => "session"];

  static final function init_appcode(string $cod_app): void { self::$manager->initAppcode($cod_app); }
  static final function get_appcode(): string { return self::$manager->getAppcode(); }

  const PROD = IConfigManager::PROD;
  const TEST = IConfigManager::TEST;
  const DEVEL = IConfigManager::DEVEL;
  const DEFAULT_PROFILES = IConfigManager::DEFAULT_PROFILES;
  const PROFILE_ALL = IConfigManager::PROFILE_ALL;

  static final function set_default_profile(?string $default_profile): void { self::$manager->setDefaultProfile($default_profile); }
  static final function get_profiles(): array { return self::$manager->getProfiles(); }

  static final function set_profile(string $profile) { self::$manager->setProfile($profile); }
  static final function get_profile(bool $ensure_nn=true): ?string { return self::$manager->getProfile($ensure_nn); }
  static final function is_prod(): bool { return self::$manager->getProfile() === "prod"; }
  static final function is_test(): bool { return self::$manager->getProfile() === "test"; }
  static final function is_devel(): bool { return self::$manager->getProfile() === "devel"; }

  static final function add($config, string ...$profiles): void { self::$manager->addConfig($config, $profiles); }
  static final function get(string $pkey, $default=null, ?string $profile=null) { return self::$manager->getValue($pkey, $default, $profile); }
  static final function schema(string $pkey, $default=null) { return self::$manager->getValue("schema.$pkey", $default); }
  static final function db(string $pkey, $default=null) { return self::$manager->getValue("dbs.$pkey", $default); }
  static final function m(string $pkey, $default=null) { return self::$manager->getValue("msgs.$pkey", $default); }
  static final function l(string $pkey, $default=null) { return self::$manager->getValue("mails.$pkey", $default); }
  static final function ak(string $pkey, $default=null) { return self::$manager->getValue("app.$pkey", $default); }
  static final function uk(string $pkey, $default=null) { return self::$manager->getValue("user.$pkey", $default); }
  static final function k(string $pkey, $default=null) { return self::$manager->getValue("conf.$pkey", $default); }

  const FACT_NEEDS_JQUERY = "needs-jquery";
  const FACT_WEB_APP = "web-app";
  const FACT_CLI_APP = "cli-app";
  static final function set_fact(string $fact, $value=true): void { self::$manager->setFact($fact, $value); }
  static final function is_fact($value=true): bool { return self::$manager->isFact($value); }

  static final function set_debug(?bool $debug=true): void { self::$manager->setDebug($debug); }
  static final function is_debug(): bool { return self::$manager->isDebug(); }
}

new class extends config {
  function __construct() {
    self::$manager = new ConfigManager();
  }
};
