from __future__ import annotations

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

from app.config import settings
from app.db import execute, fetch_all, fetch_one
from app.services import ollama


def _safe_count(table: str, where: str = "", params: tuple = ()) -> int:
    try:
        clause = f" WHERE {where}" if where else ""
        row = fetch_one(f"SELECT COUNT(*) AS c FROM {table}{clause}", params or None)
        return int(row["c"]) if row else 0
    except Exception:
        return 0


def _safe_sum(table: str, column: str, where: str = "", params: tuple = ()) -> float:
    try:
        clause = f" WHERE {where}" if where else ""
        row = fetch_one(f"SELECT COALESCE(SUM({column}), 0) AS total FROM {table}{clause}", params or None)
        return float(row["total"]) if row else 0.0
    except Exception:
        return 0.0


def serialize_stats(data: dict[str, Any]) -> dict[str, Any]:
    def _default(o: Any) -> Any:
        if hasattr(o, "isoformat"):
            return o.isoformat()
        if hasattr(o, "__float__"):
            try:
                return float(o)
            except (TypeError, ValueError):
                pass
        return str(o)

    return json.loads(json.dumps(data, default=_default))


def collect_briefing_data() -> dict[str, Any]:
    data: dict[str, Any] = {
        "date": date.today().isoformat(),
        "generated_at": datetime.now(timezone.utc).isoformat(),
    }
    data["clients"] = _safe_count("clients")
    data["deals"] = _safe_count("deals")
    data["products"] = _safe_count("products")
    data["suppliers"] = _safe_count("suppliers")
    data["pipeline_eur"] = _safe_sum("deals", "value", "stage NOT IN ('won', 'lost')")
    data["pending_approvals"] = _safe_count("agent_events", "status = 'needs_approval'")

    try:
        data["deals_by_stage"] = fetch_all(
            "SELECT stage, COUNT(*) AS cnt, COALESCE(SUM(value), 0) AS total FROM deals GROUP BY stage ORDER BY cnt DESC"
        )
    except Exception:
        data["deals_by_stage"] = []

    try:
        data["recent_clients"] = fetch_all(
            "SELECT name, stage, email, created_at FROM clients ORDER BY created_at DESC LIMIT 5"
        )
    except Exception:
        data["recent_clients"] = []

    try:
        data["recent_events"] = fetch_all(
            """SELECT agent_name, event_type, title, status, created_at
               FROM agent_events ORDER BY created_at DESC LIMIT 12"""
        )
    except Exception:
        data["recent_events"] = []

    try:
        data["pending_items"] = fetch_all(
            """SELECT agent_name, title, event_type, created_at
               FROM agent_events WHERE status = 'needs_approval'
               ORDER BY created_at DESC LIMIT 8"""
        )
    except Exception:
        data["pending_items"] = []

    try:
        row = fetch_one(
            """SELECT COUNT(*) AS docs, COALESCE(SUM(word_count), 0) AS words,
                      COALESCE(AVG(sentiment_compound), 0) AS avg_sentiment
               FROM document_analytics"""
        )
        data["nas_docs"] = int(row["docs"] or 0) if row else 0
        data["nas_words"] = int(row["words"] or 0) if row else 0
        data["nas_sentiment"] = round(float(row["avg_sentiment"] or 0), 3) if row else 0.0
    except Exception:
        data["nas_docs"] = data["nas_words"] = 0
        data["nas_sentiment"] = 0.0

    try:
        data["nas_files"] = fetch_all(
            """SELECT filename, doc_type, sentiment_label, word_count
               FROM document_analytics ORDER BY analyzed_at DESC LIMIT 8"""
        )
    except Exception:
        data["nas_files"] = []

    try:
        data["top_words"] = fetch_all(
            """SELECT lemma, SUM(count) AS total FROM document_word_counts
               WHERE NOT is_stopword GROUP BY lemma ORDER BY total DESC LIMIT 10"""
        )
    except Exception:
        data["top_words"] = []

    try:
        data["calendar_events"] = fetch_all(
            """SELECT ce.title, ce.starts_at, ce.ends_at, c.name AS client_name
               FROM calendar_events ce
               LEFT JOIN clients c ON c.id = ce.client_id
               WHERE ce.starts_at >= NOW() - INTERVAL '1 day'
                 AND ce.starts_at <= NOW() + INTERVAL '7 days'
               ORDER BY ce.starts_at ASC LIMIT 10"""
        )
    except Exception:
        data["calendar_events"] = []

    # Retail intelligence
    data["supermarkets"] = _safe_count("supermarkets")
    data["crm_partnerships"] = _safe_count("supermarkets", "partnership_status = 'active'")
    data["wholesalers"] = _safe_count("wholesalers")

    try:
        data["top_opportunities"] = fetch_all(
            """SELECT s.name, s.chain, s.city, ros.halal_opportunity_score
               FROM retail_opportunity_scores ros
               JOIN supermarkets s ON s.id = ros.supermarket_id
               ORDER BY ros.halal_opportunity_score DESC LIMIT 5"""
        )
    except Exception:
        data["top_opportunities"] = []

    try:
        data["milestones_pending"] = fetch_all(
            """SELECT sm.title, sm.milestone_type, sm.status, sm.target_date, sm.value_eur,
                      s.name AS store_name, s.chain, c.name AS client_name
               FROM sales_milestones sm
               LEFT JOIN supermarkets s ON s.id = sm.supermarket_id
               LEFT JOIN clients c ON c.id = sm.client_id
               WHERE sm.status IN ('pending', 'in_progress')
               ORDER BY sm.target_date ASC NULLS LAST, sm.created_at DESC LIMIT 8"""
        )
    except Exception:
        data["milestones_pending"] = []

    try:
        data["milestones_recent"] = fetch_all(
            """SELECT sm.title, sm.milestone_type, sm.status, sm.completed_at, sm.value_eur,
                      s.name AS store_name, s.chain
               FROM sales_milestones sm
               LEFT JOIN supermarkets s ON s.id = sm.supermarket_id
               ORDER BY sm.created_at DESC LIMIT 5"""
        )
    except Exception:
        data["milestones_recent"] = []

    try:
        data["rss_highlights"] = fetch_all(
            """SELECT i.title, i.link, i.summary, f.name AS feed_name, f.url AS feed_url
               FROM rss_items i JOIN rss_feeds f ON f.id = i.feed_id AND f.is_active = TRUE
               WHERE i.title ILIKE ANY (ARRAY['%kant%','%maaltijd%','%supermarkt%','%retail%','%halal%','%jumbo%','%meal%'])
               ORDER BY i.published_at DESC NULLS LAST LIMIT 6"""
        )
    except Exception:
        data["rss_highlights"] = []

    try:
        data["market_trends"] = fetch_all(
            "SELECT trend_name, description, opportunity_score FROM market_trends ORDER BY updated_at DESC LIMIT 4"
        )
    except Exception:
        data["market_trends"] = []

    return data


