# DYNBOT APP 
import os
import re
import uuid
import logging
import json
import sys
import random
import pprint

from flask import Flask, request, jsonify, render_template, session
from sqlalchemy.sql import func

from openai import OpenAI
from rag_service import generate_rag_addendum
from db import SessionLocal, init_db
from utils.prompt_manager import build_messages_payload
from models import (
    Session as ChatSession, User, Case, Message, Document, Topic
)

from time import perf_counter
from contextlib import contextmanager

from classifiers import is_pm_relevant
from responders import generic_pm_reply

@contextmanager
def timed(label, bag):
    t0 = perf_counter()
    try:
        yield
    finally:
        bag[label] = int((perf_counter() - t0) * 1000)  # ms

print("📍 being here 1", flush=True)

# --- DIRECT KEY USAGE (no .env dependency required) ---
FALLBACK_OPENAI_KEY = "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"  # <-- your temp key
def _resolve_api_key():
    # If you still export OPENAI_API_KEY in the environment, we'll prefer it.
    # Otherwise we fall back to the hardcoded testing key above.
    k = (os.getenv("OPENAI_API_KEY") or FALLBACK_OPENAI_KEY or "").strip()
    if not k:
        raise RuntimeError("No API key available (OPENAI_API_KEY and FALLBACK_OPENAI_KEY are empty).")
    return k

_client = None
def get_openai_client() -> OpenAI:
    global _client
    if _client is None:
        api_key = _resolve_api_key()
        logging.getLogger().info("🔑 OPENAI_API_KEY ends with: ...%s", api_key[-4:])
        _client = OpenAI(api_key=api_key)
    return _client

print(f"🗄 DB user: {os.getenv('DB_USER')}")
for var in ("HTTP_PROXY","HTTPS_PROXY","ALL_PROXY"):
    if os.getenv(var):
        logging.getLogger().warning("Proxy var %s is set; may cause 401s: %s", var, os.getenv(var))

app = Flask(__name__)
init_db()

# @app.route("/")
# def hello():
#    return "✅ Dynbot backend is running."

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/init-session", methods=["POST"])
def init_session():
    db = SessionLocal()
    data = request.get_json()

    tenant_id = data.get("tenant_id", 1)
    user_id = data.get("user_id", 1)
    session_token = data.get("session_token", str(uuid.uuid4()))

    # Ensure user exists
    user = db.query(User).filter_by(id=user_id).first()
    if not user:
        user = User(id=user_id, tenant_id=tenant_id, username="Default User", email="default@example.com")
        db.add(user)
        db.commit()

    # Always create a new session
    new_session = ChatSession(
        tenant_id=tenant_id,
        user_id=user_id,
        session_token=session_token,
    )
    db.add(new_session)
    db.commit()
    db.refresh(new_session)

    # Create a case before closing the session
    case = Case(
        tenant_id=tenant_id,
        user_id=user_id,
        session_id=new_session.id
    )
    db.add(case)
    db.commit()
    db.refresh(case)

    # ✅ Access attributes before db.close()
    response = {
        "session_id": new_session.id,
        "session_token": session_token,
        "case_id": case.id
    }

    db.close()
    return jsonify(response)


@app.route("/cases", methods=["POST"])
def create_case():
    db = SessionLocal()
    tenant_id = request.json.get("tenant_id", 1)
    new = Case(tenant_id=tenant_id, title=request.json.get("title",""))
    db.add(new); db.commit(); db.refresh(new)
    db.close()
    return jsonify({"case_id": new.id}), 201

@app.route("/cases/<int:case_id>/history", methods=["GET"])
def get_history(case_id):
    db = SessionLocal()
    msgs = db.query(Message).filter_by(case_id=case_id).order_by(Message.timestamp).all()
    # If you later add FollowupQuestion to models, import it and use it here.
    db.close()
    return jsonify({
        "messages": [{"role":m.role,"content":m.content,"timestamp":m.timestamp.isoformat()} for m in msgs]
    })


def get_initial_choices_from_db(db, tenant_id: int, n: int = 3):
    """
    Return a list of 'question' strings derived from Topic rows (randomized),
    with {topic} substituted into the question field (same as /topics/random).
    """
    rows = (
        db.query(Topic)
          .filter_by(tenant_id=tenant_id)
          .order_by(func.random())
          .limit(n)
          .all()
    )
    out = []
    for t in rows:
        q = (t.question or "").replace("{topic}", t.topic or "")
        if q.strip():
            out.append(q.strip())
    return out


