"""
The Docker Container "database" needs to run to execute this tests properly
"""
import unittest
from app import db
from Rest.Answer import MessageCodes
from Matching.PreferenceTyps import InterestsPreferences
from Rest.MatchesREST import MatchesREST
from Models.UserModel import UserModel


def prepare_db():
    db.session.commit()
    db.drop_all()
    db.create_all()
    db.session.commit()


class TestMatchesRest(unittest.TestCase):
    def test_complex_one(self):
        prepare_db()
        """
        Setup: 12 users, a to l, with common interests from 1 to 12 in order; one additional x user
        Actions:
        - as user x get match suggestions: success, order is a,b,c,d,e,f
        - like first in line: success
        - like first again: success, already message
        - dislike first: success, reverted message
        - dislike first again: success, already message
        - get match suggestions again: only user a is not in suggestions, others are in same order
        """
        # SETUP
        # initialize users with interests
        all_interests = ["interest_" + str(i) for i in range(11)]
        interests = []
        for letter, additional_interest in zip('abcdefghijklmn', all_interests):
            user = UserModel.create("user_{}".format(letter), "password123", "{}norealmail@mail.com".format(letter))
            interests.append(additional_interest)
            user.personal_information.interest_list = interests
            db.session.add(user)
            db.session.commit()
        # initialize user x, with matching preference different interests, and all interests
        user_x = UserModel.create("user_x", "password123", "xnorealmail@mail.com")
        user_x.matching_preferences.interests_pref = InterestsPreferences.DIFFERENT
        user_x.personal_information.interest_list = all_interests
        db.session.commit()

        # ACTION
        # as user x get match suggestions: success
        get_matches_first_result = MatchesREST.get_matches(user_x.id, 6, {'type': 'Preference'})
        self.assertEqual(MessageCodes.WORKED.value, get_matches_first_result.message)
        self.assertEqual(["user_{}".format(letter) for letter in "abcdef"],
                         [user.display_name for user in get_matches_first_result.result])
        # like first in line: success
        like_first_time_result = MatchesREST.post_like_user(user_x.id, get_matches_first_result.result[0].id)
        self.assertEqual(MessageCodes.WORKED.value, like_first_time_result.message)
        # like first again: success, already message
        like_second_time_result = MatchesREST.post_like_user(user_x.id, get_matches_first_result.result[0].id)
        self.assertEqual(MessageCodes.MATCHING_ALREADY_LIKED.value, like_second_time_result.message)
        # dislike first: success, reverted message
        dislike_first_time_result = MatchesREST.post_dislike(user_x.id, get_matches_first_result.result[0].id)
        self.assertEqual(MessageCodes.MATCHING_LIKE_REVERTED.value, dislike_first_time_result.message)
        # dislike first again: success, already message
        dislike_second_time_result = MatchesREST.post_dislike(user_x.id, get_matches_first_result.result[0].id)
        self.assertEqual(MessageCodes.MATCHING_ALREADY_DISLIKED.value, dislike_second_time_result.message)

        # get match suggestions again: only user a is not in suggestions, others are in same order
        get_matches_second_result = MatchesREST.get_matches(user_x.id, 6, {'type': 'Preference'})
        self.assertEqual(MessageCodes.WORKED.value, get_matches_second_result.message)
        self.assertEqual(["user_{}".format(letter) for letter in "bcdefg"],
                         [user.display_name for user in get_matches_second_result.result])
