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


class Product(models.Model):
    """
    A product is an entity related to any food
    having nutritional consistency
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=30)
    # Proteins/fats/carbohydrates
    pfc = models.CharField(max_length=10)

    # Just get a listed nutritional consistency
    def get_pfc(self):
        return self.pfc.split("/")

    cal_value = models.IntegerField(default=0)


class Record(models.Model):
    """
    A record. It is a main entity in a diary,
    contains what and when a user ate
    """
    id = models.AutoField(primary_key=True)
    datetime = models.DateTimeField()
    user = models.ForeignKey(User)
    products = models.ManyToManyField(Product, through='RecordProducts')


class RecordProducts(models.Model):
    """
    Extending default many-to-many relationship table to add a weight for each product
    """
    record = models.ForeignKey(Record, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    weight = models.IntegerField()
