from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _

class CustomUser(AbstractUser):
    """Custom user model with extended fields"""
    
    # User types
    USER_TYPE_CHOICES = [
        ('admin', 'Administrator'),
        ('manager', 'Manager'),
        ('operator', 'Operator'),
        ('viewer', 'Viewer'),
    ]
    
    user_type = models.CharField(
        max_length=20,
        choices=USER_TYPE_CHOICES,
        default='operator'
    )
    
    phone = models.CharField(max_length=20, blank=True, null=True)
    department = models.CharField(max_length=100, blank=True, null=True)
    employee_id = models.CharField(max_length=20, blank=True, null=True, unique=True)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True)
    last_login = models.DateTimeField(auto_now=True)
    
    # Profile picture
    profile_picture = models.ImageField(
        upload_to='profile_pictures/',
        blank=True,
        null=True
    )
    
    class Meta:
        verbose_name = _('User')
        verbose_name_plural = _('Users')
        ordering = ['username']
    
    def __str__(self):
        return f"{self.username} ({self.get_user_type_display()})"
    
    def get_full_name(self):
        if self.first_name and self.last_name:
            return f"{self.first_name} {self.last_name}"
        return self.username
    
    def has_manufacturing_permission(self):
        """Check if user has manufacturing permissions"""
        return self.user_type in ['admin', 'manager', 'operator']
    
    def has_inventory_permission(self):
        """Check if user has inventory permissions"""
        return self.user_type in ['admin', 'manager', 'operator']
    
    def has_purchase_permission(self):
        """Check if user has purchase permissions"""
        return self.user_type in ['admin', 'manager']
    
    def has_sales_permission(self):
        """Check if user has sales permissions"""
        return self.user_type in ['admin', 'manager', 'operator']
    
    def has_user_management_permission(self):
        """Check if user has user management permissions"""
        return self.user_type in ['admin', 'manager']
    
    def has_reporting_permission(self):
        """Check if user has reporting permissions"""
        return self.user_type in ['admin', 'manager', 'viewer']

class UserGroup(models.Model):
    """Custom user groups for specific permissions"""
    
    name = models.CharField(max_length=100, unique=True)
    description = models.TextField(blank=True)
    users = models.ManyToManyField(CustomUser, related_name='custom_groups')
    permissions = models.JSONField(default=dict)  # Store permissions as JSON
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        ordering = ['name']
    
    def __str__(self):
        return self.name
    
    def get_permissions(self):
        """Get permissions for this group"""
        return self.permissions or {}
    
    def has_permission(self, permission_name):
        """Check if group has specific permission"""
        permissions = self.get_permissions()
        return permissions.get(permission_name, False)
