import uuid
from django.db import models
from django.utils import timezone

class User(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    employee_no = models.CharField(max_length=50, unique=True, null=True, blank=True)
    firstname = models.CharField(max_length=100)
    middlename = models.CharField(max_length=100, blank=True, null=True)
    lastname = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=255)  # We'll store hashed passwords
    lang = models.CharField(
        max_length=10,
        choices=[('en', 'English'), ('fi', 'Finnish'), ('sv', 'Swedish')],
        default='en',
        help_text="User's preferred language"
    )
    compliance_enabled = models.BooleanField(
        default=True,
        help_text="Enable real-time compliance monitoring and preventive notifications"
    )
    hire_start_date = models.DateField(null=True, blank=True)
    hire_end_date = models.DateField(null=True, blank=True)
    must_change_password = models.BooleanField(
        default=False,
        help_text="Force user to change password on next login"
    )
    password_changed_at = models.DateTimeField(null=True, blank=True, help_text="When the password was last changed")
    working_time_policy = models.ForeignKey(
        'company.WorkingTimePolicy',
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name='assigned_users',
        help_text="Assigned work time policy for this user"
    )
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'users'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.firstname} {self.lastname}"

    def save(self, *args, **kwargs):
        self.updated_at = timezone.now()
        super().save(*args, **kwargs)
    
    # Required for Django REST Framework authentication
    @property
    def is_authenticated(self):
        """Always return True for authenticated users"""
        return True
    
    @property
    def is_anonymous(self):
        """Always return False for authenticated users"""
        return False