from __future__ import annotations

import json
import logging
from typing import Any, Dict

from storage.config import load_config_from_env
from storage.dynamodb_adapter import DynamoDBStorage
from storage.s3_adapter import S3Storage
from .extract import extract_article, detect_language

logger = logging.getLogger(__name__)


def handle_record(record: Dict[str, Any], s3: S3Storage, ddb: DynamoDBStorage) -> Dict[str, Any]:
    url = record.get("url", "")
    html = record.get("html", "")
    extracted = extract_article(url, html)
    lang = detect_language(extracted.text)
    payload = {
        "doc_id": url,
        "created_at": record.get("fetched_at", ""),
        "url": url,
        "title": extracted.title,
        "author": extracted.author,
        "date": extracted.date,
        "canonical_url": extracted.canonical_url,
        "extracted_text": extracted.text[:50000],
        "language": lang,
        "raw_html_s3_path": record.get("raw_html_s3_path", ""),
        "http_status": record.get("http_status"),
    }
    ddb.put_document(payload)
    return payload


def lambda_handler(event, context):
    cfg = load_config_from_env()
    no_aws = False
    s3 = S3Storage(cfg, no_aws=no_aws)
    ddb = DynamoDBStorage(cfg, no_aws=no_aws)
    ddb.ensure_table()
    records = event.get("records", [])
    outputs = [handle_record(r, s3, ddb) for r in records]
    return {"statusCode": 200, "body": json.dumps({"count": len(outputs)})}


