from ichec_django_core.test import (
    AuthAPITestCase,
    add_group_permissions,
    setup_default_users_and_groups,
)

from marinerg_test_access.models import AccessApplication, AccessApplicationBoardReview
from marinerg_test_access.client.resources import create_review
from marinerg_test_access.test import (
    setup_default_access_calls,
    setup_application,
    setup_review,
)


class TestBoardReviewView(AuthAPITestCase):

    def setUp(self):
        self.url = "/api/access_application_board_reviews/"
        setup_default_users_and_groups()

        add_group_permissions(
            "regular_users",
            AccessApplication,
            [
                "add_accessapplication",
            ],
        )

        add_group_permissions(
            "regular_users",
            AccessApplicationBoardReview,
            [
                "add_accessapplicationboardreview",
            ],
        )

        add_group_permissions(
            "admins",
            AccessApplication,
            [
                "view_accessapplication",
                "add_accessapplication",
            ],
        )

        add_group_permissions(
            "admins",
            AccessApplicationBoardReview,
            [
                "view_accessapplicationboardreview",
            ],
        )

        self.internal, self.public, _ = setup_default_access_calls()
        self.application1 = setup_application(self.public, "applicant")
        self.application2 = setup_application(self.public, "applicant2")

        self.review1 = setup_review(self.application1, "reviewer1")
        self.review2 = setup_review(self.application1, "reviewer2")

        application = self.auth_raw_get(
            "admin_user", f"/api/access_applications/{self.application2.id}/"
        )
        self.template = create_review(application.body["url"])

    def test_list_access(self):

        scenarios = (
            ["", 0, 401, "Public can't see any reviews"],
            ["regular_user", 0, 200, "Registered can't see any reviews"],
            ["member", 0, 200, "Member can't see any reviews"],
            ["coordinator", 2, 200, "Coordinator can see their calls reviews"],
            ["reviewer1", 1, 200, "Reviewer can see their own review"],
            ["chair", 2, 200, "Chair can see their calls reviews"],
            ["applicant", 0, 200, "Applicant can't see reviews"],
            ["admin_user", 2, 200, "Admin can see reviews"],
        )
        for user, count, status, msg in scenarios:
            if user:
                response = self.assert_status(self.auth_list(user), status, msg)
            else:
                response = self.assert_status(self.public_list(), status, msg)

            if status < 400:
                self.assertEqual(response.body.count, count, msg)

    def test_detail_access(self):

        review = self.review1.id
        scenarios = (
            ["", review, 401, "Public can't view review"],
            ["regular_user", review, 404, "Registered can't view review"],
            ["member", review, 404, "Member can't view review"],
            ["applicant", review, 404, "Applicant can't view review"],
            ["coordinator", review, 200, "Coordinator can view review"],
            ["chair", review, 200, "Chair can view review"],
            ["reviewer1", review, 200, "Reviewer can view own review"],
            ["reviewer2", review, 404, "Reviewer can't view other review"],
            ["admin_user", review, 200, "Admin can view review"],
        )

        for user, item, status, msg in scenarios:
            if user:
                self.assert_status(self.auth_detail(user, item), status, msg)
            else:
                self.assert_status(self.detail(item), status, msg)

    def test_create_permissions(self):

        scenarios = (
            ["", 401, "Public can't create, no auth"],
            ["regular_user", 400, "Regular user can't create, not reviewer"],
            ["member", 400, "Member can't create, not reviewer"],
            ["applicant", 400, "Applicant can't create, not reviewer"],
            ["coordinator", 400, "Coordinator can't create, not reviewer"],
            ["chair", 400, "Chair can't create, not reviewer"],
            ["reviewer1", 201, "Reviewer can create"],
            ["admin_user", 400, "Admin can't create, not reviewer"],
        )

        for user, status, msg in scenarios:
            if user:
                self.assert_status(self.auth_create(user, self.template), status, msg)
            else:
                self.assert_status(self.create(self.template), status, msg)
