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

from users.imports.db import BaseDBModel as Parent


class VCode(Parent):
    user = models.ForeignKey(
        verbose_name=_('User'),
        blank=False,
        null=False,
        to='User',
        on_delete=models.CASCADE,
        db_index=False,
        related_name='vcodes',
    )
    hashed_code = models.CharField(
        verbose_name=_('Hashed Code'),
        max_length=255,
        blank=False,
        null=False,
        db_index=False,
    )
    expiry_datetime = models.DateTimeField(
        verbose_name=_('Expiry DateTime'),
        blank=False,
        null=False,
        db_index=False,
    )

    def __str__(self):
        return f'{self.pk}(for-> {self.user})'

    class Meta:
        verbose_name = _('Verification Code')
        verbose_name_plural = _('Verification Codes')
        indexes = [
            models.Index(fields=['user', 'hashed_code', 'expiry_datetime']),
            models.Index(fields=['user', 'hashed_code', ]),
            models.Index(fields=['hashed_code', 'expiry_datetime']),
        ]
