
from django.db import models
from django.contrib.auth.models import User
from inventory.models import Product
from settings.models import Shop


class SiteTraffic(models.Model):
    """Track site traffic analytics"""
    date = models.DateField(auto_now_add=True)
    page_views = models.IntegerField(default=0)
    unique_visitors = models.IntegerField(default=0)
    bounce_rate = models.FloatField(default=0.0)
    session_duration = models.FloatField(default=0.0)  # in minutes
    
    class Meta:
        unique_together = ['date']
        
    def __str__(self):
        return f"Traffic for {self.date}"


class ProductAnalytics(models.Model):
    """Track product performance analytics"""
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='analytics')
    date = models.DateField(auto_now_add=True)
    views = models.IntegerField(default=0)
    clicks = models.IntegerField(default=0)
    cart_additions = models.IntegerField(default=0)
    purchases = models.IntegerField(default=0)
    revenue = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
    
    class Meta:
        unique_together = ['product', 'date']
        
    def __str__(self):
        return f"{self.product.name} analytics for {self.date}"


class Banner(models.Model):
    """Banners for the shop"""
    BANNER_TYPES = [
        ('hero', 'Hero Banner'),
        ('promotion', 'Promotion Banner'),
        ('category', 'Category Banner'),
        ('announcement', 'Announcement Banner'),
    ]
    
    PLACEMENT_CHOICES = [
        ('home_top', 'Home Page - Top'),
        ('home_middle', 'Home Page - Middle'),
        ('home_bottom', 'Home Page - Bottom'),
        ('category_top', 'Category Pages - Top'),
        ('product_sidebar', 'Product Pages - Sidebar'),
        ('cart_page', 'Cart Page'),
        ('checkout_page', 'Checkout Page'),
    ]
    
    title = models.CharField(max_length=200)
    subtitle = models.CharField(max_length=300, blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to='banners/', blank=True, null=True)
    link_url = models.URLField(blank=True, null=True)
    link_text = models.CharField(max_length=100, blank=True, null=True)
    banner_type = models.CharField(max_length=20, choices=BANNER_TYPES, default='promotion')
    placement = models.CharField(max_length=20, choices=PLACEMENT_CHOICES, default='home_top')
    background_color = models.CharField(max_length=7, default='#ffffff')
    text_color = models.CharField(max_length=7, default='#000000')
    is_active = models.BooleanField(default=True)
    start_date = models.DateTimeField(blank=True, null=True)
    end_date = models.DateTimeField(blank=True, null=True)
    display_order = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        ordering = ['placement', 'display_order']
        
    def __str__(self):
        return f"{self.title} ({self.get_placement_display()})"


class UserSession(models.Model):
    """Track user sessions for analytics"""
    session_key = models.CharField(max_length=40, unique=True)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
    ip_address = models.GenericIPAddressField()
    user_agent = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    last_activity = models.DateTimeField(auto_now=True)
    page_views = models.IntegerField(default=0)
    
    def __str__(self):
        return f"Session {self.session_key[:8]}... - {self.created_at}"


class ShopSettings(models.Model):
    """Extended shop settings for ecommerce"""
    shop = models.OneToOneField(Shop, on_delete=models.CASCADE, related_name='ecommerce_settings')
    tagline = models.CharField(max_length=300, blank=True, null=True)
    welcome_message = models.TextField(blank=True, null=True)
    featured_products_count = models.IntegerField(default=8)
    products_per_page = models.IntegerField(default=12)
    enable_reviews = models.BooleanField(default=True)
    enable_wishlist = models.BooleanField(default=True)
    enable_compare = models.BooleanField(default=True)
    social_facebook = models.URLField(blank=True, null=True)
    social_twitter = models.URLField(blank=True, null=True)
    social_instagram = models.URLField(blank=True, null=True)
    social_youtube = models.URLField(blank=True, null=True)
    google_analytics_id = models.CharField(max_length=50, blank=True, null=True)
    facebook_pixel_id = models.CharField(max_length=50, blank=True, null=True)
    
    def __str__(self):
        return f"E-commerce settings for {self.shop.name}"
