import pytest
from mock.mock import patch

from rhodecode.model.db import PullRequestReviewers, User
from rhodecode.tests.routes import route_path


@pytest.mark.usefixtures("autologin_user", "app")
@pytest.mark.backends("git", "hg")
class TestAiCodeReview:
    def test_pr_closed_review_not_possible(self, backend, pr_util):
        pull_request = pr_util.create_pull_request()
        pull_request.status = pull_request.STATUS_CLOSED

        with patch("rhodecode.apps.repository.views.repo_pull_requests.run_task") as run_task_mock:
            response = self.app.post(
                route_path(
                    "pullrequest_start_ai_code_review",
                    repo_name=pull_request.target_repo.repo_name,
                    pull_request_id=pull_request.pull_request_id,
                )
            )

            session = response.get_session_from_response()
            err_msg = session.peek_flash("error")
            assert response.status_code == 200
            assert response.json_body == {"response": True, "redirect_url": None}
            assert len(err_msg) == 1
            assert err_msg[0] == "Cannot start review for closed pull requests."
            assert run_task_mock.call_count == 0

    def test_ai_review_finished(self, backend, pr_util):
        pull_request = pr_util.create_pull_request()
        pull_request.ai_code_review_state = {"review_state": "finished"}

        repo_name = pull_request.target_repo.repo_name
        pull_request_id = pull_request.pull_request_id

        with patch("rhodecode.apps.repository.views.repo_pull_requests.run_task") as run_task_mock:
            response = self.app.post(
                route_path("pullrequest_start_ai_code_review", repo_name=repo_name, pull_request_id=pull_request_id)
            )
            redirect_url = route_path(
                "pullrequest_show",
                repo_name=repo_name,
                pull_request_id=pull_request_id,
            )

            session = response.get_session_from_response()
            err_msg = session.peek_flash("error")
            success_message = session.peek_flash("success")

            assert response.status_code == 200
            assert response.json_body == {"response": True, "redirect_url": redirect_url}
            assert len(err_msg) == 0
            assert len(success_message) == 0
            assert run_task_mock.call_count == 0

    def test_ai_user_added_to_reviewers_and_review_started(self, backend, pr_util):
        pull_request = pr_util.create_pull_request()
        pr_id = pull_request.pull_request_id

        with patch("rhodecode.apps.repository.views.repo_pull_requests.run_task") as run_task_mock:
            response = self.app.post(
                route_path(
                    "pullrequest_start_ai_code_review",
                    repo_name=pull_request.target_repo.repo_name,
                    pull_request_id=pr_id,
                )
            )

            existing_reviewers = PullRequestReviewers.get_pull_request_reviewers(
                pr_id, role=PullRequestReviewers.ROLE_REVIEWER
            )

            ai_user = User.get_ai_user()

            session = response.get_session_from_response()
            success_message = session.peek_flash("success")

            assert response.status_code == 200
            assert run_task_mock.call_count == 1
            assert ai_user.user_id in [u.user_id for u in existing_reviewers]
            assert len(success_message) == 1
            assert success_message[0] == "AI code review has started. The review will be ready in about 1–3 minutes."