def build_template_report(data: dict[str, Any]) -> str:
    lines = [
        f"# Foodlinkk Dagrapport — {data['date']}",
        "",
        f"*Gegenereerd: {data['generated_at'][:19]} UTC · Model: {settings.OLLAMA_MODEL}*",
        "",
        "## KPI's",
        f"- **Klanten:** {data['clients']} · **Deals:** {data['deals']} · **Pipeline:** €{data['pipeline_eur']:,.0f}",
        f"- **Supermarkten in DB:** {data.get('supermarkets', 0)} · **CRM partnerships:** {data.get('crm_partnerships', 0)}",
        f"- **Groothandels:** {data.get('wholesalers', 0)} · **Goedkeuringen open:** {data['pending_approvals']}",
        "",
    ]

    if data.get("top_opportunities"):
        lines.extend(["## Top halal-markt kansen (Retail 360)"])
        for row in data["top_opportunities"]:
            score = round(float(row.get("halal_opportunity_score") or 0))
            lines.append(f"- **{row.get('chain')} · {row.get('name')}** ({row.get('city')}) — score {score}/100")
        lines.append("")

    if data.get("milestones_pending"):
        lines.extend(["## Sales milestones — open"])
        for row in data["milestones_pending"]:
            td = row.get("target_date")
            td_s = td.isoformat()[:10] if hasattr(td, "isoformat") else str(td or "—")[:10]
            lines.append(f"- [{td_s}] **{row.get('title')}** · {row.get('chain') or ''} {row.get('store_name') or ''} · €{row.get('value_eur') or '—'}")
        lines.append("")

    if data.get("rss_highlights"):
        lines.extend(["## Kant-en-klaar & supermarkt nieuws"])
        for row in data["rss_highlights"]:
            lines.append(f"- [{row.get('feed_name')}] {row.get('title')}")
        lines.append("")

    if data.get("market_trends"):
        lines.extend(["## Markt trends"])
        for row in data["market_trends"]:
            pct = round(float(row.get("opportunity_score") or 0) * 100)
            lines.append(f"- **{row.get('trend_name')}** ({pct}% kans) — {row.get('description') or ''}")
        lines.append("")

    lines.extend(["## Pipeline per stage"])
    for row in data.get("deals_by_stage") or []:
        lines.append(f"- **{row.get('stage')}:** {row.get('cnt')} deals · €{float(row.get('total') or 0):,.0f}")
    if not data.get("deals_by_stage"):
        lines.append("- Geen deals in database.")

    if data.get("calendar_events"):
        lines.extend(["", "## Agenda (7 dagen)"])
        for row in data["calendar_events"]:
            ts = row.get("starts_at")
            ts_s = ts.isoformat()[:16] if hasattr(ts, "isoformat") else str(ts)[:16]
            lines.append(f"- [{ts_s}] {row.get('title')} ({row.get('client_name') or '-'})")

    if data.get("pending_items"):
        lines.extend(["", "## ⚠️ Wacht op jouw goedkeuring"])
        for row in data["pending_items"]:
            lines.append(f"- {row.get('agent_name')}: {row.get('title')}")

    return "\n".join(lines)


