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

class Category(models.Model):
    name = models.CharField(max_length=100)
    # e.g., 'Pants', 'T-shirts', 'Beanies', 'Rings'
    
    class Meta:
        verbose_name_plural = "Categories"

    def __str__(self):
        return self.name

class Item(models.Model):
    ITEM_TYPES = (
        ('wardrobe', 'Wardrobe'),
        ('accessory', 'Accessory'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='items', null=True, blank=True)
    name = models.CharField(max_length=255)
    image = models.ImageField(upload_to='items/', blank=True, null=True)
    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
    item_type = models.CharField(max_length=20, choices=ITEM_TYPES, default='wardrobe')
    tags = models.CharField(max_length=255, blank=True, help_text="Comma-separated tags")
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class Folder(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='folders', null=True, blank=True)
    name = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name

class Outfit(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='outfits', null=True, blank=True)
    name = models.CharField(max_length=255, blank=True)
    items = models.ManyToManyField(Item, related_name='outfits')
    folder = models.ForeignKey(Folder, on_delete=models.SET_NULL, null=True, blank=True, related_name='outfits')
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.name or f"Outfit {self.id}"

class DailySchedule(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='schedules', null=True, blank=True)
    date = models.DateField()
    outfit = models.ForeignKey(Outfit, on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        unique_together = ('user', 'date')

    def __str__(self):
        return f"{self.date} - {self.outfit}"