from django.contrib.auth import get_user_model
from django.db import models
from django.contrib.postgres.fields import JSONField
from wagtail.snippets.models import register_snippet


@register_snippet
class Survey(models.Model):
    title = models.CharField(max_length=255)
    module = models.ForeignKey('books.Module', related_name='surveys', on_delete=models.CASCADE, null=True,
                               blank=True)
    data = JSONField()

    def __str__(self):
        return self.title


class Answer(models.Model):
    owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name='answers')
    data = JSONField()
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE, related_name='answers')

    def __str__(self):
        return '{} - {}'.format(self.owner.username, self.survey.title)
