"""Inbound Gmail → filter → classify → auto-create Positive leads."""

from __future__ import annotations

import json
import re
from datetime import date, datetime, timezone
from typing import Any

from sqlalchemy.orm import Session

from app.config import settings
from app.crud import create_lead
from email.message import EmailMessage

from app.email_service import _smtp_send
from app.gmail_oauth import (
    GmailAuthError,
    account_has_readonly,
    get_default_account,
    get_message_detail,
    list_accounts,
    list_recent_messages,
)
from app.inbound_ai import (
    ai_classify_enabled,
    classify_interest_with_openai,
)
from app.models import InboundEmailEvent, InboundEmailRules, Lead
from app.schemas import LeadCreate

_URL_RE = re.compile(r"https?://[^\s<>\"']+", re.I)

# Cold-outreach subjects: "Friendly opener | TRACKCODE brand.or.domain"
# or "… | brand.or.domain TRACKCODE". Brand string varies; `|` + tracking code is stable.
# Tracking code = 5–12 chars with both letters and digits (e.g. QRV7G00, W5MMQV2).
_TRACK_CODE_RE = re.compile(r"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z0-9]{5,12}$")
_DOTTED_BRAND_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9.-]*\.[A-Za-z0-9.-]+$")

# Starter lists — empty filter means "all new inbox mail" (documented).
_DEFAULT_POSITIVE = [
    "interested",
    "yes",
    "sounds good",
    "let's talk",
    "lets talk",
    "schedule a call",
    "book a call",
    "please share",
    "pricing",
    "demo",
]
_DEFAULT_NEGATIVE = [
    "not interested",
    "no thanks",
    "unsubscribe",
    "remove me",
    "stop emailing",
    "don't contact",
    "do not contact",
]

# Do NOT put outreach tracking brands (e.g. azure.crystal.nature) here —
# they appear on Re: subjects of real prospect replies. New cold mail is
# blocked by is_pipe_tracking_spam_subject() instead.
_ALWAYS_NEGATIVE: list[str] = []

# Strip these from stored negative lists (legacy rows may still have them).
_REMOVE_FROM_NEGATIVE = [
    "azure.crystal.nature",
]


_last_run_at: datetime | None = None
_last_run_summary: str | None = None


def _parse_keywords(raw: str | None) -> list[str]:
    if not raw:
        return []
    try:
        data = json.loads(raw)
        if isinstance(data, list):
            return [str(x).strip() for x in data if str(x).strip()]
    except json.JSONDecodeError:
        pass
    return [p.strip() for p in re.split(r"[\n,]+", raw) if p.strip()]


def _dump_keywords(items: list[str]) -> str:
    cleaned = [str(x).strip() for x in items if str(x).strip()]
    return json.dumps(cleaned)


def _ensure_always_negative(row: InboundEmailRules) -> bool:
    """Normalize negative list (add required / drop legacy outreach brands)."""
    existing = _parse_keywords(row.negative_keywords)
    remove_cf = {x.casefold() for x in _REMOVE_FROM_NEGATIVE}
    cleaned = [x for x in existing if x.casefold() not in remove_cf]
    existing_cf = {x.casefold() for x in cleaned}
    changed = len(cleaned) != len(existing)
    for kw in _ALWAYS_NEGATIVE:
        if kw.casefold() not in existing_cf:
            cleaned.append(kw)
            existing_cf.add(kw.casefold())
            changed = True
    if changed:
        row.negative_keywords = _dump_keywords(cleaned)
    return changed


def get_or_create_rules(db: Session) -> InboundEmailRules:
    row = db.query(InboundEmailRules).order_by(InboundEmailRules.id).first()
    if row:
        if _ensure_always_negative(row):
            db.add(row)
            db.commit()
            db.refresh(row)
        return row
    row = InboundEmailRules(
        enabled=False,
        filter_keywords="[]",
        positive_keywords=_dump_keywords(_DEFAULT_POSITIVE),
        negative_keywords=_dump_keywords(_DEFAULT_NEGATIVE),
        poll_interval_seconds=settings.inbound_mail_poll_seconds or 120,
    )
    db.add(row)
    db.commit()
    db.refresh(row)
    return row


def rules_to_dict(row: InboundEmailRules) -> dict[str, Any]:
    return {
        "enabled": bool(row.enabled),
        "filter_keywords": _parse_keywords(row.filter_keywords),
        "positive_keywords": _parse_keywords(row.positive_keywords),
        "negative_keywords": _parse_keywords(row.negative_keywords),
        "poll_interval_seconds": int(row.poll_interval_seconds or 120),
    }


