from django import forms
from django.utils.translation import gettext_lazy as _

from TWLight.resources.models import Partner

from .models import Application
from ..users.helpers.authorizations import get_valid_partner_authorizations

"""
Lists and characterizes the types of information that partners can require as
part of access grants. See full comment at end of file and docs at
https://github.com/WikipediaLibrary/TWLight/blob/master/docs/developer.md#changing-the-data-collected-on-application-forms
"""

"""
Harvestable from user profile:
    Username (all partnerships)
    Email (all partnerships)

Required/nonharvestable:

Optional/universal:
    Real name (many partners)
    Country of residence (Pelican, Numerique)
    Occupation (Elsevier)
    Affiliation (Elsevier)

Optional/unique:
    Questions/comments/concerns (free-text, all partnerships)
    Title requested (McFarland, Pelican)
    Agreement with Terms of Use (RSUK)
"""

# ~~~~~ Named constants ~~~~~ #
REAL_NAME = "real_name"
COUNTRY_OF_RESIDENCE = "country_of_residence"
OCCUPATION = "occupation"
AFFILIATION = "affiliation"
PARTNER = "partner"
RATIONALE = "rationale"
SPECIFIC_TITLE = "specific_title"
COMMENTS = "comments"
AGREEMENT_WITH_TERMS_OF_USE = "agreement_with_terms_of_use"
ACCOUNT_EMAIL = "account_email"
REQUESTED_ACCESS_DURATION = "requested_access_duration"


# ~~~~ Basic field names ~~~~ #
USER_FORM_FIELDS = [REAL_NAME, COUNTRY_OF_RESIDENCE, OCCUPATION, AFFILIATION]

# These fields are displayed for all partners.
PARTNER_FORM_BASE_FIELDS = [RATIONALE, COMMENTS]

# These fields are displayed only when a specific partner requires that
# information.
PARTNER_FORM_OPTIONAL_FIELDS = [
    SPECIFIC_TITLE,
    AGREEMENT_WITH_TERMS_OF_USE,
    ACCOUNT_EMAIL,
    REQUESTED_ACCESS_DURATION,
]


# ~~~~ Field information ~~~~ #
FIELD_TYPES = {
    REAL_NAME: forms.CharField(max_length=128),
    COUNTRY_OF_RESIDENCE: forms.CharField(max_length=128),
    OCCUPATION: forms.CharField(max_length=128),
    AFFILIATION: forms.CharField(max_length=128),
    PARTNER: forms.ModelChoiceField(
        queryset=Partner.objects.all(), widget=forms.HiddenInput
    ),
    RATIONALE: forms.CharField(widget=forms.Textarea),
    SPECIFIC_TITLE: forms.CharField(max_length=128),
    COMMENTS: forms.CharField(widget=forms.Textarea, required=False),
    AGREEMENT_WITH_TERMS_OF_USE: forms.BooleanField(),
    ACCOUNT_EMAIL: forms.EmailField(),
    REQUESTED_ACCESS_DURATION: forms.ChoiceField(
        choices=Application.REQUESTED_ACCESS_DURATION_CHOICES
    ),
}

FIELD_LABELS = {
    # Translators: When filling out an application, users may need to specify their name
    REAL_NAME: _("Your real name"),
    # Translators: When filling out an application, users may need to specify the country in which they currently live
    COUNTRY_OF_RESIDENCE: _("Your country of residence"),
    # Translators: When filling out an application, users may need to specify their current occupation
    OCCUPATION: _("Your occupation"),
    # Translators: When filling out an application, users may need to specify if they are affiliated with an institution (e.g. a university)
    AFFILIATION: _("Your institutional affiliation"),
    # Translators: When filling out an application, this labels the name of the publisher or database the user is applying to
    PARTNER: _("Partner name"),
    # Translators: When filling out an application, users must provide an explanation of why these resources would be useful to them
    RATIONALE: _("Why do you want access to this resource?"),