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

from users.definitions import VERIFICATION_CODE
from users.imports.db import BaseDBModel as Parent


class NotificationLog(Parent):
    user = models.ForeignKey(
        verbose_name=_('User'),
        blank=False,
        null=False,
        to='User',
        on_delete=models.CASCADE,
        db_index=False,
    )
    platform = models.CharField(
        verbose_name=_('Platform'),
        choices=(
            ('SMS', _('SMS')),
            ('Email', _('Email')),
            ('Native', _('Native')),
        ),
        max_length=7,
    )
    status = models.CharField(
        verbose_name=_('Status'),
        choices=(
            ('Success', _('Success')),
            ('Fail', _('Fail')),
            ('Uncertain', _('Uncertain')),
        ),
        max_length=15,
    )
    kwargs = models.JSONField(
        verbose_name=_('**kwargs'),
        blank=True,
        null=True,
    )

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

    class Meta:
        verbose_name = _('Notification Log')
        verbose_name_plural = _('Notifications Log')

    def save(self, *args, **kwargs):
        if self.kwargs:
            for k, v in self.kwargs.items():
                if k == 'template_key' and self.kwargs.get(k) == VERIFICATION_CODE:
                    self.kwargs['message'] = '********'

        super().save(*args, **kwargs)
