"""Reusable abstract mixin for a project-specific user profile model.

The target project typically subclasses this model in its own concrete
``UserProfile`` implementation, often generated by the cabinet/client
scaffolding flows.

Examples:
    Extend the profile with project-specific fields::

        from codex_django.system.mixins.user_profile import AbstractUserProfile

        class UserProfile(AbstractUserProfile):
            loyalty_tier = models.CharField(max_length=32, blank=True)
"""

from __future__ import annotations

from django.conf import settings
from django.db import models


class AbstractUserProfile(models.Model):
    """Reusable abstract profile model linked one-to-one with the auth user.

    The scaffolded project can subclass this mixin to keep personal data,
    acquisition metadata, and presentation helpers in a single profile model.

    Admin:
        list_display:
            ("id", "user", "last_name", "first_name", "phone", "source", "created_at")
        search_fields:
            ("user__username", "user__email", "first_name", "last_name", "phone")
        list_filter:
            ("source", "created_at")
        readonly_fields:
            ("created_at", "updated_at")
    """

    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        related_name="profile",
    )

    # ── Personal data ──────────────────────────────────────────────
    first_name = models.CharField(max_length=100, blank=True)
    last_name = models.CharField(max_length=100, blank=True)
    patronymic = models.CharField(max_length=100, blank=True)
    phone = models.CharField(max_length=20, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    avatar = models.ImageField(upload_to="avatars/", null=True, blank=True)

    # ── Acquisition ────────────────────────────────────────────────
    source = models.CharField(
        max_length=50,
        blank=True,
        help_text="How the profile was created: booking / form / manual / allauth",
    )
    notes = models.TextField(blank=True)

    # ── Meta timestamps ────────────────────────────────────────────
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True

    # ── Helpers ─────────────────────────────────────────────────────

    def get_full_name(self) -> str:
        """Return the profile's full name in ``Last First Patronymic`` order.

        Returns:
            A whitespace-normalized full name string assembled from the
            available name parts.
        """
        parts = [self.last_name, self.first_name, self.patronymic]
        return " ".join(p for p in parts if p).strip()

    def get_initials(self) -> str:
        """Return up to two uppercase initials built from first and last name.

        Returns:
            A two-letter initials string when name parts exist, otherwise ``?``.
        """
        initials = ""
        if self.first_name:
            initials += self.first_name[0].upper()
        if self.last_name:
            initials += self.last_name[0].upper()
        return initials or "?"
