# Generated by Django 2.2.17 on 2021-04-11 23:25

from datetime import timezone
from typing import List

from django.db import migrations

PROFILE_REQUIRE_TYPE = "profile"
AGREE = "agree"
DECLINE = "decline"

def create_preexisting_terms(apps, schema_editor) -> None:
    """
    Adds terms that already existed in amy on the Person model.
    """
    Term = apps.get_model('consents', 'Term')
    TermOption = apps.get_model('consents', 'TermOption')
    user_privacy_policy = Term.objects.create(
        content="*I have read and agree to <a href="
        '"https://docs.carpentries.org/topic_folders/policies/privacy.html"'
        ' target="_blank" rel="noreferrer">'
        "the data privacy policy of The Carpentries</a>.",
        slug="privacy-policy",
        required_type=PROFILE_REQUIRE_TYPE,
    )
    user_privacy_policy_agree = TermOption.objects.create(
        term=user_privacy_policy, option_type=AGREE
    )
    may_contact = Term.objects.create(
        content="May contact",
        slug="may-contact",
        required_type=PROFILE_REQUIRE_TYPE,
        help_text="Allow to contact from The Carpentries according to"
        " the Privacy Policy.",
    )
    may_contact_agree = TermOption.objects.create(
        term=may_contact, option_type=AGREE
    )
    may_contact_disagree = TermOption.objects.create(
        term=may_contact, option_type=DECLINE
    )
    public_profile = Term.objects.create(
        content="Consent to making profile public",
        required_type=PROFILE_REQUIRE_TYPE,
        slug="public-profile",
        help_text="Allow to post your name and any public profile"
        " you list (website, Twitter) on our instructors website."
        " Emails will not be posted.",
    )
    public_profile_agree = TermOption.objects.create(
        term=public_profile, option_type=AGREE
    )
    public_profile_disagree = TermOption.objects.create(
        term=public_profile, option_type=DECLINE
    )
    may_publish_name = Term.objects.create(
        content="Do you consent to have your name or identity"
        " associated with lesson publications?",
        slug="may-publish-name",
    )
    may_publish_name_agree_github = TermOption.objects.create(
        term=may_publish_name,
        option_type=AGREE,
        content="Yes, and only use my GitHub Handle",
    )
    may_publish_name_agree_orcid = TermOption.objects.create(
        term=may_publish_name,
        option_type=AGREE,
        content="Yes, and use the name associated with my ORCID profile",
    )
    may_publish_name_agree_profile = TermOption.objects.create(
        term=may_publish_name,
        option_type=AGREE,
        content="Yes, and use the name associated with my profile.",
    )
    may_publish_name_disagree = TermOption.objects.create(
        term=may_publish_name, option_type=DECLINE
    )
    Consent = apps.get_model('consents', 'Consent')
    Person = apps.get_model('workshops', 'Person')
    
    consents: List[Consent] = []
    people = Person.objects.all()
    may_publish_name_answer = {
        "yes-profile": may_publish_name_agree_profile,
        "yes-orcid": may_publish_name_agree_orcid,
        "yes-github": may_publish_name_agree_github,
        "no": may_publish_name_disagree,
        "unset": None
    }
    for person in people:
        privacy_policy_consent = user_privacy_policy_agree if person.data_privacy_agreement else None
        may_contact_consent = may_contact_agree if person.may_contact else may_contact_disagree
        publish_profile_consent = public_profile_agree if person.publish_profile else public_profile_disagree
        consents.append(
            Consent(
                person=person,
                term_option=privacy_policy_consent,
                term=user_privacy_policy,
            )
        )
        consents.append(
            Consent(
                person=person,
                term_option=publish_profile_consent,
                term=public_profile,
            )
        )
        consents.append(
            Consent(
                person=person,
                term_option=may_contact_consent,
                term=may_contact,
            )
        )
        consents.append(
            Consent(
                person=person,
                term_option=may_publish_name_answer[person.lesson_publication_consent],
                term=may_publish_name,
            )
        )
    Consent.objects.bulk_create(consents)

def remove_preexisting_terms(apps, schema_editor) -> None:
    terms_added = [
        "privacy-policy",
        "may-contact",
        "public-profile",
        "may-publish-name",
    ]
    Term = apps.get_model('consents', 'Term')
    Consent = apps.get_model('consents', 'Consent')
    Consent.objects.filter(term__slug__in=terms_added).delete()
    Term.objects.filter(slug__in=terms_added).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('consents', '0004_term_rq_jobs'),
    ]

    operations = [
        migrations.RunPython(create_preexisting_terms, remove_preexisting_terms),
    ]
