"""Test the email notifications of the ``trainings`` app."""

from django.core import mail

from mtp.apps.common.tests.test_cases import MTPTestCase
from mtp.apps.common.tests.waffle_test_utils import override_switch

from ..factories import TrainingSessionFactory
from ..models import TrainingSession


class TrainingsSessionNotificationsTestcase(MTPTestCase):
    """Test cases for the email notifications of the ``TrainingSession`` model."""

    @override_switch('enable_sessions_end_mail_for_trainees', active=True)
    def test_send_session_end_email_for_trainees(self):
        """Test that the mail for trainee is successfully sent."""

        self.training_session.send_end_mail()

        # We got one mail, for the trainee
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.trainee.email])

    @override_switch('enable_sessions_end_mail_for_trainers', active=True)
    def test_send_session_end_email_for_trainer(self):
        """Test that the mail for trainer is successfully sent."""

        self.training_session.send_end_mail()

        # We got one mail, for the trainer
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.trainer.email])

        # Check that the mail contains a link with the correct filters for the session list
        session_list_filters = 'training=%s&state=%s' % (
            self.training.id,
            self.training_session.state,
        )
        session_list_url = '%s?%s' % (
            TrainingSession.get_list_url(), session_list_filters,
        )

        self.assertIn(
            session_list_url,
            mail.outbox[0].alternatives[0][0]  # HTML Alternative
        )

    @override_switch('enable_sessions_end_mail_for_trainers', active=True)
    def test_send_session_end_email_for_trainer_with_an_original_training(self):
        """Test that the end mail contains a valid link if the training is an original"""

        # Create a new session for the admin training
        session = TrainingSessionFactory(
            trainer=self.admin,
            training=self.admin_training
        )
        session.send_end_mail()

        # Check that the mail contains a link with the correct filters for the session list
        # The training id must be the one for the original training
        session_list_filters = 'training=%s&state=%s' % (
            self.admin_training.id,
            session.state,
        )
        session_list_url = '%s?%s' % (
            TrainingSession.get_list_url(), session_list_filters,
        )

        self.assertIn(
            session_list_url,
            mail.outbox[0].alternatives[0][0]  # HTML Alternative
        )

    @override_switch('enable_sessions_end_mail_for_trainers', active=True)
    def test_send_session_end_email_for_trainer_with_a_duplicated_training(self):
        """Test that the end mail contains a valid link if the training is a duplicate"""

        # Duplicate the admin's training
        new_training = self.admin_training.create_draft()

        # Create a new session for this training
        session = TrainingSessionFactory(
            trainer=self.admin,
            training=new_training
        )
        session.send_end_mail()

        # Check that the mail contains a link with the correct filters for the session list
        # The training id must be the one for the original training
        session_list_filters = 'training=%s&state=%s' % (
            self.admin_training.id,
            session.state,
        )
        session_list_url = '%s?%s' % (
            TrainingSession.get_list_url(), session_list_filters,
        )

        self.assertIn(
            session_list_url,
            mail.outbox[0].alternatives[0][0]  # HTML Alternative
        )

    @override_switch('enable_sessions_end_mail_for_trainers', active=True)
    @override_switch('enable_sessions_end_mail_for_trainees', active=True)
    def test_send_session_end_email_for_all(self):
        """Test that both the mails are successfully sent."""

        self.training_session.send_end_mail()

        # We got two mails, one for the trainee, one for the trainer
        self.assertEqual(len(mail.outbox), 2)
