#!/usr/bin/env python3
"""Full pipeline dry-run on sample posts with detailed per-post reporting.

Processes data/samples/posts.json against real catalog.db, shows:
- extraction result, matching details, dgred payload — WITHOUT sending to CRM.
"""

from __future__ import annotations

import json
import logging
import sys
from collections import Counter
from datetime import datetime, timedelta
from pathlib import Path

# Ensure src is importable
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src"))

from agent_samochodowy.config import Settings
from agent_samochodowy.dgred.client import DgredClient
from agent_samochodowy.dgred.writer import LeadWriter
from agent_samochodowy.extraction import extract
from agent_samochodowy.extraction.prefilter import is_part_request as prefilter_check
from agent_samochodowy.matcher.engine import match
from agent_samochodowy.matcher.index import CatalogIndex
from agent_samochodowy.models import Post
from agent_samochodowy.status import map_status

logging.basicConfig(
    level=logging.WARNING,
    format="%(asctime)s %(name)s %(levelname)s %(message)s",
)
# Silence httpx noise
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("openai").setLevel(logging.WARNING)

STATUS_ID_MAP = {"Dopasowano": 67, "Do weryfikacji": 68, "Brak dopasowania": 69}


def main() -> None:
    settings = Settings()
    settings.dry_run = True  # force dry-run

    print(f"LLM Provider:      {settings.llm_provider}")
    print(f"LLM Model:         {settings.llm_model}")
    print(f"DB Path:           {settings.db_path}")
    print(f"Post max age:      {settings.post_max_age_hours}h")
    print()

    # Load sample posts
    posts_path = Path(__file__).resolve().parent.parent / "data" / "samples" / "posts.json"
    raw_posts = json.loads(posts_path.read_text())
    posts = [Post(**p) for p in raw_posts]
    total_fetched = len(posts)

    # Freshness filter
    cutoff = datetime.now() - timedelta(hours=settings.post_max_age_hours) if settings.post_max_age_hours > 0 else None
    if cutoff:
        fresh = [p for p in posts if p.timestamp.replace(tzinfo=None) >= cutoff]
        skipped_old = len(posts) - len(fresh)
        posts = fresh
    else:
        skipped_old = 0

    print(f"Posts fetched:     {total_fetched}")
    print(f"Too old (>{settings.post_max_age_hours}h): {skipped_old}")
    print(f"To analyze:        {len(posts)}")
    print("=" * 80)

    index = CatalogIndex(settings.db_path)
    dgred = DgredClient(settings)
    writer = LeadWriter(dgred, settings)

    # Stats
    statuses: Counter[str] = Counter()
    methods: Counter[str] = Counter()
    part_requests = 0

    for post in posts:
        print()
        print(f"{'─' * 80}")
        print(f"POST {post.post_id} | Grupa: {post.group}")
        print(f"Autor: {post.author_name}")
        print(f"Tekst: {post.text}")
        print()

        # Pre-filter
        passes_prefilter = prefilter_check(post.text)
        if not passes_prefilter:
            print(f"  PREFILTER: ODRZUCONY (brak intencji kupna)")
            print(f"  STATUS: --- (pominięty)")
            continue

        # Extract
        query = extract(post, settings)
        if query is None:
            print(f"  PREFILTER: przeszedł, ale ekstraktor: ODRZUCONY")
            print(f"  STATUS: --- (pominięty)")
            continue

        part_requests += 1
        print(f"  KLASYFIKACJA: zapytanie o część = {query.is_part_request}")
        print(f"  POWÓD: {query.reason}")
        print(f"  ATRYBUTY:")
        print(f"    Marka:       {query.brand or '—'}")
        print(f"    Model:       {query.model or '—'}")
        print(f"    Rocznik:     {query.year or '—'}")
        print(f"    Kategoria:   {query.category or '—'}")
        print(f"    Kod silnika: {query.engine_code or '—'}")
        print(f"    Numery OE:   {', '.join(query.oe_numbers) if query.oe_numbers else '—'}")

        # Match
        result = match(query, index)
        status_name = map_status(result, settings)
        status_id = STATUS_ID_MAP.get(status_name, 0)

        statuses[status_name] += 1
        methods[result.method] += 1

        print()
        print(f"  DOPASOWANIE:")
        print(f"    Metoda:      {result.method}")
        print(f"    Pewność:     {result.confidence:.0%}")
        print(f"    Trafień:     {len(result.offers)}")

        if result.offers:
            print(f"    Oferty (top 3):")
            for offer in result.offers[:3]:
                price_str = f"{offer.price} {offer.currency}" if offer.price else "brak ceny"
                print(f"      - {offer.title[:70]}")
                print(f"        {offer.url}")
                print(f"        Cena: {price_str} | OE: {', '.join(offer.oe_numbers[:3])}")

        print()
        print(f"  STATUS: {status_id} = {status_name}")

        # Build dgred payload (dry-run)
        note = writer._build_note(post, query, result, status_name)

        custom_fields: dict = {}
        if query.brand:
            custom_fields["marka"] = query.brand
        if query.model:
            custom_fields["model"] = query.model
        if query.year:
            custom_fields["rocznik"] = query.year
        if query.category:
            custom_fields["kategoria"] = query.category
        if query.engine_code:
            custom_fields["kod_silnika"] = query.engine_code
        if query.oe_numbers:
            custom_fields["numery_oe"] = ", ".join(query.oe_numbers)
        custom_fields["fb_profil"] = post.author_profile_url
        custom_fields["url_posta"] = post.post_url
        custom_fields["grupa"] = post.group

        payload = {
            "client": {
                "name": post.author_name,
                "customFields": custom_fields,
                "tags": [{"tag": "FB Agent", "color": 1}],
                "externalId": post.author_profile_url,
            },
            "clientStatus": status_id,
            "note": note,
        }

        print()
        print(f"  PAYLOAD DO DGRED (dry-run):")
        print(json.dumps(payload, indent=4, ensure_ascii=False))

    index.close()
    dgred.close()

    # Summary
    print()
    print("=" * 80)
    print("PODSUMOWANIE")
    print("=" * 80)
    print(f"Pobrano postów:           {total_fetched}")
    print(f"Za stare (>{settings.post_max_age_hours}h):       {skipped_old}")
    print(f"Do analizy:               {len(posts)}")
    print(f"Zapytania o część:        {part_requests}")
    print(f"Odrzucone (prefilter/LLM): {len(posts) - part_requests}")
    print()
    print("Rozkład statusów:")
    for s in ["Dopasowano", "Do weryfikacji", "Brak dopasowania"]:
        print(f"  {STATUS_ID_MAP.get(s, '?')} {s:25s} {statuses.get(s, 0)}")
    print()
    print("Trafienia wg metody:")
    for m in ["oe", "engine_code", "atrybuty", "fuzzy", "brak"]:
        if methods.get(m, 0):
            print(f"  {m:20s} {methods[m]}")
    print("=" * 80)


if __name__ == "__main__":
    main()
