import pytest

from conftest import assert_data
from program.tests.factories import ShowFactory

pytestmark = pytest.mark.django_db


def url(show=None) -> str:
    return f"/api/v1/shows/{show.id}/" if show else "/api/v1/shows/"


def show_data(
    category, funding_category, host, language, link_type, music_focus, owner, topic, type_
) -> dict[str, str | int | list[dict[str, str | int]]]:
    return {
        "category_ids": [category.id],
        "funding_category_id": funding_category.id,
        "host_ids": [host.id],
        "language_ids": [language.id],
        "links": [
            {
                "type_id": link_type.id,
                "url": "https://aura.radio",
            }
        ],
        "music_focus_ids": [music_focus.id],
        "name": "NAME",
        "owner_ids": [owner.id],
        "short_description": "SHORT DESCRIPTION",
        "slug": "SLUG",
        "topic_ids": [topic.id],
        "type_id": type_.id,
    }


def test_create_show(
    admin_api_client,
    category,
    funding_category,
    profile,
    language,
    link_type,
    music_focus,
    owner,
    topic,
    type_,
):
    data = show_data(
        category, funding_category, profile, language, link_type, music_focus, owner, topic, type_
    )

    response = admin_api_client.post(url(), data=data, format="json")

    assert response.status_code == 201

    assert_data(response, data)


def test_create_show_forbidden_for_common_user(
    common_api_client1,
    category,
    funding_category,
    profile,
    language,
    link_type,
    music_focus,
    owner,
    topic,
    type_,
):
    data = show_data(
        category, funding_category, profile, language, link_type, music_focus, owner, topic, type_
    )

    response = common_api_client1.post(url(), data=data, format="json")

    assert response.status_code == 403


def test_delete_show(admin_api_client, show):
    response = admin_api_client.delete(url(show))

    assert response.status_code == 204


def test_delete_show_forbidden_for_common_user(common_api_client1, show):
    response = common_api_client1.delete(url(show))

    assert response.status_code == 403


def test_list_shows(api_client):
    SHOWS = 3
    ShowFactory.create_batch(size=SHOWS)

    response = api_client.get(url())

    assert response.status_code == 200
    assert len(response.data) == SHOWS


def test_retrieve_show_as_admin_user(admin_api_client, show):
    response = admin_api_client.get(url(show))

    assert response.status_code == 200
    assert response.data["id"] == show.id
    assert "internal_note" in response.data


def test_update_show(
    admin_api_client,
    category,
    funding_category,
    profile,
    language,
    link_type,
    music_focus,
    owner,
    topic,
    type_,
    show,
):
    update = show_data(
        category, funding_category, profile, language, link_type, music_focus, owner, topic, type_
    )

    response = admin_api_client.patch(url(show), data=update, format="json")

    assert response.status_code == 200

    assert_data(response, update)


def test_update_show_links(
    admin_api_client,
    category,
    funding_category,
    profile,
    language,
    link_type,
    music_focus,
    owner,
    topic,
    type_,
    show,
):
    update = show_data(
        category, funding_category, profile, language, link_type, music_focus, owner, topic, type_
    )

    response = admin_api_client.patch(url(show), data=update, format="json")

    assert response.status_code == 200

    assert_data(response, update)


def test_update_show_forbidden_for_common_user(
    common_api_client1,
    category,
    funding_category,
    profile,
    language,
    link_type,
    music_focus,
    owner,
    topic,
    type_,
    show,
):
    update = show_data(
        category, funding_category, profile, language, link_type, music_focus, owner, topic, type_
    )

    response = common_api_client1.patch(url(show), data=update, format="json")

    assert response.status_code == 403


def test_redacted_fields_for_unauthenticated_requests(api_client, show):
    response = api_client.get(url(show))
    data = response.json()

    assert response.status_code == 200

    assert "email" not in data
    assert "internal_note" not in data


def test_redacted_fields_for_common_user(common_api_client1, show):
    response = common_api_client1.get(url(show))
    data = response.json()

    assert response.status_code == 200

    assert "internal_note" not in data