def update_rules(db: Session, **fields: Any) -> InboundEmailRules:
    row = get_or_create_rules(db)
    if "enabled" in fields and fields["enabled"] is not None:
        row.enabled = bool(fields["enabled"])
    if "filter_keywords" in fields and fields["filter_keywords"] is not None:
        row.filter_keywords = _dump_keywords(fields["filter_keywords"])
    if "positive_keywords" in fields and fields["positive_keywords"] is not None:
        row.positive_keywords = _dump_keywords(fields["positive_keywords"])
    if "negative_keywords" in fields and fields["negative_keywords"] is not None:
        row.negative_keywords = _dump_keywords(fields["negative_keywords"])
    if "poll_interval_seconds" in fields and fields["poll_interval_seconds"] is not None:
        row.poll_interval_seconds = max(30, int(fields["poll_interval_seconds"]))
    db.add(row)
    db.commit()
    db.refresh(row)
    return row


def _match_terms(text: str, keywords: list[str]) -> list[str]:
    hay = (text or "").casefold()
    hits: list[str] = []
    for kw in keywords:
        term = (kw or "").strip()
        if term and term.casefold() in hay:
            hits.append(term)
    return hits


_REPLY_PREFIX_RE = re.compile(
    r"^((re|fw|fwd|aw|sv|antw|rif|回复|轉寄|转发)\s*:\s*)+",
    re.I,
)


def is_pipe_tracking_spam_subject(subject: str) -> bool:
    """True for *new* cold subjects like 'Hi Name | QRV7G00 brand.name'.

    Requires a `|` and, after the last pipe, a mixed alphanumeric tracking code.
    Does NOT treat bare `|` alone as spam (avoids normal subjects like 'Notes | John').

    Replies/forwards (`Re: … | CODE …`) are allowed through — those are often
    prospect replies to *our* outreach and should be AI-classified for interest.
    """
    subj = (subject or "").strip()
    if "|" not in subj:
        return False
    # Prospect reply to our tracked outreach — do not hard-reject
    if _REPLY_PREFIX_RE.match(subj):
        return False
    tail = subj.rsplit("|", 1)[-1].strip()
    if not tail:
        return False
    tokens = [t for t in re.split(r"\s+", tail) if t]
    if not tokens or len(tokens) > 3:
        return False
    has_track = any(_TRACK_CODE_RE.match(t) for t in tokens)
    if not has_track:
        return False
    # Typical: only track code, or track + dotted brand (either order), maybe one extra token
    if len(tokens) == 1:
        return True
    has_brand = any(_DOTTED_BRAND_RE.match(t) for t in tokens)
    if has_brand:
        return True
    # Two short opaque tokens after pipe (code + alternate brand without dots)
    return len(tokens) == 2 and all(len(t) <= 48 for t in tokens)


def classify_message(
    *,
    subject: str,
    body: str,
    filter_keywords: list[str],
    positive_keywords: list[str],
    negative_keywords: list[str],
    from_email: str = "",
    from_name: str = "",
    use_ai: bool | None = None,
) -> dict[str, Any]:
    """Filter gate + hard negatives, then OpenAI (or keyword) interest detect.

    Order:
    1) filter keywords (empty = all)
    2) pipe-tracking spam / negative keywords → negative (never create lead)
    3) OpenAI interest classify when enabled → positive creates lead
    4) fallback: positive keywords → positive; else skipped
    """
    blob = f"{subject or ''}\n{body or ''}"
    filter_hits = _match_terms(blob, filter_keywords)
    # Empty filter list → process all (documented behavior)
    filter_matched = True if not filter_keywords else bool(filter_hits)
    if not filter_matched:
        return {
            "filter_matched": False,
            "classification": "ignored",
            "matched_filter_terms": [],
            "matched_sentiment_terms": [],
        }

    if is_pipe_tracking_spam_subject(subject):
        return {
            "filter_matched": True,
            "classification": "negative",
            "matched_filter_terms": filter_hits,
            "matched_sentiment_terms": ["pipe-tracking-subject"],
        }

    # Skip outreach-tracking brands in negatives — they appear on Re: subjects.
    # New cold mail with those brands is handled by is_pipe_tracking_spam_subject.
    remove_cf = {x.casefold() for x in _REMOVE_FROM_NEGATIVE}
    neg_keywords_effective = [
        kw for kw in negative_keywords if kw.casefold() not in remove_cf
    ]
    neg_hits = _match_terms(blob, neg_keywords_effective)
    if neg_hits:
        return {
            "filter_matched": True,
            "classification": "negative",
            "matched_filter_terms": filter_hits,
            "matched_sentiment_terms": neg_hits,
        }

    run_ai = ai_classify_enabled() if use_ai is None else bool(use_ai)
    if run_ai:
        # When AI is on, it alone decides positive vs not (avoids keyword false positives).
        ai = classify_interest_with_openai(
            subject=subject,
            body=body,
            from_email=from_email,
            from_name=from_name,
        )
        label = ai.get("label") or "neutral"
        term = f"ai:{ai.get('reason') or label}"
        if label == "positive":
            return {
                "filter_matched": True,
                "classification": "positive",
                "matched_filter_terms": filter_hits,
                "matched_sentiment_terms": [term],
            }
        if label == "negative":
            return {
                "filter_matched": True,
                "classification": "negative",
                "matched_filter_terms": filter_hits,
                "matched_sentiment_terms": [term],
            }
        return {
            "filter_matched": True,
            "classification": "skipped",
            "matched_filter_terms": filter_hits,
            "matched_sentiment_terms": [term],
        }

    pos_hits = _match_terms(blob, positive_keywords)
    if pos_hits:
        return {
            "filter_matched": True,
            "classification": "positive",
            "matched_filter_terms": filter_hits,
            "matched_sentiment_terms": pos_hits,
        }

    return {
        "filter_matched": True,
        "classification": "skipped",
        "matched_filter_terms": filter_hits,
        "matched_sentiment_terms": [],
    }


