from django.db import models  
import os
from datetime import date, time


def symptom_picture_path(instance, filename):
    return os.path.join(
        "images/symptom_type/" + instance.keyName + "." + "png",
    )

def baby_size_picture_path(instance, filename):
    return os.path.join(
        "images/baby_size/" + instance.keyName + "." + "png",
    )

def pregnancy_week_picture_path(instance, filename):
    return os.path.join(
        "images/pregnancy_week/" + instance.keyName + "." + "png",
    )


# class Employee(models.Model):
#     fullName = models.TextField()
#     email = models.TextField()
#     password = models.CharField()
#     token = models.CharField()
#     createdDate = models.DateTimeField()
#     updatedDate = models.DateTimeField()

#     def __str__(self):
#         return str(self.fullName)

# Start Basic Data


class TokenType(models.Model):
    keyName = models.TextField()
    tokenTypeLen = models.IntegerField(default=27)
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()

class DeviceOSType(models.Model):
    keyName = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()

class Language(models.Model):
    name = models.TextField()
    countryCode = models.CharField(max_length=10)
    languageCode = models.CharField(max_length=10)
    isEnabled = models.BooleanField(default=False)
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()

    def __str__(self):
        return str(self.name)


class Gender(models.Model):
    keyName = models.TextField()


class GenderLanguage(models.Model):
    name = models.TextField()
    genderId = models.ForeignKey(
        Gender,
        on_delete=models.CASCADE,
        related_name="gender_genderlanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_genderlanguage",
    )


class ChildType(models.Model):
    keyName = models.TextField()


class ChildTypeLanguage(models.Model):
    name = models.TextField()
    childTypeId = models.ForeignKey(
        ChildType,
        on_delete=models.CASCADE,
        related_name="childtype_childtypelanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_childtypelanguage",
    )


class SymptomType(models.Model):
    keyName = models.TextField()
    image = models.ImageField(
        upload_to=symptom_picture_path,
        null=True,
        blank=True,
        default="/default/holder.jpg",
        max_length=255
    )
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()


class SymptomTypeLanguage(models.Model):
    name = models.TextField()
    symptomTypeId = models.ForeignKey(
        SymptomType,
        on_delete=models.CASCADE,
        related_name="symptomtype_symptomtypelanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_symptomtypelanguage",
    )


# Kick Tracker Status
class KickTrackerStatus(models.Model):
    keyName = models.TextField()
    count = models.IntegerField()
    color = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()


class KickTrackerStatusLanguage(models.Model):
    name = models.TextField()
    description = models.TextField()
    kickTrackerStatusId = models.ForeignKey(
        KickTrackerStatus,
        on_delete=models.CASCADE,
        related_name="kicktrackerstatus_kicktrackerstatuslanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_kicktrackerstatuslanguage",
    )


# BMI Cases
class BMICaseType(models.Model):
    keyName = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()


class BMICaseTypeLanguage(models.Model):
    name = models.TextField()
    bmiCaseTypeId = models.ForeignKey(
        BMICaseType,
        on_delete=models.CASCADE,
        related_name="bmicasetype_bmicasetypelanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_bmicasetypelanguage",
    )


class BMICase(models.Model):
    keyName = models.TextField()
    min = models.FloatField()
    max = models.FloatField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    bmiCaseTypeId = models.ForeignKey(
        BMICaseType,
        on_delete=models.CASCADE,
        related_name="bmicasetype_bmicase",
    )


class BMICaseLanguage(models.Model):
    name = models.TextField()
    bmiCaseId = models.ForeignKey(
        BMICase,
        on_delete=models.CASCADE,
        related_name="bmicase_bmicaselanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_bmicaselanguage",
    )


class BMICaseWeightGain(models.Model):
    min = models.FloatField()
    max = models.FloatField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    bmiCaseId = models.ForeignKey(
        BMICase,
        on_delete=models.CASCADE,
        related_name="bmicase_bmicaseweightgain",
    )


class CalculationMethod(models.Model):
    keyName = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()


class CalculationMethodLanguage(models.Model):
    name = models.TextField()
    calculationMethodId = models.ForeignKey(
        CalculationMethod,
        on_delete=models.CASCADE,
        related_name="calculationmethod_calculationmethodlanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_calculationmethodlanguage",
    )

#Pregnancy Week

class PregnancyWeek(models.Model):
    id = models.IntegerField(primary_key=True)
    keyName = models.TextField()
    weekNumber = models.IntegerField()
    trimester = models.IntegerField()
    image = models.ImageField(
        upload_to=pregnancy_week_picture_path,
        null=True,
        blank=True,
        default="/default/holder.jpg",
        max_length=255
    )
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()


