# Path: chat/field.py
# class Field(object):
#     def __init__(self, name, field_type):
#         self.name = name
#         self.field_type = field_type
# 
#     def to_dict(self):
#         return {'name': self.name,
#                 'type': self.field_type}
# 
# Path: chat/schema.py
# class Schema(object):
#     def __init__(self, record_type, fields):
#         self.record_type = record_type
#         self.fields = fields
# 
#     def to_dict(self):
#         return {self.record_type:
#                 {'fields': [field.to_dict() for field in self.fields]}}
# 
# class SchemaHelper(object):
#     def __init__(self, container):
#         self.container = container
# 
#     def create(self, schemas, plugin_request=False):
#         record_types = {}
#         for schema in schemas:
#             record_types.update(schema.to_dict())
#         payload = {'record_types': record_types}
#         return self.container.send_action('schema:create',
#                                           payload,
#                                           plugin_request=plugin_request)
# 
# Path: chat/utils.py
# def _get_schema_name():
#     return "app_%s" % skyoptions.appname


from psycopg2.extensions import AsIs
from skygear.container import SkygearContainer
from skygear.options import options as skyoptions
from skygear.utils import db
from .field import Field
from .schema import Schema, SchemaHelper
from .utils import _get_schema_name
import skygear




def register_initialization_event_handlers(settings):
    def _base_message_fields():
        return [Field('attachment', 'asset'),
                Field('body', 'string'),
                Field('metadata', 'json'),
                Field('conversation', 'ref(conversation)'),
                Field('message_status', 'string'),
                Field('seq', 'sequence'),
                Field('revision', 'number'),
                Field('edited_by', 'ref(user)'),
                Field('edited_at', 'datetime')]

    def _message_schema():
        fields = _base_message_fields() + [Field('deleted', 'boolean')]
        return Schema('message', fields)

    def _message_history_schema():
        fields = _base_message_fields() + [Field('parent', 'ref(message)')]
        return Schema('message_history', fields)

    @skygear.event("before-plugins-ready")
    def chat_plugin_init(config):
        container = SkygearContainer(api_key=skyoptions.masterkey)

# Path: chat/field.py
# class Field(object):
#     def __init__(self, name, field_type):
#         self.name = name
#         self.field_type = field_type
# 
#     def to_dict(self):
#         return {'name': self.name,
#                 'type': self.field_type}
# 
# Path: chat/schema.py
# class Schema(object):
#     def __init__(self, record_type, fields):
#         self.record_type = record_type
#         self.fields = fields
# 
#     def to_dict(self):
#         return {self.record_type:
#                 {'fields': [field.to_dict() for field in self.fields]}}
# 
# class SchemaHelper(object):
#     def __init__(self, container):
#         self.container = container
# 
#     def create(self, schemas, plugin_request=False):
#         record_types = {}
#         for schema in schemas:
#             record_types.update(schema.to_dict())
#         payload = {'record_types': record_types}
#         return self.container.send_action('schema:create',
#                                           payload,
#                                           plugin_request=plugin_request)
# 
# Path: chat/utils.py
# def _get_schema_name():
#     return "app_%s" % skyoptions.appname


# Path: chat/initialize.py
from psycopg2.extensions import AsIs
from skygear.container import SkygearContainer
from skygear.options import options as skyoptions
from skygear.utils import db
from .field import Field
from .schema import Schema, SchemaHelper
from .utils import _get_schema_name
import skygear





def register_initialization_event_handlers(settings):
    def _base_message_fields():
        return [Field('attachment', 'asset'),
                Field('body', 'string'),
                Field('metadata', 'json'),
                Field('conversation', 'ref(conversation)'),
                Field('message_status', 'string'),
                Field('seq', 'sequence'),
                Field('revision', 'number'),
                Field('edited_by', 'ref(user)'),
                Field('edited_at', 'datetime')]

    def _message_schema():
        fields = _base_message_fields() + [Field('deleted', 'boolean')]
        return Schema('message', fields)

    def _message_history_schema():
        fields = _base_message_fields() + [Field('parent', 'ref(message)')]
        return Schema('message_history', fields)

    @skygear.event("before-plugins-ready")
    def chat_plugin_init(config):
        container = SkygearContainer(api_key=skyoptions.masterkey)

        schema_helper = SchemaHelper(container)