import uuid

from django.db import models
from django.conf import settings
# from django.utils import timezone 
from django.utils.translation import gettext_lazy as _

# Retrieve the user model specified in the Django settings.
USER_MODEL = settings.AUTH_USER_MODEL

class BaseModel(models.Model):
    """
    Abstract base model providing common fields for other models.
    """ 
    # Unique identifier for each record using UUID.
    id = models.UUIDField(
        verbose_name=_("UUID"),
        primary_key=True, 
        unique=True,
        default=uuid.uuid4,  # Generate a unique UUID for each new record.
        editable=False  # Prevent manual editing of the UUID.
    )   
    # User who created the record.
    created_by = models.ForeignKey(
        to=USER_MODEL,
        verbose_name=_("Created by"),
        null=True,
        blank=True,
        related_name="%(class)s_created",  # Reverse relation name.
        on_delete=models.SET_NULL  # Set NULL if the related user is deleted.
    ) 
    # User who last updated the record.
    updated_by = models.ForeignKey(
        to=USER_MODEL,
        verbose_name=_("Updated by"),
        null=True,
        blank=True,
        related_name="%(class)s_updated",  # Reverse relation name.
        on_delete=models.SET_NULL  # Set NULL if the related user is deleted.
    ) 
    # Timestamp indicating when the record was created.
    created_at = models.DateTimeField(
        verbose_name=_("Created at"),
        # default=timezone.now,
        auto_now_add=True ,  # Set to the current datetime when created.
        editable=False,     # Prevent manual editing.
        db_index=True       # Add an index to this field for faster querying.
    )  
    # Timestamp indicating when the record was last updated.
    updated_at = models.DateTimeField(
        verbose_name=_("Updated at"),
        auto_now=True,      # Update to the current datetime whenever saved.
        null=True,          # Can be NULL if not updated yet.
        blank=True          # Can be left blank.
    )

    class Meta:
        # Indicates that this is an abstract model and won't create a database table.
        abstract = True  

class Language(BaseModel):
    """
    A model to represent a Language within the application.

    Attributes:
        name (CharField): The name of the language, such as 'English', 'French', etc.
        code (CharField): The language code, typically following ISO 639 standards (e.g., 'en' for English, 'fr' for French).
        description (TextField, optional): A brief description or additional information about the language, providing context or nuances.
    
    Meta:
        verbose_name (str): Specifies the singular name for the model in the admin interface ('Language').
        verbose_name_plural (str): Specifies the plural name for the model in the admin interface ('Languages').
        ordering (list): Specifies the default ordering of records based on the language name.
    
    Methods:
        __str__(): Returns a human-readable representation of the language object, displaying both its name and code.
    """

    # Define the 'name' field to store the name of the language.
    name = models.CharField(
        max_length=100,  # Set maximum length for the name to 100 characters.
        unique=True,     # Ensure that each language name is unique within the database.
        blank=False,
        null=False,
        help_text="Name of the Language, such as 'English', 'French', etc."  # Provide help text for this field.
    ) 
    # Define the 'code' field to store the language code following ISO 639 standards.
    code = models.CharField(
        max_length=10,   # Set maximum length for the code to 10 characters.
        unique=True,     # Ensure that each language code is unique within the database.
        blank=False,
        null=False,
        help_text="Language Code following ISO 639 standards, e.g., 'en' for English, 'fr' for French."  # Help text for this field.
    )
    # Define an optional 'description' field to provide additional information about the language.
    description = models.TextField(
        blank=True,      # Allow this field to be blank (optional).
        null=True,       # Allow this field to be null in the database.
        help_text="Optional description or additional information about the Language, providing context or nuances."  # Help text for this field.
    )

    # Meta class configuration for the model.
    class Meta:
        verbose_name = "Language"         # Singular name for the model in the admin interface.
        verbose_name_plural = "Languages" # Plural name for the model in the admin interface.
        ordering = ['name']               # Default ordering of records based on the language name.

    # Method to return a human-readable representation of the language object.
    def __str__(self):
        """Returns a human-readable representation of the language object."""
        return f"[{self.code}]:{self.name} "  # Display both the name and code of the language.

