<?php
namespace nur\php;

use nur\A;
use nur\data\types\Metadata;
use nur\ref\ref_type;

class Autogen {
  static function schema(array $schema): array {
    $md = new Metadata($schema);
    return $md->getSchema();
  }

  static function auto_getters($schema, ?array $aliases=null): array {
    $md = Metadata::with($schema);
    $auto_getters = [];
    foreach ($md->getKeys() as $key) {
      $method_name = A::get($aliases, $key, $key);
      $method = $md->getType($key)->getGetterName($method_name);
      $auto_getters[$method] = $key;
    }
    return $auto_getters;
  }

  static function auto_setters($schema, ?array $aliases=null): array {
    $md = Metadata::with($schema);
    $auto_setters = [];
    foreach ($md->getKeys() as $key) {
      $method_name = A::get($aliases, $key, $key);
      $method = $md->getType($key)->getSetterName($method_name);
      $auto_setters[$method] = $key;
    }
    return $auto_setters;
  }

  static function auto_deleters($schema, ?array $aliases=null): array {
    $md = Metadata::with($schema);
    $auto_deleters = [];
    foreach ($md->getKeys() as $key) {
      $method_name = A::get($aliases, $key, $key);
      $method = $md->getType($key)->getDeleterName($method_name);
      $auto_deleters[$method] = $key;
    }
    return $auto_deleters;
  }

  static function auto_ci_getters($schema, ?array $aliases=null): array {
    $md = Metadata::with($schema);
    $auto_getters = [];
    foreach ($md->getCikeys() as $key) {
      $method_name = A::get($aliases, $key, $key);
      $method = $md->getType($key)->getGetterName($method_name);
      $auto_getters[$method] = $key;

    }
    return $auto_getters;
  }

  static function auto_ci_setters($schema, ?array $aliases=null): array {
    $md = Metadata::with($schema);
    $auto_setters = [];
    foreach ($md->getCikeys() as $key) {
      $method_name = A::get($aliases, $key, $key);
      $method = $md->getType($key)->getSetterName($method_name);
      $auto_setters[$method] = $key;
    }
    return $auto_setters;
  }

  static function fix_type(?string $phpType): array {
    if ($phpType === null) return [null, null];
    if (substr($phpType, 0, 1) == "?") {
      $nullable = "?";
      $ornull = "|null";
      $phpType = substr($phpType, 1);
    } else {
      $nullable = "";
      $ornull = "";
    }
    $array = "";
    while (substr($phpType, -2, 2) == "[]") {
      $array .= "[]";
      $phpType = substr($phpType, 0, -2);
    }
    if (!in_array($phpType, ref_type::PHP_KNOWN_TYPES)) {
      $phpType = "\\$phpType";
    }
    return ["$nullable$phpType$array", "$phpType$array$ornull"];
  }

  static function auto_getters_methods($schema, ?array $aliases=null, ?string $class=null): array {
    $md = Metadata::with($schema);
    $class_methods = get_class_methods($class);
    $auto_getters = array_merge(
      self::auto_getters($md, $aliases),
      self::auto_ci_getters($md, $aliases));
    $methods = [];
    foreach ($auto_getters as $method => $key) {
      if ($class_methods !== null && in_array($method, $class_methods)) continue;
      [$phpType, $returnType] = self::fix_type($md->getType($key)->getPhpType());
      $method .= "()";
      if ($phpType !== null) $method = "$returnType $method";
      $methods[] = $method;
    }
    return $methods;
  }

  static function auto_setters_methods($schema, ?array $aliases=null, ?string $class=null): array {
    $md = Metadata::with($schema);
    $class_methods = get_class_methods($class);
    $auto_setters = array_merge(
      self::auto_setters($md, $aliases),
      self::auto_ci_setters($md, $aliases));
    $methods = [];
    foreach ($auto_setters as $method => $key) {
      if ($class_methods !== null && in_array($method, $class_methods)) continue;
      [$phpType, $returnType] = self::fix_type($md->getType($key)->getPhpType());
      if ($phpType !== null) $method .= "($phpType \$value)";
      else $method .= "(\$value)";
      $methods[] = "void $method";
    }
    return $methods;
  }

  static function auto_deleters_methods($schema, ?array $aliases=null, ?string $class=null): array {
    $md = Metadata::with($schema);
    $class_methods = get_class_methods($class);
    $auto_deleters = self::auto_deleters($md, $aliases);
    $methods = [];
    foreach ($auto_deleters as $method => $key) {
      if ($class_methods !== null && in_array($method, $class_methods)) continue;
      $method .= "()";
      $methods[] = "void $method";
    }
    return $methods;
  }
}
