from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.units import inch
B = "http://68.183.154.175"
ss = getSampleStyleSheet()
H1 = ss["Title"]; H2 = ParagraphStyle("h2", parent=ss["Heading2"], textColor=colors.HexColor("#1f4e79"), spaceBefore=10)
N = ParagraphStyle("n", parent=ss["BodyText"], fontSize=9, leading=12)
S = ParagraphStyle("s", parent=N, fontSize=8, leading=10)
def L(path, text=None):
    u = B + path
    return Paragraph(f'<link href="{u}" color="blue"><u>{text or u}</u></link>', S)
def table(rows, w=(2.0, 2.9, 2.6)):
    data = [[Paragraph("<b>Tool</b>", S), Paragraph("<b>Address (click)</b>", S), Paragraph("<b>What you use it for</b>", S)]]
    for name, path, what in rows:
        data.append([Paragraph(f"<b>{name}</b>", S), L(path) if path.startswith("/") else Paragraph(path, S), Paragraph(what, S)])
    t = Table(data, colWidths=[x*inch for x in w], repeatRows=1)
    t.setStyle(TableStyle([("GRID", (0,0), (-1,-1), 0.4, colors.grey), ("BACKGROUND", (0,0), (-1,0), colors.HexColor("#dde7f3")),
                           ("VALIGN", (0,0), (-1,-1), "TOP"), ("ROWBACKGROUNDS", (0,1), (-1,-1), [colors.white, colors.HexColor("#f6f8fb")])]))
    return t
st = []
st += [Paragraph("Trading Tools &amp; Addresses", H1),
       Paragraph("Everything built for you on the server 68.183.154.175 that you can open in a browser: live bot monitors, the Rule Lab, "
                 "day pages, reviews, knowledge documents, study reports and logs. Made 2026-11-03. All links are clickable.", N),
       Paragraph("<b>Passwords:</b> only the Rule Lab (and its book) asks for one: user <b>frank</b>, password in the server file "
                 "/root/lab_password.txt (not printed here). Everything else opens without a password.", N)]
st += [Paragraph("1. Live bots — watch them during the day", H2), table([
    ("Trade Bot Monitor", "/trade/report/", "Live trade bot (EV3): account, today's trades, decisions, charts, log tail."),
    ("Trade Bot positions", "/trade/report/positions.php", "Open positions of the trade bot with live price and P/L."),
    ("Star Bot Monitor", "/premarket/report/", "Live Star bot (bot buys, Star sells): account, trades, log."),
    ("Star Bot positions", "/premarket/report/positions.php", "Open positions of the Star bot."),
    ("Alpaca paper dashboard", "https://app.alpaca.markets/paper/dashboard/overview", "Broker's own view of both paper accounts (log in with your Alpaca login; switch account for Star)."),
])]
st += [Paragraph("2. Testing — Rule Lab (password)", H2), table([
    ("Rule Lab — build &amp; run rules", "/trade/reports/lab/", "Build entry + exit rules, pick period (10 years / 2 years / 6 or 3 months / custom), run backtest, see past runs, copy rules."),
    ("Rule Lab — a result", "/trade/reports/lab/result.php?id=JOB_ID", "Opened from the runs list: totals, halves, by-year table, exit reasons, winner/loser charts, 'load 5 more'."),
    ("Rule Lab — trade list", "/trade/reports/lab/trades.php?id=JOB_ID", "Every trade of a run: filter/sort, click any trade to draw its chart."),
    ("Rule Lab Book (PDF)", "/trade/reports/lab/book/rule_lab_book.pdf", "31-page guide: every entry and exit setting explained with real charts and value-by-value examples."),
])]
st += [Paragraph("3. Charts of past days and reviews", H2), table([
    ("Past-day pages", "/trade/reports/days/", "Each trading day: scanner stocks, 1-min chart with bot indicators per minute, bot buys/sells. Updated 16:40 ET."),
    ("Weekly reviews", "/trade/reports/weekly/", "Every Friday 18:10 ET: review of both bots and the recorders (e.g. 2026-10-30_weekly_review.html)."),
    ("All reports folder", "/trade/reports/", "Browse every report file and PDF (session summaries, studies, winner/loser books)."),
])]
st += [Paragraph("4. Knowledge documents — check before new ideas", H2), table([
    ("Fix list", "/trade/reports/fix_list.html", "Your running list of agreed fixes and their status."),
    ("Entry knowledge base", "/trade/reports/entry_knowledge.html", "Every entry test with results + DO-NOT-TRADE list."),
    ("Rules Book (PDF)", "/trade/reports/knowledge/rules_book.pdf", "All rules (live and candidates) with their test results; section 5 = EV3 live rules."),
    ("Entry situations S1-S28", "/trade/reports/entry_situations.html", "Your situation catalog ('not now' gates) and which are covered by v2."),
    ("Opening patterns (42)", "/trade/reports/opening_patterns.html", "Catalog of opening patterns used by the Lab's pattern trigger."),
    ("Original design doc", "server: /var/www/screener/trade/docs/breakout_bot_design_conversation.pdf", "(Not on the web.) The first design conversation of the trade bot."),
])]
st += [Paragraph("5. Loser / winner chart books from studies", H2), table([
    (n, f"/trade/reports/{d}/", w) for n, d, w in [
        ("x42 losers", "x42_losers", "Charts of losers of the #42 test (reference run of the Lab)."),
        ("x42 gate losers", "x42_gate_losers", "Same with the gate."),
        ("p41 / p42 losers", "p42_losers", "Big-dip V entry (#42) losers; also p41_losers, p42c_, p42d_, p42hl_, p42v_losers."),
        ("tr42 losers", "tr42_losers", "Trend-trade #42 losers; also tr42x_losers, tr42y_losers."),
        ("r6 losers", "r6_losers", "R6 setup losers."),
    ]])]
