from __future__ import annotations

import json
import logging
import os
from typing import Any, Dict

from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings

from .spiders.public_spider import PublicSpider

logger = logging.getLogger(__name__)


def run_crawl() -> Dict[str, Any]:
    settings = get_project_settings()
    process = CrawlerProcess(settings)
    process.crawl(PublicSpider)
    process.start(stop_after_crawl=True)
    # Scrapy doesn't easily expose counts without signals; keep minimal
    return {"status": "ok"}


def lambda_handler(event, context):
    result = run_crawl()
    return {"statusCode": 200, "body": json.dumps(result)}