import uuid
from django.db import models

class Configuration(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    header = models.CharField(max_length=255)
    icon = models.CharField(max_length=100)
    function_ref_id = models.CharField(max_length=100)
    color = models.CharField(max_length=50)
    questions = models.JSONField(default=list, blank=True)
    menu_type = models.CharField(max_length=10, null=True, blank=True, choices=[('InMenu', 'InMenu'), ('OutMenu', 'OutMenu')])
    is_quick_menu = models.BooleanField(default=False)
    visibility_rule = models.CharField(
        max_length=20,
        choices=[
            ('always_show', 'Always Show'),
            ('hide_when_in', 'Hide When In'),
            ('hide_when_out', 'Hide When Out'),
            ('show_when_in', 'Show When In'),
            ('show_when_out', 'Show When Out')
        ],
        default='always_show',
        help_text='Controls when this action button should be visible'
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.header} - {self.function_ref_id}"

    class Meta:
        ordering = ['-created_at']


class ConfigurationTranslation(models.Model):
    """
    Stores translations for configuration headers, question headers, and question labels.
    """
    TRANSLATION_TYPE_CHOICES = [
        ('function_header', 'Function Header'),
        ('question_label', 'Question Label'),
    ]
    
    LANGUAGE_CHOICES = [
        ('en', 'English'),
        ('fi', 'Finnish'),
        ('sv', 'Swedish'),
    ]
    
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    translation_type = models.CharField(max_length=20, choices=TRANSLATION_TYPE_CHOICES)
    language = models.CharField(max_length=10, choices=LANGUAGE_CHOICES)
    
    # For function_header: reference by function_ref_id
    # For question_label: reference by question_id
    reference_id = models.CharField(max_length=100, help_text='function_ref_id for headers, question_id for labels')
    
    # The original text (for reference)
    original_text = models.CharField(max_length=255, help_text='Original text in default language')
    
    # The translated text
    translated_text = models.CharField(max_length=255, help_text='Translated text')
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'configuration_translations'
        unique_together = [['translation_type', 'language', 'reference_id']]
        ordering = ['translation_type', 'language', 'reference_id']
        indexes = [
            models.Index(fields=['translation_type', 'language', 'reference_id']),
        ]
    
    def __str__(self):
        return f"{self.translation_type} - {self.language} - {self.reference_id}: {self.translated_text}"


class AppSettings(models.Model):
    """
    Application-wide settings including SMTP configuration
    """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    
    # SMTP Configuration
    smtp_enabled = models.BooleanField(default=False, help_text='Enable SMTP email sending')
    smtp_host = models.CharField(max_length=255, blank=True, null=True, help_text='SMTP server hostname')
    smtp_port = models.IntegerField(default=587, help_text='SMTP server port (587 for TLS, 465 for SSL)')
    smtp_use_tls = models.BooleanField(default=True, help_text='Use TLS encryption')
    smtp_use_ssl = models.BooleanField(default=False, help_text='Use SSL encryption')
    smtp_username = models.CharField(max_length=255, blank=True, null=True, help_text='SMTP username/email')
    smtp_password = models.CharField(max_length=255, blank=True, null=True, help_text='SMTP password (stored encrypted)')
    smtp_from_email = models.EmailField(blank=True, null=True, help_text='Default from email address')
    smtp_from_name = models.CharField(max_length=255, blank=True, null=True, default='TimeTracker', help_text='Default from name')
    
    # Email Settings
    send_welcome_email_on_creation = models.BooleanField(default=True, help_text='Automatically send welcome email when employee is created')
    temp_password_length = models.IntegerField(default=16, help_text='Length of temporary password')
    temp_password_expires_hours = models.IntegerField(default=48, help_text='Hours until temporary password expires')
    force_password_change_on_temp_password = models.BooleanField(
        default=True,
        help_text='Force users to change their temporary password on first login'
    )
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        db_table = 'app_settings'
        verbose_name = 'App Settings'
        verbose_name_plural = 'App Settings'
    
    def __str__(self):
        return f"App Settings - SMTP: {'Enabled' if self.smtp_enabled else 'Disabled'}"
    
    @classmethod
    def get_settings(cls):
        """Get or create the singleton settings instance"""
        settings, created = cls.objects.get_or_create(pk='00000000-0000-0000-0000-000000000001')
        return settings
