# -*- coding: utf-8 -*-
from __future__ import annotations

from django.db import models
from django.utils.translation import gettext_lazy as _

from hotpocket_backend.apps.core.models import Model


class ActiveAuthKeysManager(models.Manager):
    def get_queryset(self) -> models.QuerySet[AuthKey]:
        return super().get_queryset().filter(
            deleted_at__isnull=True,
        )


class AuthKey(Model):
    key = models.CharField(
        blank=False,
        default=None,
        null=False,
        max_length=128,
        db_index=True,
        unique=True,
        editable=False,
    )
    consumed_at = models.DateTimeField(
        blank=True,
        null=True,
        default=None,
        db_index=True,
        editable=False,
    )

    objects = models.Manager()
    active_objects = ActiveAuthKeysManager()

    class Meta:
        verbose_name = _('Auth Key')
        verbose_name_plural = _('Auth Keys')

    def __str__(self) -> str:
        return f'<AuthKey pk={self.pk} key={self.key}>'
