from django.db import models
from django.contrib.auth.models import User

from . import validators


class Profile(models.Model):
    SEX_FEMALE = "F"
    SEX_MALE = "M"
    SEX_CHOICES = [
        (SEX_FEMALE, "Female"),
        (SEX_MALE, "Male")
    ]

    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    date_of_birth = models.DateField(validators=[validators.PersonValidator.date_of_birth], verbose_name="Date of birth")
    sex = models.CharField(max_length=1, choices=SEX_CHOICES, verbose_name="Sex assigned at birth")


class BloodPressureRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    recorded = models.DateTimeField(validators=[validators.BloodPressureRecordValidator.recorded], unique=True, verbose_name="Time at which measurement was taken")
    value_systolic_mmhg = models.PositiveIntegerField(validators=[validators.BloodPressureRecordValidator.value_systolic_mmhg], verbose_name="Systolic Blood Pressure (mmhg)")
    value_diastolic_mmhg = models.PositiveIntegerField(validators=[validators.BloodPressureRecordValidator.value_diastolic_mmhg], verbose_name="Diastolic Blood Pressure (mmhg)")


class BodyTempRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    recorded = models.DateTimeField(validators=[validators.BodyTempRecordValidator.recorded], unique=True, verbose_name="Time at which measurement was taken")
    value_celsius = models.DecimalField(max_digits=5, decimal_places=2, validators=[validators.BodyTempRecordValidator.value_celsius], unique=True, verbose_name="Body Temperature (\u00B0C)")


class HeartRateRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    recorded = models.DateTimeField(validators=[validators.HeartRateRecordValidator.recorded], unique=True, verbose_name="Time at which measurement was taken")
    value_bpm = models.PositiveIntegerField(validators=[validators.HeartRateRecordValidator.value_bpm], verbose_name="Heart Rate (bpm)")


class Spo2LevelRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    recorded = models.DateTimeField(validators=[validators.Spo2LevelRecordValidator.recorded], unique=True, verbose_name="Time at which measurement was taken")
    value_percent = models.PositiveIntegerField(validators=[validators.Spo2LevelRecordValidator.value_percent], verbose_name="SPO2 (\u0025)")


class RespirationScoreRecord(models.Model):
    SEVERITY_NONE = 0
    SEVERITY_LOW = 1
    SEVERITY_HIGH = 2
    SEVERITY_CHOICES = [
        (SEVERITY_NONE, "No shortness of breath"),
        (SEVERITY_LOW, "A little shortness of breath"),
        (SEVERITY_HIGH, "Severe shortness of breath"),
    ]

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    recorded = models.DateTimeField(validators=[validators.RespirationScoreRecordValidator.recorded], unique=True, verbose_name="Time at which measurement was taken")
    value_severity = models.PositiveIntegerField(choices=SEVERITY_CHOICES, validators=[validators.RespirationScoreRecordValidator.value_severity], verbose_name="Shortness Of Breath Severity")


class MewsRecord(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    recorded = models.DateTimeField(validators=[validators.MewsRecordValidator.recorded], unique=True, verbose_name="Time at which measurement was calculated")
    value_n = models.PositiveIntegerField(validators=[validators.MewsRecordValidator.value_n], verbose_name="Modified Early Warning Score")

    blood_pressure_record = models.ForeignKey(BloodPressureRecord, on_delete=models.CASCADE)
    body_temp_record = models.ForeignKey(BodyTempRecord, on_delete=models.CASCADE)
    heart_rate_record = models.ForeignKey(HeartRateRecord, on_delete=models.CASCADE)
    spo2_level_record = models.ForeignKey(Spo2LevelRecord, on_delete=models.CASCADE)
    respiration_score_record = models.ForeignKey(RespirationScoreRecord, on_delete=models.CASCADE)


    @staticmethod
    def calculate_mews_value(
        blood_pressure_systolic_value_mmhg: int,
        body_temp_value_celsius: float,
        heart_rate_value_bpm: int,
        spo2_level_value_percent: int,
        respiration_score_value_severity: int,
        ):
        mews_value = 0

        if blood_pressure_systolic_value_mmhg <= 70:
            mews_value += 3
        elif blood_pressure_systolic_value_mmhg <= 80:
            mews_value += 2
        elif blood_pressure_systolic_value_mmhg <= 100:
            mews_value += 1
        elif blood_pressure_systolic_value_mmhg < 200:
            mews_value += 0
        else:
            mews_value += 2

        if heart_rate_value_bpm < 40:
            mews_value += 2
        elif heart_rate_value_bpm <= 50:
            mews_value += 1
        elif heart_rate_value_bpm <= 100:
            mews_value += 0
        elif heart_rate_value_bpm <= 110:
            mews_value += 1
        elif heart_rate_value_bpm < 130:
            mews_value += 2
        else:
            mews_value += 3

        if respiration_score_value_severity == 1:
            mews_value += 1
        elif respiration_score_value_severity == 2:
            mews_value += 2

        if spo2_level_value_percent < 90:
            mews_value += 2
        elif spo2_level_value_percent < 95:
            mews_value += 1

        if body_temp_value_celsius < 35:
            mews_value += 2
        elif body_temp_value_celsius <= 38.4:
            mews_value += 0
        else:
            mews_value += 2

        return mews_value
