import logging
import typing as t

from django import forms
from django.core.exceptions import ValidationError
from django.urls import reverse_lazy
from django_altcha import AltchaField

logger = logging.getLogger(__package__)


class SubscriptionForm(forms.Form):
    email_address = forms.EmailField(required=True, max_length=1000)

    email_address.widget = forms.EmailInput(attrs={"class": "mt-2 mb-3 mb-sm-5 form-control"})

    altcha = AltchaField(
        hidelogo=True,
        hidefooter=True,
        challengeurl=reverse_lazy("altcha_challenge")
    )

    def clean(self) -> t.Any:
        errors = []
        cleaned_data = super().clean()

        if cleaned_data.get("email_address") is None:
            errors.append(ValidationError("Please enter a valid email address."))

        if len(errors):
            raise ValidationError(errors)
        return cleaned_data