class PregnancyWeekLanguage(models.Model):
    fetalDevelopment = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    pregnancyWeekId = models.ForeignKey(
        PregnancyWeek,
        on_delete=models.CASCADE,
        related_name="pregnancyweek_pregnancyweeklanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_pregnancyweeklanguage",
    )

#Pregnancy Symptom

class PregnancySymptom(models.Model):
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    pregnancyWeekId = models.ForeignKey(
        PregnancyWeek, 
        on_delete=models.CASCADE,
        related_name="pregnancyweek_pregnancySymptom",
    )

class PregnancySymptomLanguage(models.Model):
    description = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    pregnancySymptomId = models.ForeignKey(
        PregnancySymptom,
        on_delete=models.CASCADE,
        related_name="pregnancysymptom_pregnancySymptomlanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_pregnancySymptomlanguage",
    )

#Pregnancy Checklist

class PregnancyCheckList(models.Model):
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    pregnancyWeekId = models.ForeignKey(
        PregnancyWeek,
        on_delete=models.CASCADE,
        related_name="pregnancyweek_pregnancychecklist",
    )

class PregnancyCheckListLanguage(models.Model):
    description = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    pregnancyCheckListId = models.ForeignKey(
        PregnancyCheckList,
        on_delete=models.CASCADE,
        related_name="pregnancychecklist_pregnancychecklistlanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_pregnancychecklistlanguage",
    )

#Pregnancy Nutritions

class PregnancyNutrition(models.Model):
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    pregnancyWeekId = models.ForeignKey(
        PregnancyWeek,
        on_delete=models.CASCADE,
        related_name="pregnancyweek_pregnancyNutrition",
    )

class PregnancyNutritionLanguage(models.Model):
    description = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    pregnancyNutritionId = models.ForeignKey(
        PregnancyNutrition,
        on_delete=models.CASCADE,
        related_name="pregnancyNutrition_pregnancyNutritionlanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_pregnancyNutritionlanguage",
    )

#Pregnancy Baby Size

class PregnancyBabySize(models.Model):
    keyName = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    image = models.ImageField(
        upload_to=baby_size_picture_path,
        null=True,
        blank=True,
        default="/default/holder.jpg",
        max_length=255
    )
    pregnancyWeekId = models.ForeignKey(
        PregnancyWeek,
        on_delete=models.CASCADE,
        related_name="pregnancyweek_pregnancybabysize",
    )

class PregnancyBabySizeLanguage(models.Model):
    description = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()    
    pregnancyBabySizeId = models.ForeignKey(
        PregnancyBabySize,
        on_delete=models.CASCADE,
        related_name="pregnancybabysize_pregnancybabysizelanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_pregnancybabysizelanguage",
    )

#MonthlyTip

class MonthlyTip(models.Model):
    monthNumber = models.IntegerField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()

class MonthlyTipLanguage(models.Model):
    title = models.TextField()
    description = models.TextField()
    inspiration = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    monthlyTipId = models.ForeignKey(
        MonthlyTip,
        on_delete=models.CASCADE,
        related_name="monthlytip_monthlytiplanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_monthlytiplanguage",
    )

# Tip
class Tip(models.Model):
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()    
    monthlyTipId = models.ForeignKey(
        MonthlyTip,
        on_delete=models.CASCADE,
        related_name="monthlytip_tip",
    )


class TipLanguage(models.Model):
    description = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()    
    tipId = models.ForeignKey(
        Tip,
        on_delete=models.CASCADE,
        related_name="tip_tiplanguage",
    )
    languageId = models.ForeignKey(
        Language,
        on_delete=models.CASCADE,
        related_name="language_tiplanguage",
    )


# End Basic Data


# Start Parent Profile
class Parent(models.Model):
    phone = models.TextField()
    isTryingToBePregnant = models.BooleanField(default=False)
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    languageId = models.ForeignKey(
        Language, on_delete=models.CASCADE, related_name="language_parent"
    )

class ParentDetail(models.Model):
    firstName = models.TextField()
    lastName = models.TextField()
    password = models.TextField()
    email = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentId = models.OneToOneField(
        Parent, on_delete=models.CASCADE, related_name="parent_parentdetail",
        primary_key=True,
    )

class ParentToken(models.Model):
    token = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    tokenTypeId = models.ForeignKey(
        TokenType, on_delete=models.CASCADE, related_name="tokentype_parenttoken"
    )
    parentId = models.OneToOneField(
        Parent, on_delete=models.CASCADE, related_name="parent_parenttoken",
        primary_key=True,
    )

class Child(models.Model):
    isSelected = models.BooleanField(default=False)
    name = models.TextField()
    dateOfBirth = models.DateTimeField()
    weight = models.FloatField(default=0)
    length = models.FloatField(default=0)
    head = models.FloatField(default=0)
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    childTypeId = models.ForeignKey(
        ChildType, on_delete=models.CASCADE, related_name="childtype_child"
    )
    genderId = models.ForeignKey(
        Gender, on_delete=models.CASCADE, related_name="gender_child"
    )
    parentId = models.ForeignKey(
        Parent, on_delete=models.CASCADE, related_name="parent_child",
    )


