from django.db import models
from django.urls import reverse

INDEPENDENT = 'IN'
ASSISTED = 'AS'
TOTALLY_DEPENDENT = 'DE'

ASSISTANCE_CHOICES = [
    (INDEPENDENT, 'Independent'),
    (ASSISTED, 'Assisted'),
    (TOTALLY_DEPENDENT, 'Totally dependent'),
]

class Chart(models.Model):
    class Meta:
        ordering = ('-created_at',)

    patient = models.CharField(max_length=250)
    assignment = models.CharField(max_length=250)
    student_name = models.CharField(max_length=250)
    student_id = models.CharField(max_length=250)

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

    def get_absolute_url(self):
        return reverse('chart-detail', kwargs={'pk': self.pk})

    def __str__(self):
        return f"Chart {self.pk}, for {self.patient}"

class Entry(models.Model):
    class Meta:
        ordering = ('time',)
        verbose_name_plural = 'entries'

    time = models.TimeField()
    chart = models.ForeignKey(Chart, on_delete=models.CASCADE)
    notes = models.TextField(blank=True)

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

    def get_absolute_url(self):
        return reverse('chart-detail', kwargs={'pk': self.chart_id})

    def __str__(self):
        return f"{self.time}"


class OralCare(Entry):
    class Meta:
        verbose_name_plural = 'oral care'

    assistance = models.CharField(
        max_length=2,
        choices=ASSISTANCE_CHOICES,
        default=INDEPENDENT,
        blank=True
    )

    dentures_upper = models.BooleanField(default=False)
    dentures_lower = models.BooleanField(default=False)

class Bathing(Entry):
    class Meta:
        verbose_name_plural = 'bathing'

    METHOD_CHOICES = [
        ('SH', 'Shower'),
        ('TU', 'Tub'),
        ('PA', 'Partial'),
        ('BB', 'Bed bath'),
        ('SP', 'Specialty'),
    ]
    method = models.CharField(
        max_length=2,
        choices=METHOD_CHOICES,
        blank=True
    )
    method_other = models.CharField(max_length=250, blank=True)
    assistance = models.CharField(
        max_length=2,
        choices=ASSISTANCE_CHOICES,
        default=INDEPENDENT,
        blank=True
    )

class Toileting(Entry): 
    assistance = models.CharField(
        max_length=2,
        choices=ASSISTANCE_CHOICES,
        default=INDEPENDENT,
        blank=True
    )

    TOILETING_METHOD_CHOICES = [
        ('CO', 'Commode'),
        ('BP', 'Bed pan'),
        ('CA', 'Catheter'),
    ]
    method = models.CharField(
        max_length=2,
        choices=TOILETING_METHOD_CHOICES,
        blank=True
    )

    brief_change = models.BooleanField(default=False)
    perineal_care = models.BooleanField(default=False)
    catheter_care = models.BooleanField(default=False)


class Dressing(Entry):
    DRESSING_TIME_CHOICES = [
        ('AM', 'Morning'),
        ('PM', 'Evening'),
    ]

    time_of_day = models.CharField(
        max_length=2,
        choices=DRESSING_TIME_CHOICES,
        blank=True
    )

    assistance = models.CharField(
        max_length=2,
        choices=ASSISTANCE_CHOICES,
        default=INDEPENDENT,
        blank=True
    )


class Meal(Entry):
    KIND_CHOICES = [
        ('BR', 'Breakfast'),
        ('LU', 'Lunch'),
        ('DI', 'Dinner'),
        ('SN', 'Snack'),
    ]
    kind = models.CharField(
        max_length=2,
        choices=KIND_CHOICES,
        blank=True
    )
    amount_consumed = models.FloatField(blank=True, null=True)


class RangeOfMotion(Entry):
    MOTION_KIND_CHOICES = [
        ('PA', 'Passive'),
        ('AC', 'Active'),
        ('CO', 'Combination'),
    ]
    motion_kind = models.CharField(
        max_length=2,
        choices=MOTION_KIND_CHOICES,
        blank=True
    )
    antiembolic_stockings = models.BooleanField(default=False)
    turn_or_position_change = models.BooleanField(default=False)

class Ambulation(Entry):
    METHOD_CHOICES = [
        ('CH', 'Chair'),
        ('RO', 'Up in room'),
        ('HA', 'Ambulate in hall'),
        ('PT', 'To PT'),
    ]
    method = models.CharField(
        max_length=2,
        choices=METHOD_CHOICES,
        blank=True
    )

    ASSISTANCE_CHOICES = [
        ('IN', 'Independent'),
        ('ON', 'One assist with gait belt'),
        ('TW', 'Two assist'),
        ('LI', 'Lift'),
    ]
    assistance  = models.CharField(
        max_length=2,
        choices=ASSISTANCE_CHOICES,
        blank=True
    )
    DEVICES_USED_CHOICES = [
        ('WA', 'Walker'),
        ('CR', 'Crutches'),
        ('WH', 'Wheelchair'),
    ]
    devices_used = models.CharField(
        max_length=2,
        choices=DEVICES_USED_CHOICES,
        blank=True
    )


