# Copyright (C) 2018-2025 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later

import json
from datetime import timedelta

from django.db import models, transaction
from django.core.exceptions import ValidationError
from django.utils import timezone
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from polymorphic.models import PolymorphicModel
from member.models import Club
from activity.models import Activity
from note.models import Transaction


class Allergen(models.Model):
    """
    Allergen and alimentary restrictions
    """
    name = models.CharField(
        verbose_name=_('name'),
        max_length=255,
    )

    class Meta:
        verbose_name = _("Allergen")
        verbose_name_plural = _("Allergens")

    def __str__(self):
        return self.name


class Food(PolymorphicModel):
    """
    Describe any type of food
    """
    name = models.CharField(
        verbose_name=_("name"),
        max_length=255,
    )

    owner = models.ForeignKey(
        Club,
        on_delete=models.PROTECT,
        related_name='+',
        verbose_name=_('owner'),
    )

    allergens = models.ManyToManyField(
        Allergen,
        blank=True,
        verbose_name=_('allergens'),
    )

    traces = models.ManyToManyField(
        Allergen,
        blank=True,
        verbose_name=_('traces'),
        related_name='food_with_traces'
    )

    expiry_date = models.DateTimeField(
        verbose_name=_('expiry date'),
        null=False,
    )

    end_of_life = models.CharField(
        blank=True,
        verbose_name=_('end of life'),
        max_length=255,
    )

    is_ready = models.BooleanField(
        verbose_name=_('is ready'),
        max_length=255,
    )

    order = models.CharField(
        blank=True,
        verbose_name=_('order'),
        max_length=255,
    )

    def __str__(self):
        return self.name

    @transaction.atomic
    def update_allergens(self):
        # update parents
        for parent in self.transformed_ingredient_inv.iterator():
            old_allergens = list(parent.allergens.all()).copy()
            parent.allergens.clear()
            for child in parent.ingredients.iterator():
                if child.pk != self.pk:
                    parent.allergens.set(parent.allergens.union(child.allergens.all()))
            parent.allergens.set(parent.allergens.union(self.allergens.all()))
            if old_allergens != list(parent.allergens.all()):
                parent.save(old_allergens=old_allergens)

    @transaction.atomic
    def update_traces(self):
        # update parents
        for parent in self.transformed_ingredient_inv.iterator():
            old_traces = list(parent.traces.all()).copy()
            parent.traces.clear()
            for child in parent.ingredients.iterator():
                if child.pk != self.pk:
                    parent.traces.set(parent.traces.union(child.traces.all()))
            parent.traces.set(parent.traces.union(self.traces.all()))
            if old_traces != list(parent.traces.all()):
                parent.save(old_traces=old_traces)

    def update_expiry_date(self):
        # update parents
        for parent in self.transformed_ingredient_inv.iterator():
            old_expiry_date = parent.expiry_date
            parent.expiry_date = parent.shelf_life + parent.creation_date
            for child in parent.ingredients.iterator():
                if (child.pk != self.pk
                    and not (child.polymorphic_ctype.model == 'basicfood'
                             and child.date_type == 'DDM')):
                    parent.expiry_date = min(parent.expiry_date, child.expiry_date)

            if self.polymorphic_ctype.model == 'basicfood' and self.date_type == 'DLC':
                parent.expiry_date = min(parent.expiry_date, self.expiry_date)
            if old_expiry_date != parent.expiry_date:
                parent.save()

    class Meta:
        verbose_name = _('Food')
        verbose_name_plural = _('Foods')


class BasicFood(Food):
    """
    A basic food is a food directly buy and stored
    """
    arrival_date = models.DateTimeField(
        default=timezone.now,
        verbose_name=_('arrival date'),
    )

    date_type = models.CharField(
        max_length=255,
        choices=(
            ("DLC", "DLC"),
            ("DDM", "DDM"),
        )
    )

    @transaction.atomic
    def save(self, force_insert=False, force_update=False, using=None, update_fields=None, **kwargs):
        created = self.pk is None
        if not created:
            # Check if important fields are updated
            old_food = Food.objects.select_for_update().get(pk=self.pk)
            if not hasattr(self, "_force_save"):
                # Allergens

                if ('old_allergens' in kwargs
                        and list(self.allergens.all()) != kwargs['old_allergens']):
                    self.update_allergens()

                if ('old_traces' in kwargs
                        and list(self.traces.all()) != kwargs['old_traces']):
                    self.update_traces()

                # Expiry date
                if ((self.expiry_date != old_food.expiry_date
                        and self.date_type == 'DLC')
                        or old_food.date_type != self.date_type):
                    self.update_expiry_date()

        return super().save(force_insert, force_update, using, update_fields)

    @staticmethod
    def get_lastests_objects(number, distinct_field, order_by_field):
        """
        Get the last object with distinct field and ranked with order_by
        This methods exist because we can't distinct with one field and
        order with another
        """
        foods = BasicFood.objects.order_by(order_by_field).all()
        field = []
        for food in foods:
            if getattr(food, distinct_field) in field:
                continue
            else:
                field.append(getattr(food, distinct_field))
                number -= 1
                yield food
            if not number:
                return

    class Meta:
        verbose_name = _('Basic food')
        verbose_name_plural = _('Basic foods')

    def __str__(self):
        return self.name


