# retoor <retoor@molodetz.nl>
from __future__ import annotations

from dataclasses import dataclass

GIT_SIZE_LIMIT_BYTES: int = 3 * 1024 * 1024 * 1024
GIT_CLONE_TIMEOUT_SECONDS: int = 900
GIT_PROBE_TIMEOUT_SECONDS: int = 25
GIT_SIZE_POLL_SECONDS: float = 2.0

WEBSITE_MAX_PAGES: int = 40
WEBSITE_MAX_FILES: int = 150
WEBSITE_MAX_TOTAL_BYTES: int = 80 * 1024 * 1024
WEBSITE_MAX_FILE_BYTES: int = 5 * 1024 * 1024
WEBSITE_MAX_DEPTH: int = 2
WEBSITE_REQUEST_TIMEOUT_SECONDS: float = 30.0

ANALYSIS_FILE_CAP_BYTES: int = 512 * 1024
ANALYSIS_MAX_FILES: int = 2000
AI_SAMPLE_LIMIT: int = 12
AI_EXCERPT_CHARS: int = 6000

LLM_MODEL: str = "molodetz"
LLM_TIMEOUT_SECONDS: float = 180.0
LLM_MAX_RETRIES: int = 3
LLM_RETRY_BACKOFF_SECONDS: float = 2.0
LLM_TEMPERATURE: float = 0.0

WORKER_TIMEOUT_SECONDS: int = 3600
SOURCE_URL_MAX_LENGTH: int = 2048

IMAGE_MAX_COUNT: int = 20
IMAGE_CONCURRENCY: int = 5
IMAGE_MIN_BYTES: int = 6 * 1024
IMAGE_MAX_BYTES: int = 12 * 1024 * 1024
IMAGE_MIN_DIMENSION: int = 128
IMAGE_EXTENSIONS: tuple[str, ...] = (".png", ".jpg", ".jpeg", ".webp", ".avif", ".bmp", ".gif")
VISION_TIMEOUT_SECONDS: float = 120.0


THUMBNAIL_MAX_DIMENSION: int = 480
THUMBNAIL_QUALITY: int = 80

DOM_ANALYSIS_MAX_PAGES: int = 1
DOM_CAPTURE_TIMEOUT_MS: int = 8000
CONSOLE_SAMPLE_CAP: int = 20
HEADER_VALUE_CAP: int = 500
DOM_AI_WEIGHT: float = 0.12


@dataclass(frozen=True)
class WorkerSettings:
    allow_private_hosts: bool
    llm_endpoint: str
    llm_model: str
    llm_api_key: str
    ai_review_enabled: bool
    image_review_enabled: bool
    image_max_count: int
    media_dir: str
    template_detection: bool


def settings_from_payload(payload: dict) -> WorkerSettings:
    return WorkerSettings(
        allow_private_hosts=bool(payload.get("allow_private", False)),
        llm_endpoint=str(payload.get("llm_endpoint", "")),
        llm_model=str(payload.get("llm_model", LLM_MODEL)),
        llm_api_key=str(payload.get("api_key", "")),
        ai_review_enabled=bool(payload.get("ai_review", True)),
        image_review_enabled=bool(payload.get("image_review", True)),
        image_max_count=int(payload.get("image_max", IMAGE_MAX_COUNT)),
        media_dir=str(payload.get("media_dir", "")),
        template_detection=bool(payload.get("template_detection", True)),
    )
