# retoor <retoor@molodetz.nl>

import os
import logging

from devplacepy.database import get_setting, internal_gateway_key

logger = logging.getLogger(__name__)


def _get_ai_key(setting: str = "news_ai_key", fallback_setting: str | None = None) -> str:
    key = os.environ.get("NEWS_AI_KEY")
    if key:
        return key
    key = get_setting(setting, "")
    if key:
        return key
    if fallback_setting:
        key = get_setting(fallback_setting, "")
        if key:
            return key
    return internal_gateway_key()


from .constants import (
    AI_MODEL_DEFAULT,
    AI_URL_DEFAULT,
    FEATURE_MIN_SCORE,
    FORMAT_INPUT_MAX_CHARS,
    FORMAT_MAX_TOKENS,
    FORMAT_MODEL_DEFAULT,
    FORMAT_OUTPUT_MAX_CHARS,
    FORMAT_PROMPT_SPEC,
    FORMAT_URL_DEFAULT,
    GRADE_MAX_TOKENS,
    GRADE_PROMPT_SPEC,
    GRADE_THRESHOLD_DEFAULT,
    GRADING_RULES_DESCRIPTION,
    IMG_FETCH_MAX_BYTES,
    IMG_FETCH_TIMEOUT,
    IMG_PER_ARTICLE,
    INTERNAL_GATEWAY_URL,
    INTERNAL_MODEL,
    JUNK_PATTERNS,
    LANDING_MAX,
    LANDING_MIN_SCORE,
    LANDING_RECENCY_DAYS,
    MAX_TITLE_CAPS_RATIO,
    MIN_BODY_CHARS,
    MIN_IMAGE_DIMENSION,
    MIN_TITLE_CHARS,
    NEWS_API_URL_DEFAULT,
    NEWS_SUMMARY,
    PHASH_DISTANCE,
    THIN_CONTENT_PENALTY,
    UNIQUE_IMAGE_BONUS,
    WHITESPACE_PATTERN,
    re,
)
from .models import ArticleGrade, ImageCandidate, dataclass, field
from .clean import (
    _caps_ratio,
    _extract_grade,
    _is_valid_url,
    _strip_md_fence,
    clean_news_text,
    effective_score,
    reliability_reason,
    strip_html,
)
from .images import (
    Image,
    _decode_phash,
    _fetch_image_candidate,
    _flag_shared_placeholders,
    _get_article_images,
    _primary_image,
    asyncio,
    httpx,
    imagehash,
    io,
    net_guard,
)
from .service import (
    BaseService,
    ConfigField,
    NewsService,
    accumulate_usage,
    add_news_usage,
    audit,
    datetime,
    generate_uid,
    get_news_usage,
    get_table,
    make_combined_slug,
    new_usage_totals,
    schedule_seo_meta,
    stealth,
    timedelta,
    timezone,
    usage_metric_cards,
)
