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


# ── Message ───────────────────────────────────────────────────────────────────

class Message(models.Model):
    sender    = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='sent_messages')
    recipient = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='received_messages')
    subject   = models.CharField(_('subject'), max_length=200)
    body      = models.TextField(_('body'))
    sent_at   = models.DateTimeField(auto_now_add=True)
    read_at   = models.DateTimeField(null=True, blank=True)
    deleted_by_sender    = models.BooleanField(default=False)
    deleted_by_recipient = models.BooleanField(default=False)

    class Meta:
        ordering = ['-sent_at']

    def __str__(self):
        return f'{self.sender} → {self.recipient}: {self.subject}'


# ── BlogPost ──────────────────────────────────────────────────────────────────

class BlogPost(models.Model):
    author     = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='blog_posts')
    title      = models.CharField(_('title'), max_length=300)
    body       = models.TextField(_('body'))
    is_public  = models.BooleanField(_('public'), default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return self.title


# ── Bug ───────────────────────────────────────────────────────────────────────

class BugSeverity(models.TextChoices):
    LOW      = 'low',      _('Low')
    MEDIUM   = 'medium',   _('Medium')
    HIGH     = 'high',     _('High')
    CRITICAL = 'critical', _('Critical')


class BugStatus(models.TextChoices):
    OPEN        = 'open',        _('Open')
    IN_PROGRESS = 'in_progress', _('In Progress')
    RESOLVED    = 'resolved',    _('Resolved')
    CLOSED      = 'closed',      _('Closed')


class Bug(models.Model):
    reporter    = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.SET_NULL,
        null=True, related_name='reported_bugs',
    )
    title       = models.CharField(_('title'), max_length=300)
    description = models.TextField(_('description'))
    severity    = models.CharField(
        _('severity'), max_length=10,
        choices=BugSeverity.choices, default=BugSeverity.MEDIUM,
    )
    status      = models.CharField(
        _('status'), max_length=15,
        choices=BugStatus.choices, default=BugStatus.OPEN,
    )
    created_at  = models.DateTimeField(auto_now_add=True)
    updated_at  = models.DateTimeField(auto_now=True)
    resolved_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return f'[{self.get_severity_display()}] {self.title}'


# ── Friendship ────────────────────────────────────────────────────────────────

class FriendshipStatus(models.TextChoices):
    PENDING  = 'pending',  _('Pending')
    ACCEPTED = 'accepted', _('Accepted')
    BLOCKED  = 'blocked',  _('Blocked')


class Friendship(models.Model):
    from_user  = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='friendships_sent',
    )
    to_user    = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='friendships_received',
    )
    status     = models.CharField(
        _('status'), max_length=10,
        choices=FriendshipStatus.choices, default=FriendshipStatus.PENDING,
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = [('from_user', 'to_user')]
        ordering        = ['-created_at']

    def __str__(self):
        return f'{self.from_user} → {self.to_user} ({self.status})'
