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

from topdownrbac.models import RestrictedObject, UserGroupSubject


class Organization(RestrictedObject, UserGroupSubject):
    """A top-level restricted object. Root of the hierarchy. Is also a UserGroupSubject,
    so we can assign permissions to users on the org itself."""

    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name


class Project(RestrictedObject):
    """A restricted object that lives under an Organization."""

    name = models.CharField(max_length=200)
    description = models.TextField(blank=True)

    def __str__(self):
        return self.name


class UserProfile(models.Model):
    """
    App-specific profile attached to the library-provided UserSubject.

    This is the recommended way to add custom fields to the user model
    without modifying or subclassing UserSubject.
    """

    user = models.OneToOneField(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="profile",
    )
    bio = models.TextField(blank=True)
    avatar_url = models.URLField(blank=True)

    def __str__(self):
        return f"Profile of {self.user.username}"
