# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from woodbox.db import db
from woodbox.push_service import NotificationService

from .document_model import DocumentModel
from .node_model import NodeModel

class DocumentNodeModel(NodeModel):
    id = db.Column(db.Integer, db.ForeignKey('node_model.id'), primary_key=True)

    document_id = db.Column(db.Integer, db.ForeignKey('document_model.id'), nullable=False)
    document = db.relationship(DocumentModel,
                               foreign_keys='DocumentNodeModel.document_id', single_parent=True,
                               cascade='all, delete-orphan')

    __mapper_args__ = {
        'polymorphic_identity': 'document_node_model',
    }


NotificationService.register_model(DocumentNodeModel)
        if cls._root_id is None:
            root = FolderNodeModel.query.filter_by(title='root').order_by(FolderNodeModel.id).first()
            cls._root_id = root.id
        return cls._root_id


    def checkDeletePrecondition(self):
        if self.content:
            return "Folder is not empty. Delete or move content before deleting the folder."
        return None


class FolderNodeModelInitializer(DatabaseInitializer):
    dependencies = [UserModelInitializer]

    """Database initializer for folders: insert the root folder."""
    @staticmethod
    def do_init():
        root = FolderNodeModel(title='root', owner_id=UserModel.get_admin_id(), parent_node_id=None)
        db.session.add(root)
        db.session.commit()

        acl = RecordACLModel(record_type='ContentNode',
                             record_id=root.id,
                             user_role_id=UserModel.get_user_role_id(),
                             permission='read')
        db.session.add(acl)
        db.session.commit()

NotificationService.register_model(FolderNodeModel)