async def _ai_executive_summary(data: dict[str, Any]) -> str:
    opp_lines = ""
    for row in data.get("top_opportunities") or []:
        opp_lines += f"- {row.get('chain')} {row.get('name')} ({row.get('city')}): score {round(float(row.get('halal_opportunity_score') or 0))}\n"

    ms_lines = ""
    for row in data.get("milestones_pending") or []:
        ms_lines += f"- {row.get('title')} ({row.get('chain') or 'CRM'}) deadline {row.get('target_date') or '?'}\n"

    prompt = (
        "Schrijf in het Nederlands (markdown) voor CEO Aïssa van Foodlinkk (halal kant-en-klaar maaltijden):\n\n"
        "## Samenvatting\n(5-7 zinnen: wat is vandaag belangrijk, pipeline, retail kansen, milestones)\n\n"
        "## Actiepunten vandaag — korte termijn\n(minimaal 5 concrete bullets met CRM/retail acties)\n\n"
        "## Lange termijn focus\n(3-5 bullets: groei supermarkt partnerships, halal markt, milestones komende weken)\n\n"
        f"Data vandaag ({data['date']}):\n"
        f"- Pipeline €{data['pipeline_eur']:,.0f}, {data['clients']} klanten, {data['deals']} deals\n"
        f"- {data.get('supermarkets',0)} supermarkten, {data.get('crm_partnerships',0)} actieve CRM partnerships\n"
        f"- {data['pending_approvals']} goedkeuringen open\n"
        f"Top kansen:\n{opp_lines or '- geen data'}\n"
        f"Milestones open:\n{ms_lines or '- geen milestones'}\n"
    )
    system = (
        "Je bent Herman, AI co-CEO van Foodlinkk. Schrijf warm, professioneel en actionable. "
        "Focus op halal kant-en-klaar retail groei in Nederland. Geen vage tekst — concrete namen en acties."
    )
    try:
        return await ollama.generate(prompt, system=system, timeout=120.0)
    except Exception:
        return ""


def _fallback_summary(data: dict[str, Any]) -> str:
    opp = data.get("top_opportunities") or []
    ms = data.get("milestones_pending") or []
    lines = [
        "## Samenvatting",
        f"Vandaag ({data['date']}) heb je **€{data['pipeline_eur']:,.0f}** in je pipeline en **{data.get('crm_partnerships',0)} actieve supermarkt-partnerships**. "
        f"In Retail 360 staan **{data.get('supermarkets',0)} filialen** met live CBS-data.",
    ]
    if opp:
        top = opp[0]
        lines.append(
            f"De grootste halal-kans is **{top.get('chain')} · {top.get('name')}** in {top.get('city')} "
            f"(score {round(float(top.get('halal_opportunity_score') or 0))}/100)."
        )
    lines.extend(["", "## Actiepunten vandaag — korte termijn"])
    actions = [
        "Open Retail 360 en benader top-3 halal-gap filialen via CRM koppeling",
        f"Behandel {data['pending_approvals']} openstaande agent-goedkeuringen",
        "Check Marketing Live Feed voor kant-en-klaar trends",
    ]
    if ms:
        actions.insert(0, f"Follow-up milestone: **{ms[0].get('title')}**")
    for a in actions[:6]:
        lines.append(f"- {a}")
    lines.extend(["", "## Lange termijn focus"])
    lines.extend([
        "- Schaal CRM partnerships van proposal naar actief in top-10 kans-filialen",
        "- Halal kant-en-klaar listing bij Jumbo/AH regio's met hoogste demografische vraag",
        "- Wekelijks milestones review in Retail 360 sales tab",
    ])
    return "\n".join(lines)


def _save_briefing(content: str, data: dict[str, Any]) -> None:
    safe = serialize_stats(data)
    metadata = {"stats": safe, "model": settings.OLLAMA_MODEL, "type": "daily_ceo_report"}
    try:
        execute(
            "INSERT INTO daily_briefings (content, generated_by, metadata) VALUES (%s, %s, %s::jsonb)",
            (content, "herman", json.dumps(metadata)),
        )
    except Exception:
        pass
    try:
        execute(
            """INSERT INTO agent_events (agent_name, agent_type, event_type, title, body, status, channel, metadata)
               VALUES (%s, %s, %s, %s, %s, %s, %s, %s::jsonb)""",
            (
                "herman", "herman_delegate", "briefing",
                f"CEO dagrapport {data['date']}", content[:2000],
                "completed", "dashboard", json.dumps({"stats": safe}),
            ),
        )
    except Exception:
        pass


async def generate_daily_briefing() -> tuple[str, dict[str, Any]]:
    data = collect_briefing_data()
    template = build_template_report(data)
    try:
        ai_part = await asyncio.wait_for(_ai_executive_summary(data), timeout=25.0)
    except (asyncio.TimeoutError, Exception):
        ai_part = ""

    if ai_part and len(ai_part.strip()) > 80:
        content = ai_part.strip() + "\n\n---\n\n" + template
    else:
        content = _fallback_summary(data) + "\n\n---\n\n" + template

    _save_briefing(content, data)
    return content, serialize_stats(data)
