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

User = settings.AUTH_USER_MODEL

class Post(models.Model):
    author = models.ForeignKey(User, related_name="posts", on_delete=models.CASCADE)
    content = models.TextField(blank=True, default='')
    image = models.ImageField(upload_to="posts/", blank=True, null=True)
    tag = models.CharField(max_length=20, choices=[("none","—"),("announcement","📢 Announcement"),("help","🆘 Help Needed"),("celebration","🎉 Celebration"),("job","💼 Job Opportunity"),("question","❓ Question"),("event","📅 Event")], default="none", blank=True)
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(auto_now=True)
    boosted_until = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['-created_at']),
            models.Index(fields=['author']),
        ]

    def __str__(self):
        return f"Post by {self.author.username} at {self.created_at}"


class Like(models.Model):
    user = models.ForeignKey(User, related_name="likes", on_delete=models.CASCADE)
    post = models.ForeignKey(Post, related_name="likes", on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)

    class Meta:
        unique_together = ("user", "post")
        indexes = [
            models.Index(fields=['post', 'user']),
        ]

    def __str__(self):
        return f"{self.user.username} likes post {self.post.id}"


class Love(models.Model):
    user = models.ForeignKey(User, related_name="loves", on_delete=models.CASCADE)
    post = models.ForeignKey(Post, related_name="loves", on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)

    class Meta:
        unique_together = ("user", "post")
        indexes = [
            models.Index(fields=['post', 'user']),
        ]

    def __str__(self):
        return f"{self.user.username} loves post {self.post.id}"


class Comment(models.Model):
    user = models.ForeignKey(User, related_name="comments", on_delete=models.CASCADE)
    post = models.ForeignKey(Post, related_name="comments", on_delete=models.CASCADE)
    text = models.TextField()
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['created_at']
        indexes = [
            models.Index(fields=['post', 'created_at']),
            models.Index(fields=['user']),
        ]

    def __str__(self):
        return f"Comment by {self.user.username} on post {self.post.id}"


class CommentLike(models.Model):
    user = models.ForeignKey(User, related_name="comment_likes", on_delete=models.CASCADE)
    comment = models.ForeignKey(Comment, related_name="comment_likes", on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)

    class Meta:
        unique_together = ("user", "comment")
        indexes = [
            models.Index(fields=['comment', 'user']),
        ]

    def __str__(self):
        return f"{self.user.username} likes comment {self.comment.id}"


class CommentDislike(models.Model):
    user = models.ForeignKey(User, related_name="comment_dislikes", on_delete=models.CASCADE)
    comment = models.ForeignKey(Comment, related_name="comment_dislikes", on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)

    class Meta:
        unique_together = ("user", "comment")
        indexes = [
            models.Index(fields=['comment', 'user']),
        ]

    def __str__(self):
        return f"{self.user.username} dislikes comment {self.comment.id}"


class Bookmark(models.Model):
    user = models.ForeignKey(User, related_name="bookmarks", on_delete=models.CASCADE)
    post = models.ForeignKey(Post, related_name="bookmarks", on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)

    class Meta:
        unique_together = ("user", "post")
        ordering = ["-created_at"]


class Poll(models.Model):
    post = models.OneToOneField(Post, related_name="poll", on_delete=models.CASCADE)
    question = models.CharField(max_length=200)
    closes_at = models.DateTimeField(null=True, blank=True)
    created_at = models.DateTimeField(default=timezone.now)

    @property
    def total_votes(self):
        return sum(o.votes.count() for o in self.options.all())


class PollOption(models.Model):
    poll = models.ForeignKey(Poll, related_name="options", on_delete=models.CASCADE)
    text = models.CharField(max_length=120)
    order = models.PositiveSmallIntegerField(default=0)

    class Meta:
        ordering = ["order"]


class PollVote(models.Model):
    option = models.ForeignKey(PollOption, related_name="votes", on_delete=models.CASCADE)
    user   = models.ForeignKey(User, related_name="poll_votes", on_delete=models.CASCADE)
    created_at = models.DateTimeField(default=timezone.now)

    class Meta:
        unique_together = ("option", "user")