class EmergencyContact(models.Model):
    fullName = models.TextField()
    phone = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentId = models.ForeignKey(
        Parent,
        on_delete=models.CASCADE,
        related_name="parent_emergencycontact",
    )


class ParentProfile(models.Model):
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    childId = models.OneToOneField(
        Child,
        on_delete=models.CASCADE,
        related_name="child_parentprofile",
        primary_key=True
    )

# End Parent Profile

# Tools and other features that use ParentProfile with Child

# Symptom Group
class SymptomGroup(models.Model):
    date = models.DateTimeField()
    note = models.TextField(blank=True, default="", null=True)
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentProfileId = models.ForeignKey(
        ParentProfile, on_delete=models.CASCADE, related_name="parentprofile_symptom"
    )


class SymptomRow(models.Model):
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    symptomTypeId = models.ForeignKey(
        SymptomType, on_delete=models.CASCADE, related_name="symptomtype_symptomrow"
    )
    symptomGroupId = models.ForeignKey(
        SymptomGroup, on_delete=models.CASCADE, related_name="symptomgroup_symptomrow"
    )


# Kick Tracker Group
class KickTrackerGroup(models.Model):
    date = models.DateTimeField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentProfileId = models.ForeignKey(
        ParentProfile,
        on_delete=models.CASCADE,
        related_name="parentprofile_kicktrackergroup",
    )


class KickTrackerRow(models.Model):
    date = models.DateTimeField()
    seconds = models.IntegerField(default=0)
    kicks = models.IntegerField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    kickTrackerGroupId = models.ForeignKey(
        KickTrackerGroup,
        on_delete=models.CASCADE,
        related_name="kicktrackergroup_kicktrackerrow",
    )


class WeightGain(models.Model):
    height = models.FloatField()
    prePregnancyWeight = models.FloatField()
    currentWeight = models.FloatField()
    currentWeek = models.IntegerField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    bmiCaseId = models.ForeignKey(
        BMICase, on_delete=models.CASCADE, related_name="bmicase_weightgain"
    )
    parentProfileId = models.ForeignKey(
        ParentProfile, on_delete=models.CASCADE, related_name="parentprofile_weightgain"
    )


class Ovulation(models.Model):
    lastMenstrualPeriod = models.DateTimeField()
    cycleLength = models.IntegerField()
    nextPeriod = models.DateTimeField()
    ovulationDay = models.DateTimeField()
    fertileStart = models.DateTimeField()
    fertileEnd = models.DateTimeField()
    expectedDueDate = models.DateTimeField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentProfileId = models.ForeignKey(
        ParentProfile, on_delete=models.CASCADE, related_name="parentprofile_ovulation"
    )


class DueDateCalculator(models.Model):
    #LMP
    lastMenstrualPeriod = models.DateTimeField()
    cycleLength = models.IntegerField()
    #Conception
    conceiveDate = models.DateTimeField()
    #IVF
    dateOfTransfer = models.DateTimeField()
    numOfDays = models.IntegerField()
    #Ultrasound
    ultrasoundDate = models.DateTimeField()
    weeks = models.IntegerField()
    days = models.IntegerField()
    #Output
    expectedDueDate = models.DateTimeField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()

    parentProfileId = models.ForeignKey(
        ParentProfile,
        on_delete=models.CASCADE,
        related_name="parentprofile_duedatecalculator",
    )
    calculationMethodId = models.ForeignKey(
        CalculationMethod,
        on_delete=models.CASCADE,
        related_name="calculationmethod_duedatecalculator",
    )

class Notes(models.Model):
    title = models.TextField()
    description = models.TextField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentProfileId = models.ForeignKey(
        ParentProfile,
        on_delete=models.CASCADE,
        related_name="parentprofile_notes",
    )


class Appointment(models.Model):
    title = models.TextField()
    doctorName = models.TextField()
    speciality = models.TextField()
    date = models.DateTimeField()
    dateTime = models.DateTimeField()
    isDone = models.BooleanField()
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentProfileId = models.ForeignKey(
        ParentProfile,
        on_delete=models.CASCADE,
        related_name="parentprofile_appointment",
    )

# Calender

# Todo
class Todo(models.Model):
    title = models.TextField()
    date = models.DateField()
    dateTime = models.DateTimeField()
    isDone = models.BooleanField(default=False)
    createdDate = models.DateTimeField()
    updatedDate = models.DateTimeField()
    parentProfileId = models.ForeignKey(
        ParentProfile,
        on_delete=models.CASCADE,
        related_name="parentprofile_todo",
    )
