"""
scripts/daily_summary.py — Tages-Zusammenfassung für E-Mail
============================================================
Gibt eine HTML-Zusammenfassung aller Trades des gestrigen Tages aus.
Aufruf: python daily_summary.py [YYYY-MM-DD]
"""

import sys
import sqlite3
import datetime
from pathlib import Path

DB_PATH = Path(__file__).parent.parent / "oil_widget_history.db"

def run(target_date: datetime.date) -> str:
    ts_start = int(datetime.datetime(target_date.year, target_date.month, target_date.day, 0, 0, 0).timestamp())
    ts_end   = ts_start + 86400

    conn = sqlite3.connect(DB_PATH)
    conn.row_factory = sqlite3.Row
    c = conn.cursor()

    c.execute("""
        SELECT setup, direction, pnl, closed_by, exit_time, entry_price, exit_price
          FROM trades
         WHERE exit_time >= ? AND exit_time < ? AND exit_time IS NOT NULL
         ORDER BY exit_time ASC
    """, (ts_start, ts_end))
    trades = [dict(r) for r in c.fetchall()]
    conn.close()

    if not trades:
        return f"<p>Keine Trades am {target_date.strftime('%d.%m.%Y')}.</p>"

    total   = len(trades)
    wins    = sum(1 for t in trades if (t["pnl"] or 0) > 0)
    pnl_sum = sum(t["pnl"] or 0 for t in trades)
    wr      = wins / total * 100

    # Pro Setup
    setups: dict[str, dict] = {}
    for t in trades:
        s = t["setup"] or "NO_SETUP"
        if s not in setups:
            setups[s] = {"n": 0, "wins": 0, "pnl": 0.0}
        setups[s]["n"]    += 1
        setups[s]["wins"] += 1 if (t["pnl"] or 0) > 0 else 0
        setups[s]["pnl"]  += t["pnl"] or 0

    best  = max(trades, key=lambda t: t["pnl"] or 0)
    worst = min(trades, key=lambda t: t["pnl"] or 0)

    def fmt_pnl(v):
        sign = "+" if v >= 0 else ""
        return f"{sign}{v:.2f}€"

    def row_color(pnl):
        if pnl > 0:   return "#1a4731"
        if pnl < 0:   return "#3d1a1a"
        return "#1a1e24"

    # ── HTML ──────────────────────────────────────────────────────────────────
    header_color = "#1f6feb" if pnl_sum >= 0 else "#b02020"
    pnl_color    = "#3fb950" if pnl_sum >= 0 else "#f85149"

    rows = ""
    for s, d in sorted(setups.items(), key=lambda x: -x[1]["pnl"]):
        wr_s = d["wins"] / d["n"] * 100
        bg   = row_color(d["pnl"])
        rows += f"""
        <tr style="background:{bg};">
          <td style="padding:6px 12px;font-family:monospace;">{s}</td>
          <td style="padding:6px 12px;text-align:center;">{d['n']}</td>
          <td style="padding:6px 12px;text-align:center;">{wr_s:.0f}%</td>
          <td style="padding:6px 12px;text-align:right;font-weight:bold;">{fmt_pnl(d['pnl'])}</td>
        </tr>"""

    import time as _time
    best_time  = _time.strftime("%H:%M", _time.localtime(best["exit_time"]))
    worst_time = _time.strftime("%H:%M", _time.localtime(worst["exit_time"]))

    html = f"""
<html><body style="background:#0d1117;color:#e6edf3;font-family:sans-serif;padding:24px;">
  <h2 style="color:{header_color};margin-bottom:4px;">
    Oil Trading &mdash; {target_date.strftime('%d.%m.%Y')}
  </h2>

  <table style="border-collapse:collapse;margin:16px 0;width:100%;max-width:400px;">
    <tr>
      <td style="padding:8px 16px;background:#161b22;color:#8b949e;">Trades</td>
      <td style="padding:8px 16px;background:#161b22;font-weight:bold;">{total}</td>
    </tr>
    <tr>
      <td style="padding:8px 16px;background:#0d1117;color:#8b949e;">Win Rate</td>
      <td style="padding:8px 16px;background:#0d1117;font-weight:bold;">{wr:.0f}%  ({wins}/{total})</td>
    </tr>
    <tr>
      <td style="padding:8px 16px;background:#161b22;color:#8b949e;">PnL</td>
      <td style="padding:8px 16px;background:#161b22;font-weight:bold;color:{pnl_color};font-size:18px;">{fmt_pnl(pnl_sum)}</td>
    </tr>
  </table>

  <h3 style="color:#8b949e;margin-bottom:8px;">Pro Setup</h3>
  <table style="border-collapse:collapse;width:100%;max-width:500px;">
    <tr style="background:#161b22;color:#8b949e;">
      <th style="padding:6px 12px;text-align:left;">Setup</th>
      <th style="padding:6px 12px;">N</th>
      <th style="padding:6px 12px;">WR</th>
      <th style="padding:6px 12px;text-align:right;">PnL</th>
    </tr>
    {rows}
  </table>

  <table style="border-collapse:collapse;margin-top:16px;width:100%;max-width:500px;">
    <tr>
      <td style="padding:6px 12px;color:#8b949e;">Bester Trade</td>
      <td style="padding:6px 12px;color:#3fb950;font-weight:bold;">{fmt_pnl(best['pnl'])}</td>
      <td style="padding:6px 12px;color:#8b949e;font-family:monospace;">{best.get('setup','—')}  {best_time}</td>
    </tr>
    <tr>
      <td style="padding:6px 12px;color:#8b949e;">Schlechtester</td>
      <td style="padding:6px 12px;color:#f85149;font-weight:bold;">{fmt_pnl(worst['pnl'])}</td>
      <td style="padding:6px 12px;color:#8b949e;font-family:monospace;">{worst.get('setup','—')}  {worst_time}</td>
    </tr>
  </table>
</body></html>
"""
    return html


if __name__ == "__main__":
    if len(sys.argv) > 1:
        target = datetime.date.fromisoformat(sys.argv[1])
    else:
        target = datetime.date.today() - datetime.timedelta(days=1)

    sys.stdout.reconfigure(encoding="utf-8")
    print(run(target))