class Vitals(Entry):
    o2_saturation = models.PositiveIntegerField(blank=True, null=True)
    temperature = models.FloatField(blank=True, null=True)
    TEMPERATURE_UNITS_CHOICES = [
        ('CE', 'C'),
        ('FA', 'F'),
    ]
    temperature_units = models.CharField(
        max_length=2,
        choices=TEMPERATURE_UNITS_CHOICES,
        default='FA',
        blank=True,
        null=True
    )
    pulse = models.IntegerField(blank=True, null=True)
    respirations = models.IntegerField(blank=True, null=True)
    blood_pressure_systolic = models.IntegerField(blank=True, null=True)
    blood_pressure_diastolic = models.IntegerField(blank=True, null=True)

    weight = models.FloatField(blank=True, null=True)
    
    WEIGHT_UNITS_CHOICES = [
        ('LB', 'LBS'),
        ('KG', 'KG'),
    ]
    weight_units = models.CharField(
        max_length=2,
        choices=WEIGHT_UNITS_CHOICES,
        default='LB',
        blank=True,
        null=True
    )


class FluidIntake(Entry):
    fluid_type = models.CharField(max_length=100)
    intake_amount = models.PositiveIntegerField()
    npo = models.BooleanField(default=False)


class Urine(Entry):
    URINE_COLOR_CHOICES = [
        ('YE', 'Yellow'),
        ('DA', 'Dark yellow'),
        ('BR', 'Brown'),
    ]
    assistance = models.CharField(
        max_length=2,
        choices=ASSISTANCE_CHOICES,
        default=INDEPENDENT,
        blank=True
    )
    color = models.CharField(
        max_length=2,
        choices=URINE_COLOR_CHOICES,
        blank=True
    )
    amount = models.PositiveIntegerField()

class Emesis(Entry):
    amount = models.PositiveIntegerField()
    color = models.CharField(max_length=100, blank=True)

class BowelMovement(Entry):
    assistance = models.CharField(
        max_length=2,
        choices=ASSISTANCE_CHOICES,
        default=INDEPENDENT,
        blank=True
    )
    AMOUNT_CHOICES = [
        ('SM', 'Small'),
        ('MD', 'Moderate'),
        ('LG', 'Large'),
    ]
    amount = models.CharField(
        max_length=2,
        choices=AMOUNT_CHOICES,
        blank=True
    )
    color = models.CharField(max_length=100, blank=True)
    CONSISTENCY_CHOICES = [
        ('SO', 'Soft'),
        ('HD', 'Hard'),
        ('LQ', 'Liquid'),
        ('FM', 'Formed'),
        ('SF', 'Soft and formed'),
    ]
    consistency = models.CharField(
        max_length=2,
        choices=CONSISTENCY_CHOICES,
        blank=True
    )

class PainLevel(Entry):
    score = models.PositiveIntegerField(blank=True, null=True)
    reported_to_nurse = models.BooleanField(default=False)


class OxygenLevel(Entry):
    oxygen_level = models.FloatField(blank=True, null=True)
    DELIVERY_METHOD_CHOICES = [
        ('NA', 'Per nasal cannula'),
        ('MA', 'Per mask'),
        ('RE', 'Refused to wear'),
    ]
    delivery_method = models.CharField(
        max_length=2,
        choices=DELIVERY_METHOD_CHOICES,
        blank=True
    )


class Restraint(Entry):
    RESTRAINTS_USED_CHOICES = [
        ('TW', 'Side rails x2'),
        ('FO', 'Side rails x4'),
        ('NO', 'None'),
    ]
    restraints_used = models.CharField(
        max_length=2,
        choices=RESTRAINTS_USED_CHOICES,
        blank=True
    )
    restraints_soft = models.BooleanField(default=False)


class Safety(Entry):
    call_light_in_reach = models.BooleanField(default=False)
    bed_low = models.BooleanField(default=False)
    brakes_on = models.BooleanField(default=False)
    falls = models.CharField(max_length=999, blank=True)
    nausea = models.CharField(max_length=999, blank=True)
    vomiting = models.CharField(max_length=999, blank=True)
    confusion = models.CharField(max_length=999, blank=True)
    combative = models.CharField(max_length=999, blank=True)
