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


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    birthdate = models.DateField()
    sexe = models.BooleanField() # bio sex
    gender = models.CharField(max_length=200) # gender identity
    weight = models.IntegerField()  # in g
    height = models.IntegerField()  # in cm
    picture = models.ImageField(upload_to="profiles")
    allergies = models.ManyToManyField("Ingredient", related_name="users")
    diet = models.ForeignKey(
        "Diet", on_delete=models.SET_NULL, null=True, blank=True, related_name="users"
    )
    objective = models.ForeignKey(
        "Objectif", on_delete=models.SET_NULL, null=True, related_name="users"
    )
    subscription = models.ForeignKey(
        "Subscription", related_name="users", on_delete=models.SET_NULL, null=True
    )
    # exercise_plans / exercises [old]
    #   will be better to have an object "exercise plan"
    #   wich store the current plan for the user along
    #   with the date of plan generation.


class Objectif(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    image_url = models.ImageField("objectives")


class Subscription(models.Model):
    # Plan
    name = models.CharField(max_length=200)
    price = models.FloatField()  # in USD
    description = models.TextField()
    image_url = models.ImageField(upload_to="subscriptions")


class Diet(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()


class Ingredient(models.Model):
    # Aliment
    name = models.CharField(max_length=200)
    description = models.TextField()
    type = models.CharField(max_length=50)
    calories = models.IntegerField()
    proteins = models.IntegerField()  # in mg/100g
    lipids = models.IntegerField()  # in mg/100g
    fibers = models.IntegerField()  # in mg/100g
    sugars = models.IntegerField()  # in mg/100g
    salts = models.IntegerField()  # in mg/100g
    is_allergen = models.BooleanField(default=False)
    diet = models.ForeignKey(
        Diet,
        related_name="ingredients",
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )


class Recipe(models.Model):
    nom = models.CharField(max_length=300)
    minutes_preparation = models.IntegerField()
    minutes_cuisson = models.IntegerField()
    summary = models.TextField()
    steps = models.TextField()
    picture = models.ImageField("recipes")


class ExercisePlan(models.Model):
    created_date = models.DateTimeField(default=datetime.now, editable=False)
    exercises = models.ManyToManyField("Exercise", related_name="plans")
    user = models.ForeignKey(
        UserProfile, related_name="plans", on_delete=models.CASCADE
    )


class Exercise(models.Model):
    name = models.CharField(max_length=200)
    instuctions = models.TextField()
    image = models.ImageField(upload_to="exercise")
    equipements = models.ManyToManyField("Equipement", related_name="exercises")
    bodyparts = models.ManyToManyField("Bodypart", related_name="exercises")
    muscles = models.ManyToManyField("Muscle", related_name="exercises")


class Equipement(models.Model):
    name = models.CharField(max_length=200)


class Bodypart(models.Model):
    name = models.CharField(max_length=200)


class Muscle(models.Model):
    name = models.CharField(max_length=200)
