import logging
from datetime import timedelta

from django.contrib.auth.models import User
from django.core.validators import MaxValueValidator, MinValueValidator, FileExtensionValidator
from django.db import models
from django.utils.translation import gettext_lazy
from solo.models import SingletonModel

_log = logging.getLogger(__name__)


class TLSSettings(SingletonModel):
    singleton_instance_id = 1
    enabled = models.BooleanField(verbose_name=gettext_lazy("Enable TLS"), default=False)
    certificate = models.FileField(verbose_name=gettext_lazy("Certificate"),
                                   help_text=gettext_lazy("TLS certificate in PEM format"),
                                   validators=[FileExtensionValidator(allowed_extensions=["crt", "pem"])],
                                   null=True, blank=True)
    key = models.FileField(verbose_name=gettext_lazy("Key"), help_text=gettext_lazy("TLS certificate's key"),
                           validators=[FileExtensionValidator(allowed_extensions=["key"])],
                           null=True, blank=True)


class UsernameLoginAuthCheck(models.Model):
    """ Model for storing authentication data for certain usernames """
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    is_username_auth_blocked = models.BooleanField(default=False,
                                                   verbose_name=gettext_lazy(
                                                       "Is authentication blocked for this username"),
                                                   help_text=gettext_lazy(
                                                       "Flag, that shows if authentication for that username is blocked"))
    failed_login_attempts = models.IntegerField(default=0,
                                                verbose_name=gettext_lazy("Failed login attempts"),
                                                help_text=gettext_lazy("Failed login attempts for username"))
    username_unlock_time = models.DateTimeField(verbose_name=gettext_lazy("Authentication block time"),
                                                help_text=gettext_lazy(
                                                    "Time, when access to authentication for that username was blocked"),
                                                blank=True,
                                                null=True)


class ConsoleAuthSettings(SingletonModel):
    """ Singleton model for storing console authentication backend settings """
    login_attempts_limit = models.IntegerField(default=3,
                                               validators=[MinValueValidator(0), MaxValueValidator(100)],
                                               verbose_name=gettext_lazy('Login attempts limit'),
                                               help_text=gettext_lazy(
                                                   'Attempts, after which access to authorization will be blocked. Range from 1 to 100. 0 is off'))
    login_block_timeout = models.DurationField(default=timedelta(minutes=30),
                                               verbose_name=gettext_lazy('Login authentication timeout'),
                                               help_text=gettext_lazy('Timeout, during which user cannot authenticate'))