class TransformedFood(Food):
    """
    A transformed food is a food with ingredients
    """
    creation_date = models.DateTimeField(
        default=timezone.now,
        verbose_name=_('creation date'),
    )

    # Without microbiological analyzes, the storage time is 3 days
    shelf_life = models.DurationField(
        default=timedelta(days=3),
        verbose_name=_('shelf life'),
    )

    ingredients = models.ManyToManyField(
        Food,
        blank=True,
        symmetrical=False,
        related_name='transformed_ingredient_inv',
        verbose_name=_('transformed ingredient'),
    )

    def check_cycle(self, ingredients, origin, checked):
        for ingredient in ingredients:
            if ingredient == origin:
                # We break the cycle
                self.ingredients.remove(ingredient)
            if ingredient.polymorphic_ctype.model == 'transformedfood' and ingredient not in checked:
                ingredient.check_cycle(ingredient.ingredients.all(), origin, checked)
                checked.append(ingredient)

    @transaction.atomic
    def save(self, force_insert=False, force_update=False, using=None, update_fields=None, **kwargs):
        created = self.pk is None
        if not created:
            # Check if important fields are updated
            update = {'allergens': False, 'traces': False, 'expiry_date': False}
            old_food = Food.objects.select_for_update().get(pk=self.pk)
            if not hasattr(self, "_force_save"):
                # Allergens
                # Unfortunately with the many-to-many relation we can't access
                # to old allergens
                if ('old_allergens' in kwargs
                        and list(self.allergens.all()) != kwargs['old_allergens']):
                    update['allergens'] = True

                if ('old_traces' in kwargs
                        and list(self.traces.all()) != kwargs['old_traces']):
                    update['traces'] = True

                # Expiry date
                update['expiry_date'] = (self.shelf_life != old_food.shelf_life
                                         or self.creation_date != old_food.creation_date)
                if update['expiry_date']:
                    self.expiry_date = self.creation_date + self.shelf_life
                # Unfortunately with the set method ingredients are already save,
                # we check cycle after if possible
                if ('old_ingredients' in kwargs
                        and list(self.ingredients.all()) != list(kwargs['old_ingredients'])):
                    update['allergens'] = True
                    update['traces'] = True
                    update['expiry_date'] = True

                    # it's preferable to keep a queryset but we allow list too
                    if type(kwargs['old_ingredients']) is list:
                        kwargs['old_ingredients'] = Food.objects.filter(
                            pk__in=[food.pk for food in kwargs['old_ingredients']])
                    self.check_cycle(self.ingredients.all().difference(kwargs['old_ingredients']), self, [])
                if update['allergens']:
                    self.update_allergens()
                if update['traces']:
                    self.update_traces()
                if update['expiry_date']:
                    self.update_expiry_date()

        if created:
            self.expiry_date = self.shelf_life + self.creation_date

            # We save here because we need pk for many-to-many relation
            super().save(force_insert, force_update, using, update_fields)

            for child in self.ingredients.iterator():
                self.allergens.set(self.allergens.union(child.allergens.all()))
                self.traces.set(self.traces.union(child.traces.all()))
                if not (child.polymorphic_ctype.model == 'basicfood' and child.date_type == 'DDM'):
                    self.expiry_date = min(self.expiry_date, child.expiry_date)
        return super().save(force_insert=False, force_update=force_update, using=using, update_fields=update_fields)

    class Meta:
        verbose_name = _('Transformed food')
        verbose_name_plural = _('Transformed foods')

    def __str__(self):
        return self.name


class QRCode(models.Model):
    """
    QR-code for register food
    """
    qr_code_number = models.PositiveIntegerField(
        unique=True,
        verbose_name=_('qr code number'),
    )

    food_container = models.ForeignKey(
        Food,
        on_delete=models.CASCADE,
        related_name='QR_code',
        verbose_name=_('food container'),
    )

    class Meta:
        verbose_name = _('QR-code')
        verbose_name_plural = _('QR-codes')

    def __str__(self):
        return _('QR-code number') + ' ' + str(self.qr_code_number)


class Dish(models.Model):
    """
    A dish is a food proposed during a meal
    """
    main = models.ForeignKey(
        TransformedFood,
        on_delete=models.PROTECT,
        related_name='dishes_as_main',
        verbose_name=_('main food'),
    )

    price = models.PositiveIntegerField(
        verbose_name=_('price')
    )

    activity = models.ForeignKey(
        Activity,
        on_delete=models.CASCADE,
        related_name='dishes',
        verbose_name=_('activity'),
    )

    available = models.BooleanField(
        default=True,
        verbose_name=_('available'),
    )

    class Meta:
        verbose_name = _('Dish')
        verbose_name_plural = _('Dishes')
        unique_together = ('main', 'activity')

    def __str__(self):
        return self.main.name + ' (' + str(self.activity) + ')'

    def save(self, *args, **kwargs):
        "Check the type of activity"
        if self.activity.activity_type.name != 'Perm bouffe':
            raise ValidationError(_('(You cannot select this type of activity.'))

        return super().save(*args, **kwargs)