@app.route("/cases/<int:case_id>/messages", methods=["POST"])
def post_message(case_id):
    mode = request.args.get("mode", "primary")
    db = SessionLocal()
    timings = {}
    t_all = perf_counter()
    try:
        data = request.get_json() or {}
        user_text = (data.get("content") or "").strip()
        button_choice = data.get("button_choice", "")
        tenant_id = data.get("tenant_id", 1)

        if not user_text or not tenant_id:
            return jsonify({"error": "bad_request", "detail": "Missing 'content' or 'tenant_id'"}), 400

        # -------------------------------
        # Fast path: RAG-only endpoint
        # -------------------------------
        if mode == "rag":
            with timed("rag_addendum", timings):
                try:
                    rag_res = generate_rag_addendum(user_text, tenant_id=tenant_id, top_k=3)
                    rag_addendum = (rag_res.get("addendum") or "").strip()
                except Exception:
                    rag_addendum = ""
                    timings["rag_error"] = 1
            resp = jsonify({"reply": rag_addendum, "timings": timings})
            total = int((perf_counter() - t_all) * 1000)
            resp.headers["Server-Timing"] = f'rag_addendum;dur={timings.get("rag_addendum",0)}, total;dur={total}'
            return resp, 200

        # ===========================================================
        # PM RELEVANCE GATE — run BEFORE storing messages in the DB
        # ===========================================================
        with timed("pm_classify", timings):
            pm_ok = is_pm_relevant(user_text)

        if not pm_ok:
            # Out-of-scope branch:
            #  - return a short generic (non-RAG) model reply
            #  - DO NOT show rag-box
            #  - surface 3 initial buttons from DB (not static)
            with timed("generic_reply", timings):
                ai_text = generic_pm_reply(user_text)

            initial_choices = get_initial_choices_from_db(db, tenant_id=tenant_id, n=3)

            # NOTE: We intentionally do NOT store this turn in the DB,
            # so it doesn't advance your iteration/summary logic.

            payload = {
                "reply": ai_text,
                "followups": [],           # no followups in this branch
                "choices": [],             # no in-text **choices** either
                "is_summary": False,       # never a summary turn here
                "initialChoices": initial_choices,  # <-- for frontend to render the 3 buttons
                "show_rag": False,         # <-- signal to hide rag-box
                "pm_relevant": False,
                "timings": timings
            }
            resp = jsonify(payload)
            total = int((perf_counter() - t_all) * 1000)
            server_timing = ", ".join([f"{k};dur={v}" for k, v in timings.items()])
            resp.headers["Server-Timing"] = f'{server_timing}, total;dur={total}' if server_timing else f'total;dur={total}'
            return resp, 200

        # ===============================================
        # PM-relevant path (original flow, mostly intact)
        # Now we do store user/assistant messages in DB.
        # ===============================================
        with timed("db_store_user", timings):
            user_msg = Message(case_id=case_id, role="user", content=user_text, button_choice=button_choice)
            db.add(user_msg); db.commit(); db.refresh(user_msg)

        with timed("build_payload", timings):
            messages_payload = build_messages_payload(db, case_id, tenant_id)

        openai_client = get_openai_client()
        with timed("openai", timings):
            resp = openai_client.chat.completions.create(
                model="gpt-4o-mini",
                temperature=1.2,
                messages=messages_payload,
                tools=[{
                    "type": "function",
                    "function": {
                        "name": "extract_followups",
                        "description": "Suggest 6–8 follow-up questions based on the assistant's reply.",
                        "parameters": {"type": "object", "properties": {"questions": {"type":"array","items":{"type":"string"}}}, "required": ["questions"]}
                    }
                }],
                tool_choice="auto"
            )

        choice = resp.choices[0]
        msg = getattr(choice, "message", None)
        finish = getattr(choice, "finish_reason", None)

        if finish == "tool_calls" and msg and getattr(msg, "tool_calls", None):
            tool_call = msg.tool_calls[0]
            args = json.loads(getattr(tool_call.function, "arguments", "{}") or "{}")
            followups = args.get("questions", [])
            ai_text = msg.content or "Vielen Dank für Ihre Auswahl. Hier sind mögliche nächste Schritte oder Zusammenfassungen."
        else:
            followups = []
            ai_text = (msg.content if msg else "") or "(no text reply provided)"

        # keep your 3-item truncation logic
        ai_text = truncate_numbered_blocks(ai_text, max_items=3)

        # extract bold **choices** and limit to 3 (unless summary)
        choices = re.findall(r"\*\*(.*?)\*\*", ai_text)

        past_messages = db.query(Message).filter_by(case_id=case_id).order_by(Message.timestamp).all()
        turn = len([m for m in past_messages if m.role == "user"])
        is_summary = (turn == 3)

        if is_summary:
            with timed("summary_prep", timings):
                intro_doc = db.query(Document).filter_by(tenant_id=tenant_id, title="summary_intro_1").first()
                quotes = db.query(Document).filter_by(tenant_id=tenant_id, title="quote").all()
                intro_text = intro_doc.content if intro_doc else ""
                quote_text = random.choice(quotes).content if quotes else ""
                button_choices = [m.button_choice for m in past_messages if m.role == "user"]
                intro_filled = (intro_text or "").format(
                    iteration1=(button_choices[0] if len(button_choices) > 0 else ""),
                    iteration2=(button_choices[1] if len(button_choices) > 1 else ""),
                    iteration3=(button_choices[2] if len(button_choices) > 2 else "")
                )
                ai_text = f"{intro_filled}\n\n{ai_text}\n\n {quote_text}"
            # no additional inline choices in summary view
            choices = []
        else:
            if len(choices) > 3:
                choices = random.sample(choices, 3)

        with timed("db_store_ai", timings):
            ai_msg = Message(case_id=case_id, role="assistant", content=ai_text)
            db.add(ai_msg); db.commit(); db.refresh(ai_msg)

        payload = {
            "reply": ai_text,
            "followups": followups,
            "choices": choices,
            "is_summary": is_summary,
            "show_rag": True,         # signal: rag-box can be shown on PM path
            "pm_relevant": True,
            "timings": timings
        }
        resp = jsonify(payload)
        total = int((perf_counter() - t_all) * 1000)
        server_timing = ", ".join([f"{k};dur={v}" for k, v in timings.items()])
        resp.headers["Server-Timing"] = f'{server_timing}, total;dur={total}' if server_timing else f'total;dur={total}'
        return resp, 200

    except Exception as e:
        return jsonify({"error": "server_error", "detail": str(e)}), 500
    finally:
        db.close()



