from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Questions(models.Model):
    # by default null=True,blank=True only
    title = models.TextField(null=True,blank=True)
    status = models.CharField(default="inactive",max_length=10)
    created_by = models.ForeignKey(User,null=True,blank=True,on_delete=models.CASCADE)
    start_date = models.DateTimeField(null=True,blank=True)
    end_date = models.DateTimeField(null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return  self.title
    @property
    def choices(self):
        # Fetching all chices of particular question
        return self.choice_set.all()


class Choice(models.Model):
    Question = models.ForeignKey('poll.Questions', on_delete=models.CASCADE)
    text = models.TextField(null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return  self.text
    @property
    def votes(self):
        return self.answer_set.count()

class Answer(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    choice = models.ForeignKey(Choice, on_delete=models.CASCADE)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return  self.user.first_name + "_" +self.choice.text

