import uuid
from django.db import models
from editormd.models import EditorMdField
from mptt.models import MPTTModel, TreeForeignKey
import reversion
from django.conf import settings


class Version(MPTTModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=250, unique=True)
    slug = models.CharField(max_length=250, unique=True)
    published = models.BooleanField(default=True)
    document_domain = models.ManyToManyField('DocumentDomain')
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['name']

    def __str__(self):
        return self.name


class DocumentDomain(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=250, unique=True)
    slug = models.SlugField(max_length=250, unique=True)
    published = models.BooleanField(default=True)
    last_modified = models.DateTimeField(auto_now=True, editable=False)
    prev_rev = models.UUIDField(blank=True, null=True)

    def __str__(self):
        return self.name


class Collection(MPTTModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=250, unique=True)
    slug = models.SlugField(max_length=250, unique=True)
    published = models.BooleanField(default=True)
    version = TreeForeignKey(Version)
    domain = models.ForeignKey(DocumentDomain)
    last_modified = models.DateTimeField(auto_now=True, editable=False)
    prev_rev = models.UUIDField(blank=True, null=True)
    parent = TreeForeignKey('self', null=True, blank=True,
                            related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['name']

    def __str__(self):
        return self.name


@reversion.register()
class Page(MPTTModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=250)
    slug = models.CharField(max_length=250)
    content = EditorMdField()
    published = models.BooleanField()
    last_modified = models.DateTimeField(auto_now=True, editable=False)
    prev_rev = models.UUIDField(blank=True, null=True)
    collection = TreeForeignKey(Collection, null=True, blank=True)
    document_domain = models.ForeignKey(DocumentDomain)
    version = models.ForeignKey(Version)
    parent = TreeForeignKey('self', null=True, blank=True,
                            related_name='children', db_index=True)
    # author = models.ForeignKey(user, editable=False)

    class MPTTMeta:
        order_insertion_by = ['title']


    def __str__(self):
        return self.title


    def get_content(self):
        return self.content


    def get_absolute_url(self):
        from django.urls import reverse
        return reverse('atlas_doc-view-page', args=[self.document_domain.slug, self.version.slug, self.slug])


    def get_collections(self):
        collections = [
            {"slug": "test", "name": "Col 1"},
            {"slug": "test", "name": "Col 2"},
            {"slug": "test", "name": "Col 3"}
        ]
        return collections