class Supplement(models.Model):
    """
    A supplement is a food added to a dish
    """
    dish = models.ForeignKey(
        Dish,
        on_delete=models.CASCADE,
        related_name='supplements',
        verbose_name=_('dish'),
    )

    food = models.ForeignKey(
        Food,
        on_delete=models.PROTECT,
        related_name='supplements',
        verbose_name=_('food'),
    )

    price = models.PositiveIntegerField(
        verbose_name=_('price')
    )

    class Meta:
        verbose_name = _('Supplement')
        verbose_name_plural = _('Supplements')

    def __str__(self):
        return _("Supplement {food} for {dish}").format(
            food=str(self.food), dish=str(self.dish))

    def save(self, *args, **kwargs):
        # Check the owner of the food
        if self.food.owner != self.dish.main.owner:
            raise ValidationError(_('You cannot select food that belongs to the same club than the main food.'))
        return super().save(*args, **kwargs)


class Order(models.Model):
    """
    An order is a dish ordered by a member during an activity
    """
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='food_orders',
        verbose_name=_('user'),
    )

    activity = models.ForeignKey(
        Activity,
        on_delete=models.CASCADE,
        related_name='food_orders',
        verbose_name=_('activity'),
    )

    dish = models.ForeignKey(
        Dish,
        on_delete=models.CASCADE,
        related_name='orders',
        verbose_name=_('dish'),
    )

    supplements = models.ManyToManyField(
        Supplement,
        related_name='orders',
        verbose_name=_('supplements'),
        blank=True,
    )

    request = models.TextField(
        blank=True,
        verbose_name=_('request'),
        help_text=_('A specific request (to remove an ingredient for example)')
    )

    number = models.PositiveIntegerField(
        verbose_name=_('number'),
        default=1,
    )

    ordered_at = models.DateTimeField(
        default=timezone.now,
        verbose_name=_('order date'),
    )

    served = models.BooleanField(
        default=False,
        verbose_name=_('served'),
    )

    served_at = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name=_('served date'),
    )

    class Meta:
        verbose_name = _('Order')
        verbose_name_plural = _('Orders')
        unique_together = ('activity', 'number', )

    @property
    def amount(self):
        return self.dish.price + sum(s.price for s in self.supplements.all())

    def __str__(self):
        return _("Order of {dish} by {user}").format(
            dish=str(self.dish),
            user=str(self.user))

    def save(self, *args, **kwargs):
        if self.activity != self.dish.activity:
            raise ValidationError(_('Activities must be the same.'))
        created = self.pk is None
        if created:
            last_order = Order.objects.filter(activity=self.activity).last()
            if last_order is None:
                self.number = 1
            else:
                self.number = last_order.number + 1
            super().save(*args, **kwargs)

            transaction = FoodTransaction(
                order=self,
                source=self.user.note,
                destination=self.activity.organizer.note,
                amount=self.amount,
                quantity=1,
                reason=str(self.dish),
            )
            transaction.save()
        else:
            old_object = Order.objects.get(pk=self.pk)
            if not old_object.served and self.served:
                self.served_at = timezone.now()
            self.transaction.save()
            super().save(*args, **kwargs)


class FoodTransaction(Transaction):
    """
    Special type of :model:`note.Transaction` associated to a :model:`food.Order`.
    """
    order = models.OneToOneField(
        Order,
        on_delete=models.PROTECT,
        related_name='transaction',
        verbose_name=_('order')
    )

    class Meta:
        verbose_name = _("food transaction")
        verbose_name_plural = _("food transactions")

    def save(self, *args, **kwargs):
        self.valid = self.order.served
        super().save(*args, **kwargs)


class Recipe(models.Model):
    """
    A recipe is a list of ingredients one can use to easily create a recurrent TransformedFood
    """
    name = models.CharField(
        verbose_name=_("name"),
        max_length=255,
    )

    ingredients_json = models.TextField(
        blank=True,
        default="[]",
        verbose_name=_("list of ingredients"),
        help_text=_("Ingredients of the recipe, encoded in JSON")
    )

    creater = models.ForeignKey(
        Club,
        on_delete=models.CASCADE,
        verbose_name=_("creater"),
    )

    class Meta:
        verbose_name = _("Recipe")
        verbose_name_plural = _("Recipes")
        unique_together = ('name', 'creater',)

    def __str__(self):
        return "{name} ({creater})".format(name=self.name, creater=str(self.creater))

    @property
    def ingredients(self):
        """
        Ingredients are stored in a JSON string
        """
        return json.loads(self.ingredients_json)

    @ingredients.setter
    def ingredients(self, ingredients):
        """
        Store ingredients as JSON string
        """
        self.ingredients_json = json.dumps(ingredients, indent=2)
