from django.db import models
from core.models import TimeStampedModel 
from subjects.models import Subject
from researchers.models import Researcher
from surveys.models import Survey

class Treatment(TimeStampedModel):
    name = models.CharField(max_length=150)
    description = models.CharField(max_length=1024)
    participants = models.ManyToManyField(Researcher, through='Membership')
    enabled = models.BooleanField(default=True)

    def __str__(self):
        return self.name

class Treatment_Subjects(TimeStampedModel):
    treatment = models.ForeignKey(Treatment, on_delete=models.CASCADE)
    patient = models.ForeignKey(Subject, on_delete=models.CASCADE)
    added_by = models.ForeignKey(Researcher)

class Stage(TimeStampedModel):
    name = models.CharField(max_length=150)
    description = models.CharField(max_length=512)
    treatment = models.ForeignKey(Treatment, on_delete=models.CASCADE)
    subjects = models.ManyToManyField(Subject)
    surveys = models.ManyToManyField(Survey)

    def __str__(self):
        return self.name

class Membership(models.Model):
    LEAD = 'l'
    ASSISTANT = 'a'
    ROLE_IN_TREATMENT = ((LEAD, 'Lead'), (ASSISTANT, 'Assistant'))
    treatment = models.ForeignKey(Treatment, on_delete=models.CASCADE)
    researcher = models.ForeignKey(Researcher, on_delete=models.CASCADE)
    date_joined = models.DateField()
    role_in_treatment = models.CharField(max_length=1, choices=ROLE_IN_TREATMENT, default=ASSISTANT)

class Invitation(TimeStampedModel):
    email_invited = models.EmailField(verbose_name='e-mail')
    message = models.CharField(max_length=100)
    treatment = models.OneToOneField(Treatment)
    researcher = models.OneToOneField(Researcher)

    def __str__(self):
        return self.message

class Request(TimeStampedModel):
    message = models.CharField(max_length=255)
    subject = models.ForeignKey(Subject, null=True)
    researcher = models.ForeignKey(Researcher, null=True)
    processed = models.BooleanField(default=False)

    def __str__(self):
        return self.message