def truncate_numbered_blocks(text: str, max_items: int = 3) -> str:
    import re
    # Suche Start der nummerierten Liste
    m = re.search(r'(?m)^\s*\d+\.\s+', text)
    if not m:
        return text

    head = text[:m.start()].rstrip("\n")
    body = text[m.start():]

    # Blöcke: "N. ..." bis vor die nächste Nummer oder Textende
    blocks = re.findall(r'(?ms)^\s*(\d+)\.\s+(.*?)(?=^\s*\d+\.|\Z)', body)
    if not blocks:
        return text

    # Nimm die ersten max_items und baue sie wieder zusammen
    out = []
    for idx, (_num, content) in enumerate(blocks[:max_items], start=1):
        out.append(f"{idx}. {content.strip()}")   # optional: Nummern neu 1..N

    return (head + "\n\n" if head else "") + "\n\n".join(out)

    


@app.route("/tenants", methods=["POST"])
def create_tenant():
    db = SessionLocal()
    name = request.json["name"]
    t = models.Tenant(name=name)
    db.add(t); db.commit(); db.refresh(t)
    db.close()
    return jsonify({"tenant_id": t.id}), 201


@app.route("/topics/random", methods=["GET"])
def get_random_topics():
    db = SessionLocal()
    tenant_id = 1  # or dynamic if implemented
    raw_topics = db.query(Topic)\
        .filter_by(tenant_id=tenant_id)\
        .order_by(func.random())\
        .limit(3)\
        .all()

    # Replace {topic} in question field
    results = []
    for t in raw_topics:
        filled_question = t.question.replace("{topic}", t.topic)
        results.append({
            "topic": t.topic,
            "question": filled_question
        })

    db.close()
    return jsonify(results)    


if __name__ == '__main__':
    app.run(
        host='0.0.0.0',
        port=5002,
        ssl_context=(
            '/var/www/html/decompression/certs/fullchain.pem',
            '/var/www/html/decompression/certs/privkey.pem'
        ),
        debug=True
    )