def extract_website(text: str) -> str | None:
    m = _URL_RE.search(text or "")
    if not m:
        return None
    url = m.group(0).rstrip(").,;]")
    return url[:512]


def extract_contact_name(from_name: str, from_email: str) -> str:
    name = (from_name or "").strip().strip('"')
    if name and "@" not in name:
        return name[:255]
    local = (from_email or "").split("@", 1)[0].replace(".", " ").replace("_", " ").strip()
    return (local or "Unknown").title()[:255]


def _gmail_search_query(filter_keywords: list[str]) -> str:
    base = "in:inbox newer_than:1d"
    if not filter_keywords:
        return base
    # Gmail OR query for subject/body — quote multi-word terms
    parts: list[str] = []
    for kw in filter_keywords[:20]:
        term = kw.strip()
        if not term:
            continue
        if " " in term:
            parts.append(f'"{term}"')
        else:
            parts.append(term)
    if not parts:
        return base
    return f"{base} ({' OR '.join(parts)})"


def _already_processed(db: Session, account_email: str, message_id: str) -> bool:
    return (
        db.query(InboundEmailEvent.id)
        .filter(
            InboundEmailEvent.gmail_account_email == account_email,
            InboundEmailEvent.gmail_message_id == message_id,
        )
        .first()
        is not None
    )


def _record_event(
    db: Session,
    *,
    account_email: str,
    detail: dict,
    classification: str,
    filter_matched: bool,
    matched_filter_terms: list[str],
    matched_sentiment_terms: list[str],
    lead_id: int | None = None,
    error: str | None = None,
) -> InboundEmailEvent:
    event = InboundEmailEvent(
        gmail_account_email=account_email,
        gmail_message_id=detail.get("id") or "",
        thread_id=detail.get("thread_id") or None,
        from_email=detail.get("from_email") or None,
        from_name=detail.get("from_name") or None,
        subject=(detail.get("subject") or "")[:512] or None,
        filter_matched=filter_matched,
        classification=classification,
        matched_filter_terms=", ".join(matched_filter_terms) or None,
        matched_sentiment_terms=", ".join(matched_sentiment_terms) or None,
        lead_id=lead_id,
        raw_snippet=(detail.get("snippet") or "")[:1000] or None,
        error=(error or "")[:1000] or None,
    )
    db.add(event)
    db.commit()
    db.refresh(event)
    return event


