from django.contrib import admin
from django.db import models
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class BceidUser(models.Model):
    """
    BCeID user table
    """

    user_guid = models.CharField(db_index=True, max_length=32, unique=True, blank=False)
    """ BCEID identifier for user """

    display_name = models.TextField(blank=True)
    """ BCEID display name """

    sm_user = models.TextField(blank=True)
    """ SiteMinder user value """

    date_joined = models.DateTimeField(default=timezone.now)
    """ First login timestamp """

    last_login = models.DateTimeField(default=timezone.now)
    """ Most recent login timestamp """

    has_seen_orders_page = models.BooleanField(default=False)
    """ Flag for intercept page """

    has_accepted_terms = models.BooleanField(default=False)
    """ Flag for accepting terms of service """

    def is_authenticated(self):
        return True

    def is_anonymous(self):
        return False

    is_staff = True

    is_active = True

    def has_module_perms(self, *args):
        return True

    def has_perm(self, *args):
        return True

    def __str__(self):
        return 'BCeID User %s' % self.user_guid


@python_2_unicode_compatible
class Question(models.Model):
    """
    Questions being asked of the user.
    """

    key = models.TextField(primary_key=True)
    """ Unique question identifier """

    name = models.TextField(blank=True)
    """ Readable name of question (n.b., NOT content) """

    description = models.TextField(blank=True)
    """ Extended description (n.b., NOT content) """

    summary_order = models.PositiveIntegerField(default=0)
    """ Convenience for listing these in the admin """

    required = models.TextField(blank=True)
    """ 'Required', 'Conditional', or '' [blank = not required] """

    conditional_target = models.TextField(blank=True)
    """ For conditional questions, this is the question it is conditional upon """

    reveal_response = models.TextField(blank=True)
    """ The value of the other question that makes this question required """

    class Meta:
        ordering = ('summary_order', )

    def __str__(self):
        return '%s: %s' % (self.key, self.name)


@python_2_unicode_compatible
class UserResponse(models.Model):
    """
    User input
    """

    bceid_user = models.ForeignKey(BceidUser, related_name='responses')
    """ User providing response """

    question = models.ForeignKey(Question, related_name='responses')
    """ Originating question """

    value = models.TextField(blank=True)
    """ The question's response from the user """

    class Meta:
        unique_together = ("bceid_user", "question")

    def __str__(self):
        return '%s -> %s' % (self.bceid_user, self.question.key)


admin.site.register(BceidUser)
admin.site.register(Question)
admin.site.register(UserResponse)
