import pytest
from django.contrib.auth import get_user_model
from django.urls import reverse

from atlas.models import CaseCollection, CaseDetail, CaseReviewMessage


@pytest.mark.django_db
def test_collection_case_query_messaging_enabled_default_true():
    collection = CaseCollection.objects.create(name="Default messaging collection")

    assert collection.case_query_messaging_enabled is True


@pytest.mark.django_db
def test_collection_case_review_thread_returns_404_when_messaging_disabled(create_case, client):
    user_model = get_user_model()
    author = user_model.objects.create_user(username="author-thread", password="testpass")

    collection = CaseCollection.objects.create(
        name="Thread disabled",
        case_query_messaging_enabled=False,
    )
    collection.author.add(author)

    case = create_case
    CaseDetail.objects.create(collection=collection, case=case, sort_order=0)
    attempt = collection.get_or_create_cid_user_exam(user_user=author)

    client.force_login(author)
    url = reverse(
        "atlas:collection_case_review_thread",
        kwargs={
            "exam_id": collection.pk,
            "cid_user_exam_id": attempt.pk,
            "case_id": case.pk,
        },
    )

    response = client.get(url)

    assert response.status_code == 404


@pytest.mark.django_db
def test_collection_user_messages_returns_404_when_messaging_disabled(create_case, client):
    user_model = get_user_model()
    author = user_model.objects.create_user(username="author-user-messages", password="testpass")
    learner = user_model.objects.create_user(username="learner-user-messages", password="testpass")

    collection = CaseCollection.objects.create(
        name="User messages disabled",
        case_query_messaging_enabled=False,
    )
    collection.author.add(author)

    case = create_case
    CaseDetail.objects.create(collection=collection, case=case, sort_order=0)
    collection.get_or_create_cid_user_exam(user_user=learner)

    client.force_login(author)
    url = reverse(
        "atlas:collection_user_messages",
        kwargs={"exam_id": collection.pk, "user_pk": learner.pk},
    )

    response = client.get(url)

    assert response.status_code == 404


@pytest.mark.django_db
def test_user_messages_inbox_ignores_disabled_collection_messages(create_case, client):
    user_model = get_user_model()
    moderator = user_model.objects.create_user(username="moderator", password="testpass")
    learner = user_model.objects.create_user(username="learner", password="testpass")

    enabled_collection = CaseCollection.objects.create(
        name="Enabled messaging collection",
        case_query_messaging_enabled=True,
    )
    disabled_collection = CaseCollection.objects.create(
        name="Disabled messaging collection",
        case_query_messaging_enabled=False,
    )
    enabled_collection.author.add(moderator)
    disabled_collection.author.add(moderator)

    case = create_case
    CaseDetail.objects.create(collection=enabled_collection, case=case, sort_order=0)
    CaseDetail.objects.create(collection=disabled_collection, case=case, sort_order=0)

    enabled_attempt = enabled_collection.get_or_create_cid_user_exam(user_user=learner)
    disabled_attempt = disabled_collection.get_or_create_cid_user_exam(user_user=learner)

    CaseReviewMessage.objects.create(
        cid_user_exam=enabled_attempt,
        case=case,
        author=learner,
        message_type=CaseReviewMessage.MessageType.QUERY,
        message="Enabled message",
        requires_acknowledgement=True,
    )
    CaseReviewMessage.objects.create(
        cid_user_exam=disabled_attempt,
        case=case,
        author=learner,
        message_type=CaseReviewMessage.MessageType.QUERY,
        message="Disabled message",
        requires_acknowledgement=True,
    )

    client.force_login(moderator)
    response = client.get(reverse("atlas:user_messages_inbox"))

    assert response.status_code == 200
    assert "Enabled messaging collection" in response.content.decode()
    assert "Disabled messaging collection" not in response.content.decode()
