from django.contrib.auth.models import AbstractUser
from django.db import models
import uuid

class User(AbstractUser):
    TELEGRAM_ROLES = [
        ('admin', 'Администратор системы'),
        ('hotel_user', 'Сотрудник отеля'),
    ]

    telegram_id = models.BigIntegerField(
        unique=True,
        null=True,
        blank=True,
        verbose_name="ID Телеграм"
    )
    chat_id = models.BigIntegerField(
        unique=True,
        null=True,
        blank=True,
        verbose_name="ID чата в телеграм"
    )
    role = models.CharField(
        max_length=20,
        choices=TELEGRAM_ROLES,
        default='hotel_user',
        verbose_name="Роль"
    )
    confirmed = models.BooleanField(default=False, verbose_name="Подтвержден")

    groups = models.ManyToManyField(
        'auth.Group',
        related_name='custom_user_set',  # Уникальное имя для обратной связи
        blank=True
    )
    user_permissions = models.ManyToManyField(
        'auth.Permission',
        related_name='custom_user_set',  # Уникальное имя для обратной связи
        blank=True
    )

    def __str__(self):
        return self.username or f"Telegram User {self.telegram_id}"
    
    class Meta:
        verbose_name = "Пользователь"
        verbose_name_plural = "Пользователи"
        
        
class UserConfirmation(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Пользователь")
    confirmation_code = models.UUIDField(default=uuid.uuid4, verbose_name="Код подтверждения")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="Создан: ")

    def __str__(self):
        return f"Confirmation for {self.user.username} - {self.confirmation_code}"
    
    class Meta:
        verbose_name = "Подтверждение пользователя"
        verbose_name_plural = "Подтверждения пользователей"
        
class WordPressUserActivityLog(models.Model):
    id = models.AutoField(primary_key=True)
    user_id = models.IntegerField()
    activity_type = models.CharField(max_length=255)
    timestamp = models.DateTimeField()
    additional_data = models.JSONField(null=True, blank=True)

    class Meta:
        db_table = 'wpts_user_activity_log'  # Название таблицы в базе данных WordPress
        managed = False  # Django не будет управлять этой таблицей
        app_label = 'Users'  # Замените на имя вашего приложения
        
class LocalUserActivityLog(models.Model):
    id = models.AutoField(primary_key=True)
    user_id = models.IntegerField()
    activity_type = models.CharField(max_length=255)
    timestamp = models.DateTimeField()
    additional_data = models.JSONField(null=True, blank=True)

    def __str__(self):
        return f"User {self.user_id} - {self.activity_type}"
    
class UserActivityLog(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name="ID")
    user_id = models.BigIntegerField( verbose_name="ID пользователя")
    ip = models.CharField(max_length=100, null=True, blank=True, verbose_name="IP адрес")
    created = models.DateTimeField(auto_now_add=True, verbose_name="Создан")
    timestamp = models.IntegerField(verbose_name="Время")
    date_time = models.DateTimeField(verbose_name="Дата")
    referred = models.CharField(max_length=255, null=True, blank=True)
    agent = models.CharField(max_length=255, null=True, blank=True, verbose_name="Браузер")
    platform = models.CharField(max_length=255, null=True, blank=True)
    version = models.CharField(max_length=50, null=True, blank=True)
    model = models.CharField(max_length=255, null=True, blank=True)
    device = models.CharField(max_length=50, null=True, blank=True)
    UAString = models.TextField(null=True, blank=True)
    location = models.CharField(max_length=255, null=True, blank=True)
    page_id = models.BigIntegerField(null=True, blank=True)
    url_parameters = models.TextField(null=True, blank=True)
    page_title = models.CharField(max_length=255, null=True, blank=True)
    type = models.CharField(max_length=50, null=True, blank=True)
    last_counter = models.IntegerField(null=True, blank=True)
    hits = models.IntegerField(null=True, blank=True)
    honeypot = models.BooleanField(null=True, blank=True)
    reply = models.BooleanField(null=True, blank=True)
    page_url = models.CharField(max_length=255, null=True, blank=True)

    class Meta:
        db_table = 'user_activity_log'  # Название таблицы в локальной базе
        verbose_name = 'Журнал активности'
        verbose_name_plural = 'Журналы активности'

    def __str__(self):
        return f"User {self.user_id} - {self.type} - {self.date_time}"
    
class NotificationSettings(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Пользователь")
    telegram_enabled = models.BooleanField(default=True, verbose_name="Уведомления в Telegram")
    email_enabled = models.BooleanField(default=False, verbose_name="Уведомления по Email")
    email = models.EmailField(blank=True, null=True, verbose_name="Email для уведомлений")
    notification_time = models.TimeField(default="09:00", verbose_name="Время отправки уведомлений")

    def __str__(self):
        return f"Настройки уведомлений для {self.user.username}"
    
    class Meta:
        verbose_name = "Способ оповещения"
        verbose_name_plural = "Способы оповещений"