"""Test for the document favorite_list endpoint."""

import pytest
from rest_framework.test import APIClient

from core import factories, models

pytestmark = pytest.mark.django_db


def test_api_document_favorite_list_anonymous():
    """Anonymous users should receive a 401 error."""
    client = APIClient()

    response = client.get("/api/v1.0/documents/favorite_list/")

    assert response.status_code == 401


def test_api_document_favorite_list_authenticated_no_favorite():
    """Authenticated users should receive an empty list."""
    user = factories.UserFactory()
    client = APIClient()
    client.force_login(user)

    response = client.get("/api/v1.0/documents/favorite_list/")

    assert response.status_code == 200
    assert response.json() == {
        "count": 0,
        "next": None,
        "previous": None,
        "results": [],
    }


def test_api_document_favorite_list_authenticated_with_favorite():
    """Authenticated users with a favorite should receive the favorite."""

    user = factories.UserFactory()
    client = APIClient()
    client.force_login(user)

    # If the user doesn't have access to this document (e.g the user had access
    # and this access was removed), it should not be in the favorite list anymore.
    factories.DocumentFactory(favorited_by=[user])

    document = factories.UserDocumentAccessFactory(
        user=user, role=models.RoleChoices.READER, document__favorited_by=[user]
    ).document

    response = client.get("/api/v1.0/documents/favorite_list/")

    assert response.status_code == 200
    assert response.json() == {
        "count": 1,
        "next": None,
        "previous": None,
        "results": [
            {
                "abilities": document.get_abilities(user),
                "ancestors_link_reach": None,
                "ancestors_link_role": None,
                "computed_link_reach": document.computed_link_reach,
                "computed_link_role": document.computed_link_role,
                "created_at": document.created_at.isoformat().replace("+00:00", "Z"),
                "creator": str(document.creator.id),
                "deleted_at": None,
                "content": document.content,
                "depth": document.depth,
                "excerpt": document.excerpt,
                "id": str(document.id),
                "is_favorite": True,
                "link_reach": document.link_reach,
                "link_role": document.link_role,
                "nb_accesses_ancestors": 1,
                "nb_accesses_direct": 1,
                "numchild": document.numchild,
                "path": document.path,
                "title": document.title,
                "updated_at": document.updated_at.isoformat().replace("+00:00", "Z"),
                "user_role": "reader",
            }
        ],
    }


def test_api_document_favorite_list_with_favorite_children():
    """Authenticated users should receive their favorite documents, including children."""

    user = factories.UserFactory()
    client = APIClient()
    client.force_login(user)

    root = factories.DocumentFactory(creator=user, users=[user])
    children = factories.DocumentFactory.create_batch(
        2, parent=root, favorited_by=[user]
    )

    access = factories.UserDocumentAccessFactory(
        user=user, role=models.RoleChoices.READER, document__favorited_by=[user]
    )

    other_root = factories.DocumentFactory(creator=user, users=[user])
    factories.DocumentFactory.create_batch(2, parent=other_root)

    response = client.get("/api/v1.0/documents/favorite_list/")

    assert response.status_code == 200
    assert response.json()["count"] == 3

    content = response.json()["results"]

    assert content[0]["id"] == str(children[0].id)
    assert content[1]["id"] == str(children[1].id)
    assert content[2]["id"] == str(access.document.id)