def process_account(db: Session, account, rules: dict[str, Any]) -> dict[str, int]:
    stats = {
        "processed": 0,
        "created": 0,
        "skipped": 0,
        "negative": 0,
        "duplicate": 0,
        "errors": 0,
        "ignored": 0,
    }
    if not account_has_readonly(account):
        stats["errors"] += 1
        return stats

    query = _gmail_search_query(rules["filter_keywords"])
    account_email = account.email
    # End DB txn before slow Gmail HTTP (avoids idle-in-transaction timeouts).
    db.commit()
    try:
        stubs = list_recent_messages(db, account, query=query, max_results=50)
    except GmailAuthError:
        stats["errors"] += 1
        return stats

    for stub in stubs:
        mid = stub.get("id") or ""
        if not mid or _already_processed(db, account_email, mid):
            continue
        stats["processed"] += 1
        db.commit()
        try:
            detail = get_message_detail(db, account, mid)
        except GmailAuthError as exc:
            stats["errors"] += 1
            _record_event(
                db,
                account_email=account_email,
                detail={"id": mid, "thread_id": stub.get("threadId"), "snippet": ""},
                classification="error",
                filter_matched=False,
                matched_filter_terms=[],
                matched_sentiment_terms=[],
                error=str(exc),
            )
            continue

        result = classify_message(
            subject=detail.get("subject") or "",
            body=detail.get("body") or "",
            filter_keywords=rules["filter_keywords"],
            positive_keywords=rules["positive_keywords"],
            negative_keywords=rules["negative_keywords"],
            from_email=detail.get("from_email") or "",
            from_name=detail.get("from_name") or "",
        )
        classification = result["classification"]

        if classification == "ignored":
            stats["ignored"] += 1
            _record_event(
                db,
                account_email=account_email,
                detail=detail,
                classification="ignored",
                filter_matched=False,
                matched_filter_terms=result["matched_filter_terms"],
                matched_sentiment_terms=[],
            )
            continue

        if classification == "negative":
            stats["negative"] += 1
            _record_event(
                db,
                account_email=account_email,
                detail=detail,
                classification="negative",
                filter_matched=True,
                matched_filter_terms=result["matched_filter_terms"],
                matched_sentiment_terms=result["matched_sentiment_terms"],
            )
            continue

        if classification == "skipped":
            stats["skipped"] += 1
            _record_event(
                db,
                account_email=account_email,
                detail=detail,
                classification="skipped",
                filter_matched=True,
                matched_filter_terms=result["matched_filter_terms"],
                matched_sentiment_terms=[],
            )
            continue

        # positive
        from_email = (detail.get("from_email") or "").strip().lower()
        if not from_email:
            stats["errors"] += 1
            _record_event(
                db,
                account_email=account_email,
                detail=detail,
                classification="error",
                filter_matched=True,
                matched_filter_terms=result["matched_filter_terms"],
                matched_sentiment_terms=result["matched_sentiment_terms"],
                error="Missing from email",
            )
            continue

        exists = (
            db.query(Lead.id)
            .filter(Lead.client_email.ilike(from_email))
            .first()
        )
        if exists:
            stats["duplicate"] += 1
            _record_event(
                db,
                account_email=account_email,
                detail=detail,
                classification="duplicate",
                filter_matched=True,
                matched_filter_terms=result["matched_filter_terms"],
                matched_sentiment_terms=result["matched_sentiment_terms"],
                lead_id=exists[0],
            )
            continue

        body = detail.get("body") or ""
        subject = detail.get("subject") or ""
        snippet = (detail.get("snippet") or body[:300]).strip()
        notes = (
            f"Inbound email\nSubject: {subject}\n"
            f"{snippet}\n"
            f"source: inbound email {mid}"
        )[:4000]
        try:
            lead = create_lead(
                db,
                LeadCreate(
                    contact_name=extract_contact_name(
                        detail.get("from_name") or "", from_email
                    ),
                    client_email=from_email,
                    our_email=account_email,
                    website=extract_website(body),
                    date_added=date.today(),
                    stage="Positive lead",
                    notes=notes,
                    deal_value=None,
                    call_scheduled=False,
                ),
            )
        except Exception as exc:  # noqa: BLE001
            stats["errors"] += 1
            _record_event(
                db,
                account_email=account_email,
                detail=detail,
                classification="error",
                filter_matched=True,
                matched_filter_terms=result["matched_filter_terms"],
                matched_sentiment_terms=result["matched_sentiment_terms"],
                error=str(exc)[:500],
            )
            continue

        stats["created"] += 1
        _record_event(
            db,
            account_email=account_email,
            detail=detail,
            classification="positive",
            filter_matched=True,
            matched_filter_terms=result["matched_filter_terms"],
            matched_sentiment_terms=result["matched_sentiment_terms"],
            lead_id=lead.id,
        )

    return stats


