from django.db import models
from django.utils.timezone import now
from django.conf import settings
from typing import List, Set

class BaseModel(models.Model):
    creation_date = models.DateTimeField(default=now, editable=False)
    last_update = models.DateTimeField(default=now)
    related_user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL, related_name='+')
    last_updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL, related_name='+')

    class Meta:
        abstract = True

    def get_store_id(self):
        if isinstance(self, SideStoreModel):
            return self.store_id
        else:
            return None

    def get_children_by_model(self):
        """
        Returns a dictionary where:
        - keys are the model names
        - values are QuerySets of related objects
        """
        children = self._meta.get_fields(include_parents=False, include_hidden=False)
        related_objects = {}

        for child in children:
            if (child.one_to_many or child.one_to_one) and child.auto_created:
                model_name = child.related_model.__name__
                # print(f'>>> add children for {model_name}')
                related_objects[model_name] = getattr(self, child.name).all()

        return related_objects

    def get_parents_by_model(self):
        """
        Returns a dictionary where:
        - keys are the model names
        - values are the parent model instances
        """
        parents = {}

        # Get all fields including parent links
        fields = self._meta.get_fields(include_parents=True, include_hidden=True)

        for field in fields:
            # Check if the field is a parent link (OneToOne field that points to a parent model)
            if isinstance(field, models.OneToOneRel) and field.parent_link:
                model_name = field.related_model.__name__
                # Get the parent instance using the related name
                parent_instance = getattr(self, field.get_accessor_name())
                if parent_instance:
                    parents[model_name] = parent_instance

            # Also check for direct foreign key relationships that might represent parent relationships
            elif isinstance(field, models.ForeignKey):
                model_name = field.related_model.__name__
                parent_instance = getattr(self, field.name)
                if parent_instance:
                    parents[model_name] = parent_instance

        return parents

    def get_recursive_children(self, processed_objects: Set = None) -> List:
        """
        Recursively get all children objects through the hierarchy
        """
        if processed_objects is None:
            processed_objects = set()

        # Skip if we've already processed this object to avoid infinite recursion
        if self.pk in processed_objects:
            return []

        processed_objects.add(self.pk)
        children = []

        # Get immediate children
        children_by_model = self.get_children_by_model()
        for queryset in children_by_model.values():
            for child in queryset:
                children.append(child)
                # Recursively get children of children
                if isinstance(child, BaseModel):
                    children.extend(child.get_recursive_children(processed_objects))

        return children

    def get_recursive_parents(self, processed_objects: Set = None) -> List:
        """
        Recursively get all parent objects through the hierarchy
        """
        if processed_objects is None:
            processed_objects = set()

        # Skip if we've already processed this object to avoid infinite recursion
        if self.pk in processed_objects:
            return []

        processed_objects.add(self.pk)
        parents = []

        # Get immediate parents
        parents_by_model = self.get_parents_by_model()
        for parent in parents_by_model.values():
            parents.append(parent)
            # Recursively get parents of parents
            if isinstance(parent, BaseModel):
                parents.extend(parent.get_recursive_parents(processed_objects))

        return parents

    def related_instances(self):
        """
        Get all related instances (both children and parents) recursively
        """
        instances = []
        processed_objects = set()
        instances.extend(self.get_recursive_children(processed_objects))

        processed_objects = set()
        instances.extend(self.get_recursive_parents(processed_objects))

        return instances

class SideStoreModel(BaseModel):
    store_id = models.CharField(max_length=100, default="") # a value matching LeStorage directory sub-stores. Matches the name of the directory.

    class Meta:
        abstract = True
