"""
byceps.services.user.email_address_confirmation_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2019 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from ...database import db

from ..email import service as email_service
from ..site import service as site_service
from ..site.transfer.models import SiteID
from ..verification_token.models import Token
from ..verification_token import service as verification_token_service


def send_email_address_confirmation_email(
    recipient_email_address: str,
    recipient_screen_name: str,
    verification_token: Token,
    site_id: SiteID,
) -> None:
    site = site_service.get_site(site_id)

    email_config = email_service.get_config(site.email_config_id)
    sender = email_config.sender

    confirmation_url = (
        f'https://{site.server_name}/users/email_address/'
        f'confirmation/{verification_token.token}'
    )

    subject = f'{recipient_screen_name}, bitte bestätige deine E-Mail-Adresse'
    body = (
        f'Hallo {recipient_screen_name},\n\n'
        f'bitte bestätige deine E-Mail-Adresse, indem du diese URL abrufst: {confirmation_url}'
    )
    recipients = [recipient_email_address]

    email_service.enqueue_email(sender, recipients, subject, body)


def confirm_email_address(verification_token: Token) -> None:
    """Confirm the email address of the user assigned with that
    verification token.
    """
    user = verification_token.user

    user.email_address_verified = True
    user.initialized = True
    db.session.commit()

    verification_token_service.delete_token(verification_token)

"""
byceps.services.user.email_address_confirmation_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2019 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from ...database import db

from ..email import service as email_service
from ..site import service as site_service
from ..site.transfer.models import SiteID
from ..verification_token.models import Token
from ..verification_token import service as verification_token_service

from . import command_service


def send_email_address_confirmation_email(
    recipient_email_address: str,
    recipient_screen_name: str,
    verification_token: Token,
    site_id: SiteID,
) -> None:
    site = site_service.get_site(site_id)

    email_config = email_service.get_config(site.email_config_id)
    sender = email_config.sender

    confirmation_url = (
        f'https://{site.server_name}/users/email_address/'
        f'confirmation/{verification_token.token}'
    )

    subject = f'{recipient_screen_name}, bitte bestätige deine E-Mail-Adresse'
    body = (
        f'Hallo {recipient_screen_name},\n\n'
        f'bitte bestätige deine E-Mail-Adresse, indem du diese URL abrufst: {confirmation_url}'
    )
    recipients = [recipient_email_address]

    email_service.enqueue_email(sender, recipients, subject, body)


def confirm_email_address(verification_token: Token) -> None:
    """Confirm the email address of the user assigned with that
    verification token.
    """
    user = verification_token.user

    user.email_address_verified = True
    db.session.commit()

    if not user.initialized:
        command_service.initialize_account(user.id, user.id)

    verification_token_service.delete_token(verification_token)

Initialize user account on e-mail address confirmation if not already initialized