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

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

from hotpocket_backend.apps.core.models import Model


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


class Save(Model):
    key = models.CharField(
        blank=False, null=False, default=None, db_index=True,
    )
    url = models.CharField(
        blank=False, null=False, default=None, db_index=True,
        validators=[
            validators.URLValidator(schemes=['http', 'https']),
        ],
    )
    content = models.BinaryField(
        blank=True, null=True, default=None, editable=False,
    )
    title = models.CharField(
        blank=True, null=True, default=None, db_index=True,
    )
    description = models.CharField(
        blank=True, null=True, default=None, db_index=True,
    )
    last_processed_at = models.DateTimeField(
        auto_now=False,
        auto_now_add=False,
        blank=True,
        null=True,
        default=None,
    )
    is_netloc_banned = models.BooleanField(
        blank=False, null=False, default=False,
    )

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

    class Meta:
        verbose_name = _('Save')
        verbose_name_plural = _('Saves')

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