from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    imagen = models.ImageField(default='perfil/steve.png')
    def __str__(self):
        return f'Perfil de {self.user.username}'
    def siguiendo(self):
        user_ids = Relacion.objects.filter(from_user=self.user)\
                            .values_list('to_user_id', flat=True)
        return User.objects.filter(id__in=user_ids)
    def seguidores(self):
        user_ids = Relacion.objects.filter(to_user=self.user)\
                            .values_list('from_user_id', flat=True)
        return User.objects.filter(id__in=user_ids)
class Post(models.Model):
    autor = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    texto = models.TextField()
    imagen = models.ImageField(upload_to="Posts", null=True)
    fecha_creacion = models.DateTimeField(
            default=timezone.now)
    fecha_publicacion = models.DateTimeField(
            blank=True, null=True)
    def publish(self):
        self.fecha_publicacion = timezone.now()
        self.save()

    def __str__(self):
        return f'{self.autor.username}: {self.texto}'
class Relacion(models.Model):
    from_user = models.ForeignKey(User, related_name='relaciones', on_delete=models.CASCADE)
    to_user = models.ForeignKey(User, related_name='related_to', on_delete=models.CASCADE)
    def __str__(self):
        return f'{self.from_user} to {self.to_user}'
    class Meta:
        indexes = [
            models.Index(fields=['from_user', 'to_user',]),
        ]

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    imagen = models.ImageField(default='perfil/steve.png')
    def __str__(self):
        return f'Perfil de {self.user.username}'
    def siguiendo(self):
        user_ids = Relacion.objects.filter(from_user=self.user)\
                            .values_list('to_user_id', flat=True)
        return User.objects.filter(id__in=user_ids)
    def seguidores(self):
        user_ids = Relacion.objects.filter(to_user=self.user)\
                            .values_list('from_user_id', flat=True)
        return User.objects.filter(id__in=user_ids)
class Post(models.Model):
    autor = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    texto = models.TextField()
    imagen = models.ImageField(upload_to="Posts", null=True)
    fecha_creacion = models.DateTimeField(
            default=timezone.now)
    fecha_publicacion = models.DateTimeField(
            blank=True, null=True)
    def publish(self):
        self.fecha_publicacion = timezone.now()
        self.save()

    def __str__(self):
        return f'{self.autor.username}: {self.texto}'
class Relacion(models.Model):
    from_user = models.ForeignKey(User, related_name='relaciones', on_delete=models.CASCADE)
    to_user = models.ForeignKey(User, related_name='related_to', on_delete=models.CASCADE)
    def __str__(self):
        return f'{self.from_user} to {self.to_user}'
    class Meta:
        indexes = [
            models.Index(fields=['from_user', 'to_user',]),
        ]
