import pytest

from backend.cli import is_unrelated_image_candidate, resolve_article_id_from_permalink
from backend.news_service import (
    GENERIC_FINANCE_FALLBACK_URL,
    fetch_royalty_free_image,
    validate_translation_quality,
)


def test_permalink_resolution_supports_numeric_and_query_formats():
    assert resolve_article_id_from_permalink("123") == 123
    assert resolve_article_id_from_permalink("/?article=456") == 456
    assert resolve_article_id_from_permalink("https://x.test/?article=789") == 789
    assert resolve_article_id_from_permalink("bad") is None


def test_unrelated_image_candidate_filters_pet_terms():
    assert is_unrelated_image_candidate("https://img.example.com/cat-photo.jpg", "Example")
    assert is_unrelated_image_candidate("https://img.example.com/market.jpg", "Wildlife digest")
    assert not is_unrelated_image_candidate(
        "https://img.example.com/nasdaq-chart.jpg", "Market Desk"
    )


def test_translation_quality_validation_accepts_valid_tamil_and_malayalam():
    ta_ok, ta_reason = validate_translation_quality(
        "ஏஐ முதலீட்டில் சந்தை ஏற்றம்",
        "ஏஐ சார்ந்த முதலீட்டு செய்திகளால் இந்திய சந்தையில் உயர்வு ஏற்பட்டது.",
        "ta",
    )
    ml_ok, ml_reason = validate_translation_quality(
        "എഐ നിക്ഷേപ വാർത്തകൾ വിപണിയെ ഉയർത്തി",
        "എഐ കേന്ദ്രിത സാമ്പത്തിക റിപ്പോർട്ടുകൾ മൂലം മാർക്കറ്റ് മുന്നേറ്റം രേഖപ്പെടുത്തി.",
        "ml",
    )
    assert ta_ok and ta_reason is None
    assert ml_ok and ml_reason is None


def test_translation_quality_validation_rejects_script_mismatch_and_gibberish():
    mismatch_ok, mismatch_reason = validate_translation_quality(
        "AI market rally",
        "Stocks up after AI earnings beat expectations.",
        "ta",
    )
    gibberish_ok, gibberish_reason = validate_translation_quality(
        "aaaaaaaabbbbbbbb",
        "xxxxxxxxyyyyyyyy",
        "ml",
    )
    assert not mismatch_ok
    assert mismatch_reason == "script-mismatch"
    assert not gibberish_ok
    assert gibberish_reason in {"repeated-sequence", "script-mismatch"}


@pytest.mark.anyio
async def test_finance_refetch_guard_rejects_pet_image_and_uses_finance_fallback(monkeypatch):
    async def fake_provider(_query: str):
        return "https://example.com/cat-on-wall-street.jpg", "cat photo"

    monkeypatch.setattr(
        "backend.news_service.get_enabled_providers",
        lambda: [("fake", fake_provider)],
    )
    monkeypatch.setattr("backend.config.ROYALTY_IMAGE_MCP_ENDPOINT", "")

    image_url, credit = await fetch_royalty_free_image("stock market rally and earnings")
    assert image_url == GENERIC_FINANCE_FALLBACK_URL
    assert credit == "Finance-safe fallback"