st += [Paragraph("6. Main study PDFs", H2), table([
    ("EV3 account sim 2 years", "/trade/reports/ev3/sim_2y.pdf", "Account simulation of the live EV3 setups 2024-26."),
    ("EV3 sample trades", "/trade/reports/ev3/ev3_samples_y1.pdf", "Example EV3 trades with charts."),
    ("Open-window library", "/trade/reports/open_window/open_window_report.pdf", "What is in the 2-year data library."),
    ("All tests summary 10/3", "/trade/reports/2026-10-03_all_tests_summary.pdf", "Summary of the September/October tests."),
    ("Entry rules v2", "/trade/reports/2026-09-29_entry_rules_v2.pdf", "The v2 entry rules (used by the Star bot's buys)."),
    ("Big winners report", "/trade/reports/2026-09-28_big_winners_report.pdf", "Traits of the big winners."),
    ("Issues &amp; fixes catalog", "/trade/reports/2026-09-28_issues_and_fixes_catalog.pdf", "Early issue list."),
    ("Practice charts", "/trade/reports/practice/", "Chart sets you marked (groups/G8, G10, G4 ...)."),
])]
st += [Paragraph("7. Logs", H2), table([
    ("Trade bot logs (web)", "/trade/logs/", "Daily log trade_bot_YYYY-MM-DD.log, e.g. /trade/logs/trade_bot_2026-11-03.log."),
    ("Star bot logs (web)", "/premarket/logs/", "Daily log star_bot_YYYY-MM-DD.log."),
    ("Bot decisions (server)", "/var/www/screener/trade/data/decisions/", "Every decision per minute (YYYY-MM-DD.jsonl; older than 7 days .jsonl.gz). Premarket same path."),
    ("Real fills (server)", "/var/www/screener/trade/data/trades/", "Trades rewritten with real broker fills each day 16:03 (reconcile_fills). Same in premarket/data/trades/."),
    ("Nightly jobs logs (server)", "/var/www/screener/trade/logs/ and reports/open_window/*.log", "reference_build, edgar_events, news_recorder, reconcile_fills, bars10y, flow_tick, pm_history ..."),
    ("Star exit daily check", "/var/www/screener/trade/reports/playbook/star_exit_daily.jsonl", "16:15 out-of-sample check of 'bot buys, Star sells'."),
])]
st += [Spacer(1, 6), Paragraph("Server paths (starting with /var/www) are opened over SSH/WinSCP to 68.183.154.175, or in your desktop copy under the same folders. "
       "<b>Note:</b> the bot log folders are readable on the web by anyone who knows the address (they hold no keys, but they show your trades). "
       "Ask Claude if you want them password-protected like the Rule Lab.", N)]
SimpleDocTemplate("/var/www/screener/trade/reports/my_trading_tools.pdf", pagesize=letter, leftMargin=0.5*inch, rightMargin=0.5*inch,
                  topMargin=0.5*inch, bottomMargin=0.5*inch, title="Trading Tools & Addresses").build(st)
print("ok")
