"""
scrylo/scrapers/run.py
-----------------------
Entry point for lead scraping. Thin wrapper over finder.py.

All scraping logic lives in finder.py.
This file exists for backward compatibility with the CLI and Web UI SSE endpoint.

Pipeline (defined in finder.py):
  Serper → fetch pages (Patchright/httpx) → LLM extract → save to DB
"""

from __future__ import annotations

import asyncio
import logging
from dataclasses import dataclass, field

from scrylo.scrapers.finder import find_leads, FinderResult

log = logging.getLogger("scrylo.run")


@dataclass
class ScrapeResult:
    query:                   str
    source:                  str       = "serper"
    leads_found:             int       = 0
    leads_saved:             int       = 0
    leads_skipped_duplicate: int       = 0
    domains_fetched:         int       = 0
    emails_found:            int       = 0
    emails_verified:         int       = 0
    linkedin_dm_count:       int       = 0
    errors:                  list[str] = field(default_factory=list)
    lead_ids:                list[int] = field(default_factory=list)

    @classmethod
    def from_finder(cls, query: str, fr: FinderResult) -> "ScrapeResult":
        return cls(
            query                    = query,
            source                   = "serper",
            leads_found              = fr.leads_saved + fr.leads_skipped,
            leads_saved              = fr.leads_saved,
            leads_skipped_duplicate  = fr.leads_skipped,
            domains_fetched          = fr.pages_fetched,
            emails_found             = fr.emails_found,
            emails_verified          = fr.emails_verified,
            errors                   = fr.errors,
            lead_ids                 = fr.lead_ids,
        )


async def run_scrape(
    query:               str,
    limit:               int  = 40,
    source:              str  = "serper",
    fetch_company_pages: bool = True,
    verify_emails:       bool = True,
    progress_callback         = None,
) -> ScrapeResult:
    fr = await find_leads(
        query             = query,
        limit             = limit,
        progress_callback = progress_callback,
        fetch_pages       = fetch_company_pages,
    )
    return ScrapeResult.from_finder(query, fr)


def run_scrape_sync(
    query:         str,
    limit:         int  = 40,
    source:        str  = "serper",
    verify_emails: bool = True,
) -> ScrapeResult:
    """Blocking wrapper for Click CLI."""
    return asyncio.run(run_scrape(query, limit, source))