def run_inbound_poll(db: Session, *, force: bool = False) -> dict[str, Any]:
    """Process all connected accounts. force=True runs even if rules.enabled is False."""
    global _last_run_at, _last_run_summary

    row = get_or_create_rules(db)
    rules = rules_to_dict(row)
    if not force and not rules["enabled"]:
        summary = "Inbound mail automation is disabled."
        _last_run_at = datetime.now(timezone.utc)
        _last_run_summary = summary
        return {
            "processed": 0,
            "created": 0,
            "skipped": 0,
            "negative": 0,
            "duplicate": 0,
            "errors": 0,
            "message": summary,
        }

    totals = {
        "processed": 0,
        "created": 0,
        "skipped": 0,
        "negative": 0,
        "duplicate": 0,
        "errors": 0,
        "ignored": 0,
    }
    accounts = list_accounts(db)
    if not accounts:
        summary = "No Gmail accounts connected."
        _last_run_at = datetime.now(timezone.utc)
        _last_run_summary = summary
        return {**{k: 0 for k in ("processed", "created", "skipped", "negative", "duplicate", "errors")}, "message": summary}

    for account in accounts:
        part = process_account(db, account, rules)
        for k, v in part.items():
            totals[k] = totals.get(k, 0) + v

    summary = (
        f"processed={totals['processed']} created={totals['created']} "
        f"negative={totals['negative']} skipped={totals['skipped']} "
        f"duplicate={totals['duplicate']} errors={totals['errors']}"
    )
    _last_run_at = datetime.now(timezone.utc)
    _last_run_summary = summary
    return {
        "processed": totals["processed"],
        "created": totals["created"],
        "skipped": totals["skipped"],
        "negative": totals["negative"],
        "duplicate": totals["duplicate"],
        "errors": totals["errors"],
        "message": summary,
    }


def get_poll_status(db: Session) -> dict[str, Any]:
    row = get_or_create_rules(db)
    missing = [a.email for a in list_accounts(db) if not account_has_readonly(a)]
    return {
        "enabled": bool(row.enabled),
        "poll_interval_seconds": int(row.poll_interval_seconds or 120),
        "last_run_at": _last_run_at,
        "last_run_summary": _last_run_summary,
        "accounts_missing_readonly": missing,
        "oauth_configured": settings.gmail_oauth_configured,
        "ai_classify_enabled": ai_classify_enabled(),
    }


def list_events(db: Session, *, limit: int = 50) -> list[InboundEmailEvent]:
    return (
        db.query(InboundEmailEvent)
        .order_by(InboundEmailEvent.processed_at.desc())
        .limit(limit)
        .all()
    )


def run_live_self_test(db: Session, *, wait_seconds: float = 8.0) -> dict[str, Any]:
    """Send 2 self-emails (spam + positive) via SMTP, then poll inbox via Gmail API."""
    import time

    if not settings.smtp_configured:
        raise GmailAuthError(
            "SMTP is required for live self-test send. Configure SMTP_* in .env "
            "(Gmail API is still used to read the inbox)."
        )

    account = get_default_account(db)
    if not account:
        raise GmailAuthError("No Gmail account connected.")
    if not account_has_readonly(account):
        raise GmailAuthError(
            f"{account.email} needs gmail.readonly — reconnect before live test."
        )

    to_email = account.email
    from_email = (settings.smtp_from or settings.smtp_user or to_email).strip()
    # Release DB before SMTP + Gmail I/O
    db.commit()

    stamp = datetime.now(timezone.utc).strftime("%H%M%S")
    samples = [
        {
            "kind": "spam",
            "subject": f"Celine, pick your brain? | QRV{stamp[-4:]}7 azure.crystal.nature",
            "body": "Automated inbound filter live test (spam pattern). Please ignore.",
        },
        {
            "kind": "positive",
            "subject": f"[Pipeline live test {stamp}] Re: proposal",
            "body": (
                "Yes I'm interested — let's talk and please share pricing for a demo.\n\n"
                "Automated inbound filter live test (positive). Please ignore."
            ),
        },
    ]

    sent: list[dict[str, Any]] = []
    for sample in samples:
        msg = EmailMessage()
        msg["To"] = to_email
        msg["From"] = from_email
        msg["Subject"] = sample["subject"]
        msg.set_content(sample["body"])
        _smtp_send(msg)
        sent.append(
            {
                "kind": sample["kind"],
                "subject": sample["subject"],
                "gmail_message_id": None,
                "to": to_email,
                "via": "smtp",
            }
        )

    time.sleep(max(2.0, float(wait_seconds)))
    poll = run_inbound_poll(db, force=True)
    events = list_events(db, limit=10)
    return {
        "sent": sent,
        "poll": poll,
        "events": events,
        "message": (
            f"Sent {len(sent)} test emails to {to_email} via SMTP, then polled. "
            f"{poll.get('message') or ''}"
        ).strip(),
    }
