"""
Test config API endpoints in the messages core app.
"""

from django.test import override_settings

import pytest
from rest_framework.status import (
    HTTP_200_OK,
)
from rest_framework.test import APIClient

from core import factories

pytestmark = pytest.mark.django_db


@override_settings(
    LANGUAGES=[["en-us", "English"], ["fr-fr", "French"], ["de-de", "German"]],
    LANGUAGE_CODE="en-us",
    AI_API_KEY=None,
    AI_BASE_URL=None,
    AI_MODEL=None,
    FEATURE_AI_SUMMARY=False,
    FEATURE_AI_AUTOLABELS=False,
    FEATURE_MAILBOX_ADMIN_CHANNELS=[],
    DRIVE_CONFIG={"base_url": None, "app_name": "Drive"},
    MAX_OUTGOING_ATTACHMENT_SIZE=20971520,  # 20MB
    MAX_OUTGOING_BODY_SIZE=5242880,  # 5MB
    MAX_INCOMING_EMAIL_SIZE=10485760,  # 10MB
    MAX_RECIPIENTS_PER_MESSAGE=42,
    MAX_TEMPLATE_IMAGE_SIZE=2097152,  # 2MB
    IMAGE_PROXY_ENABLED=False,
    MESSAGES_MANUAL_RETRY_MAX_AGE=86400,  # 1 day in seconds
)
@pytest.mark.parametrize("is_authenticated", [False, True])
def test_api_config(is_authenticated):
    """Anonymous users should be allowed to get the configuration."""
    client = APIClient()

    if is_authenticated:
        user = factories.UserFactory()
        client.force_login(user)

    response = client.get("/api/v1.0/config/")
    assert response.status_code == HTTP_200_OK
    assert response.json() == {
        "ENVIRONMENT": "test",
        "LANGUAGES": [["en-us", "English"], ["fr-fr", "French"], ["de-de", "German"]],
        "LANGUAGE_CODE": "en-us",
        "AI_ENABLED": False,
        "FEATURE_AI_SUMMARY": False,
        "FEATURE_AI_AUTOLABELS": False,
        "FEATURE_MAILBOX_ADMIN_CHANNELS": [],
        "SCHEMA_CUSTOM_ATTRIBUTES_USER": {},
        "SCHEMA_CUSTOM_ATTRIBUTES_MAILDOMAIN": {},
        "MAX_INCOMING_EMAIL_SIZE": 10485760,
        "MAX_OUTGOING_ATTACHMENT_SIZE": 20971520,
        "MAX_OUTGOING_BODY_SIZE": 5242880,
        "MAX_RECIPIENTS_PER_MESSAGE": 42,
        "MAX_TEMPLATE_IMAGE_SIZE": 2097152,
        "IMAGE_PROXY_ENABLED": False,
        "MESSAGES_MANUAL_RETRY_MAX_AGE": 86400,
    }


@override_settings(
    DRIVE_CONFIG={
        "base_url": "http://localhost:8902",
        "sdk_url": "/sdk",
        "api_url": "/api/v1.0",
        "file_url": "/explorer/items/files",
        "app_name": "Drive App",
    }
)
def test_api_config_with_external_services():
    """If Drive external service is configured, it should be included in the configuration."""
    client = APIClient()

    response = client.get("/api/v1.0/config/")
    assert response.status_code == HTTP_200_OK
    assert response.json().get("DRIVE") == {
        "sdk_url": "http://localhost:8902/sdk",
        "api_url": "http://localhost:8902/api/v1.0",
        "file_url": "http://localhost:8902/explorer/items/files",
        "app_name": "Drive App",
    }
