from django.db import models

# Create your models here.
from users.models import User
from wagtail.models import Page


class Note(models.Model):
    text = models.TextField()


class Bookmark(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    note = models.OneToOneField(Note, null=True, on_delete=models.SET_NULL)

    class Meta:
        abstract = True


class ContentBlockBookmark(Bookmark):
    uuid = models.UUIDField(unique=False)
    content_block = models.ForeignKey("books.ContentBlock", on_delete=models.CASCADE)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["uuid", "content_block", "user"],
                name="unique_content_bookmark_per_user",
            )
        ]


class ModuleBookmark(Bookmark):
    module = models.ForeignKey("books.Module", on_delete=models.CASCADE)


class ChapterBookmark(Bookmark):
    chapter = models.ForeignKey("books.Chapter", on_delete=models.CASCADE)


class InstrumentBookmark(Bookmark):
    uuid = models.UUIDField(unique=False)
    instrument = models.ForeignKey(
        "basicknowledge.BasicKnowledge", on_delete=models.CASCADE
    )

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["uuid", "instrument", "user"],
                name="unique_instrument_bookmark_per_user",
            )
        ]


class Highlight(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    page = models.ForeignKey(Page, on_delete=models.CASCADE, related_name="highlights")
    # see highlight.ts for comments
    content_index = models.IntegerField(null=True)
    content_uuid = models.UUIDField(null=True)
    paragraph_index = models.IntegerField()
    start_position = models.IntegerField()
    selection_length = models.IntegerField()
    text = models.TextField()
    note = models.OneToOneField(Note, null=True, on_delete=models.SET_NULL)
    color = models.CharField(max_length=50)

    def __str__(self) -> str:
        return self.text
