from django.contrib.auth import get_user_model
from django.db import models
from django_extensions.db.models import TimeStampedModel

User = get_user_model()

PERIOD_CHOICES = [
    ("last_7", "Last 7 days"),
    ("last_30", "Last 30 days"),
    ("last_90", "Last 90 days"),
    ("last_year", "Last year"),
]


class TrendResult(TimeStampedModel):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    trend_slug = models.CharField(max_length=100, db_index=True)
    period = models.CharField(
        max_length=20,
        choices=PERIOD_CHOICES,
        default="last_30",
    )
    computed_at = models.DateTimeField(auto_now_add=True)
    data = models.JSONField(default=dict)

    class Meta:
        unique_together = ["user", "trend_slug", "period"]

    def __str__(self):
        return f"{self.user} - {self.trend_slug} ({self.period})"
