import json
import os

from .type_validation import validate
from .type_validation_functions import int_range, non_empty_str, one_of


OP_DEV_CONFIG = json.loads(os.environ['OP_DEV_CONFIG'])
OP_DEV_METADATA = json.loads(os.environ['OP_DEV_METADATA'])


# Make sure our config has the minimal set of attributes
OP_DEV_CONFIG_FORMAT = {
  'op:alarm': { 'type': one_of('null', 'op_dev'), 'config': dict },
  'op:storage': { 'type': one_of('memory', 'mongo_db'), 'config': dict },
  'op:auth': {
    'secret': non_empty_str,
    'exp_s': int_range('(0,∞)'),
    '[methods]': {
      '[facebook]': {
        # Enables authentication using Facebook as an o-auth provider
        # Preconditions: you have pre-configured Facebook to allow auth at your URL
        # Input data to /__private/auth: the token generated by Facebook's UI components
        # Input data to @auth function: the deserialized dictionary containing the scoped information for the authorized FB user
        'fields': [ non_empty_str ]
      },
      '[google]': {
        # Enables authentication using Google as an o-auth provider
        # Preconditions: you have pre-configured Google to allow auth at your URL
        # Input data to /__private/auth: the token generated by Google's UI components
        # Input data to @auth function: the deserialized dictionary containing the scoped information for the authorized Google user
        'client_id': non_empty_str
      },
      # '[op-service]': {
      #   # Lets other op-services (not necessarily just ones that you own) call yours
      #   # Input data to /__private/auth: a token generated by op-dev
      #   # Input data to @auth function: { 'pkg_id': ..., 'commit_id': ..., 'config_id': ..., 'domain': ... }
      #   'ids': [] # The package IDs of any op services that should be able to make authenticated calls to us
      # },
      # '[PASSTHROUGH]': {
      #   # You can use this method to implement your own authentication in your @auth function
      #   # Input data to /__private/auth: anything
      #   # Input data to @auth function: the data as passed to the /__private/auth function
      # },
      '[refresh]': {
        # Input data to /__private/auth: a JWT previously issued by us using the same secret (this JWT can be expired)
        # Input data to @auth function: the deserialized dictionary from the potentially-expired JWT
        '[exp_s]': int_range('(0,∞)')
      }
    }
  }
}

validate(OP_DEV_CONFIG, OP_DEV_CONFIG_FORMAT)
