from django.db import models
from django.urls import reverse
from datetime import date
from django.contrib.auth.models import User                       #! 1 - Import user models

MEALS = (
    ('B', 'Breakfast'),
    ('L', 'Lunch'),
    ('D', 'Dinner')
)

class Toy(models.Model):
  name = models.CharField(max_length=50)
  color = models.CharField(max_length=20)

  def __str__(self):
    return self.name

  def get_absolute_url(self):
    return reverse('toys_detail', kwargs={'pk': self.id})

class Cat(models.Model):
  name = models.CharField(max_length=100)
  breed = models.CharField(max_length=100)
  description = models.TextField(max_length=250)
  age = models.IntegerField()
  toys = models.ManyToManyField(Toy)
  user = models.ForeignKey(User, on_delete=models.CASCADE)          #+ 1.1 - Add user as ForeignKey (URL mapping, we use this to point to our class based views)

  def __str__(self):
    return self.name

  def get_absolute_url(self):
    return reverse('detail', kwargs={'cat_id': self.id})

  def fed_for_today(self):
    return self.feeding_set.filter(date=date.today()).count() >= len(MEALS)

class Feeding(models.Model):
  date = models.DateField('feeding date')
  meal = models.CharField(
    max_length=1,
    choices=MEALS,
    default=MEALS[0][0]
  )
  cat = models.ForeignKey(Cat, on_delete=models.CASCADE)

  def __str__(self):
    # Nice method for obtaining the friendly value of a Field.choice
    return f"{self.get_meal_display()} on {self.date}"

  # change the default sort
  class Meta:
    ordering = ['-date']

class Photo(models.Model):
  url = models.CharField(max_length=200)
  cat = models.ForeignKey(Cat, on_delete=models.CASCADE)

  def __str__(self):
    return f"Photo for cat_id: {self.cat_id} @{self.url}"
[(13, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import models\n'), (14, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import models\n'), (23, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import models\n'), (24, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import models\n'), (25, 'django.db.models.TextField', 'models.TextField', False, 'from django.db import models\n'), (26, 'django.db.models.IntegerField', 'models.IntegerField', False, 'from django.db import models\n'), (27, 'django.db.models.ManyToManyField', 'models.ManyToManyField', False, 'from django.db import models\n'), (28, 'django.db.models.ForeignKey', 'models.ForeignKey', False, 'from django.db import models\n'), (40, 'django.db.models.DateField', 'models.DateField', False, 'from django.db import models\n'), (41, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import models\n'), (46, 'django.db.models.ForeignKey', 'models.ForeignKey', False, 'from django.db import models\n'), (57, 'django.db.models.CharField', 'models.CharField', False, 'from django.db import models\n'), (58, 'django.db.models.ForeignKey', 'models.ForeignKey', False, 'from django.db import models\n'), (20, 'django.urls.reverse', 'reverse', False, 'from django.urls import reverse\n'), (34, 'django.urls.reverse', 'reverse', False, 'from django.urls import reverse\n')]