"""WS5.12 — multilingual activation pin test.

The contract requires English / French / Arabic / Portuguese activation.
This test is small and structural — it ensures the framework wiring stays
in place. Translation completeness lives in the .po files under /locale/
and is reviewed separately.
"""
from __future__ import annotations

from pathlib import Path

from django.conf import settings


def test_languages_includes_required_set():
    """Contract: en + fr + ar + pt activated."""
    codes = {code for code, _ in settings.LANGUAGES}
    assert {"en", "fr", "ar", "pt"}.issubset(codes), (
        f"Required languages missing from settings.LANGUAGES: {codes}"
    )


def test_locale_paths_configured():
    """LOCALE_PATHS must point at a real directory so makemessages has somewhere to write."""
    assert settings.LOCALE_PATHS, "LOCALE_PATHS must be set"
    for p in settings.LOCALE_PATHS:
        assert Path(p).is_dir(), f"LOCALE_PATHS entry does not exist: {p}"


def test_locale_subdirs_exist_for_each_active_language():
    """Each activated non-English language must have an LC_MESSAGES subdir
    so makemessages -l <code> succeeds out of the box."""
    base = Path(settings.LOCALE_PATHS[0])
    for code, _label in settings.LANGUAGES:
        if code == "en":
            continue  # source language, no .po needed
        sub = base / code / "LC_MESSAGES"
        assert sub.is_dir(), f"locale/{code}/LC_MESSAGES is missing"


def test_locale_middleware_in_chain():
    """LocaleMiddleware must be active for request.LANGUAGE_CODE to resolve."""
    assert "django.middleware.locale.LocaleMiddleware" in settings.MIDDLEWARE


def test_locale_middleware_after_session_middleware():
    """LocaleMiddleware reads session, so it must come AFTER SessionMiddleware."""
    chain = list(settings.MIDDLEWARE)
    sess_idx = chain.index("django.contrib.sessions.middleware.SessionMiddleware")
    locale_idx = chain.index("django.middleware.locale.LocaleMiddleware")
    assert locale_idx > sess_idx, "LocaleMiddleware must come after SessionMiddleware"


def test_use_i18n_enabled():
    assert settings.USE_I18N, "USE_I18N must stay True for translation lookups"
