"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 30.05.22

"""
from django.urls import reverse
from konova.tests.test_views import BaseWorkflowTestCase
from user.models import Team


class UserWorkflowTestCase(BaseWorkflowTestCase):
    """ This test case adds workflow tests

    """

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

    def setUp(self) -> None:
        super().setUp()

        # Add user to team
        self.team.users.add(self.superuser)

    def test_new_team(self):
        """
        Check a normal creation of a new team.

        Returns:

        """
        team_name = self.create_dummy_string()
        team_description = self.create_dummy_string()

        new_url = reverse("user:team-new", args=())

        post_data = {
            "name": team_name,
            "description": team_description,
            "members": [self.superuser.id],
        }
        response = self.client_user.post(
            new_url,
            post_data
        )
        response_code = response.status_code
        self.assertEqual(response_code, 302, msg=f"Unexpected status code received from response ({response_code})")
        new_team = Team.objects.get(
            name=team_name
        )
        self.assertEqual(new_team.description, team_description)
        self.assertEqual([self.superuser], list(new_team.users.all()))
        self.assertEqual([self.superuser], list(new_team.admins.all()), msg="Creator is not admin by default but should!")

    def test_edit_team(self):
        """
        Check editing of an existing team.

        Returns:

        """
        existing_team = self.team
        existing_team_name = existing_team.name
        existing_team_description = existing_team.description

        edited_team_name = self.create_dummy_string()
        edited_team_description = self.create_dummy_string()

        new_url = reverse("user:team-edit", args=(existing_team.id,))

        post_data = {
            "name": edited_team_name,
            "description": edited_team_description,
        }
        # Expect the first try to fail since user is member but not admin of the team
        response = self.client_user.post(
            new_url,
            post_data
        )
        response_code = response.status_code
        self.assertEqual(response_code, 404, msg=f"Unexpected status code received from response ({response_code})")

        # Now add the user to the list of team admins and try again!
        existing_team.admins.add(self.superuser)
        response = self.client_user.post(
            new_url,
            post_data
        )
        response_code = response.status_code
        self.assertEqual(response_code, 200, msg=f"Unexpected status code received from response ({response_code})")

        existing_team.refresh_from_db()
        self.assertEqual(existing_team.description, existing_team_description)
        self.assertEqual(existing_team.name, existing_team_name)
        self.assertEqual([self.superuser], list(existing_team.users.all()))
        self.assertEqual([self.superuser], list(existing_team.admins.all()), msg="Creator is not admin by default but should!")

    def test_leave_team(self):
        """
        Checks leaving of a user from an existing team.

        Returns:

        """
        existing_team = self.team

        new_url = reverse("user:team-leave", args=(existing_team.id,))

        post_data = {
            "confirm": True,
        }
        response = self.client_user.post(
            new_url,
            post_data
        )
        response_code = response.status_code
        self.assertEqual(response_code, 302, msg=f"Unexpected status code received from response ({response_code})")
        existing_team.refresh_from_db()

        self.assertEqual([], list(existing_team.users.all()))
        self.assertEqual([], list(existing_team.admins.all()))

    def test_remove_team(self):
        """
        Checks removing of an existing team.

        Returns:

        """
        existing_team = self.team

        new_url = reverse("user:team-remove", args=(existing_team.id,))

        post_data = {
            "confirm": True,
        }
        # User is member but not admin. This response must fail!
        response = self.client_user.post(
            new_url,
            post_data
        )
        response_code = response.status_code
        self.assertEqual(response_code, 404, msg=f"Unexpected status code received from response ({response_code})")

        # Add user to admins and try again
        existing_team.admins.add(self.superuser)
        response = self.client_user.post(
            new_url,
            post_data
        )
        response_code = response.status_code
        self.assertEqual(response_code, 302, msg=f"Unexpected status code received from response ({response_code})")
        existing_team.refresh_from_db()
        self.assertIsNotNone(existing_team.deleted, msg="Deleted action not created")
        self.assertNotIn(existing_team, self.superuser.shared_teams)