import re

import pytest

from backend.app.services.platforms import (
    PLATFORMS,
    PlatformInfo,
    auth_type_for,
    known_platform_keys,
    to_dict,
)


def test_known_platform_keys_is_gs_five():
    assert known_platform_keys() == frozenset({
        "patreon", "subscribestar", "hentaifoundry",
        "discord", "pixiv",
    })


def test_fanbox_not_in_registry():
    # Sanity check — FC-3a added 'fanbox' by mistake; it's not a GS platform.
    assert "fanbox" not in PLATFORMS


def test_deviantart_is_retired():
    # #3069 executed the 2026-07-05 drop decision (FC downloaders = ART-
    # DEDICATED services only). The registry is what /api/platforms, the
    # source validator and the credential validator all read, so its absence
    # here is what actually retires the platform everywhere else.
    assert "deviantart" not in PLATFORMS
    assert auth_type_for("deviantart") is None


def test_auth_type_for_known_and_unknown():
    assert auth_type_for("patreon") == "cookies"
    assert auth_type_for("discord") == "token"
    assert auth_type_for("nope") is None


def test_each_platform_has_required_fields():
    for key, info in PLATFORMS.items():
        assert isinstance(info, PlatformInfo)
        assert info.key == key
        assert info.name
        assert info.description
        assert info.auth_type in ("cookies", "token")
        assert isinstance(info.requires_auth, bool)
        re.compile(info.url_pattern)  # valid regex
        assert info.url_examples
        assert isinstance(info.default_config, dict)


def test_to_dict_round_trip():
    d = to_dict(PLATFORMS["patreon"])
    assert d["key"] == "patreon"
    assert d["auth_type"] == "cookies"
    assert d["default_config"]["sleep"] == 3.0
    assert "url_examples" in d
