#!/usr/bin/env python3
"""Generate Balanced Breakfast pitch PDF."""

from fpdf import FPDF

# Colors (warm breakfast palette)
BG = (250, 248, 245)       # Warm cream background
TEXT = (61, 50, 37)         # Dark brown text
ACCENT = (232, 168, 65)    # Egg-yolk yellow accent
TOMATO = (201, 75, 75)     # Tomato red (secondary accent)
MUTED = (154, 139, 120)    # Muted brown text
WHITE = (255, 255, 255)
DARK = (50, 40, 30)        # Dark brown (title page bg)
GREEN = (107, 155, 90)     # Success green
SECTION_BG = (245, 240, 232)


class PitchPDF(FPDF):
    def header(self):
        pass

    def footer(self):
        if self.page_no() > 1:
            self.set_y(-15)
            self.set_font("Helvetica", "I", 8)
            self.set_text_color(*MUTED)
            self.cell(0, 10, f"Balanced Breakfast  |  Page {self.page_no()}", align="C")

    def section_title(self, title):
        self.set_font("Helvetica", "B", 16)
        self.set_text_color(*TEXT)
        self.cell(0, 10, title, new_x="LMARGIN", new_y="NEXT")
        # Yolk underline
        self.set_draw_color(*ACCENT)
        self.set_line_width(1.5)
        self.line(self.l_margin, self.get_y(), self.l_margin + 50, self.get_y())
        self.ln(6)

    def feature_block(self, icon, title, bullets):
        y_start = self.get_y()
        # Title
        self.set_font("Helvetica", "B", 12)
        self.set_text_color(*TEXT)
        self.cell(0, 7, f"{icon}  {title}", new_x="LMARGIN", new_y="NEXT")
        self.ln(1)
        # Bullets
        self.set_font("Helvetica", "", 9.5)
        self.set_text_color(*MUTED)
        for bullet in bullets:
            self.set_x(self.l_margin + 6)
            self.multi_cell(self.epw - 6, 4.5, f"- {bullet}", new_x="LMARGIN", new_y="NEXT")
        self.ln(3)


def build_pdf():
    pdf = PitchPDF()
    pdf.set_auto_page_break(auto=True, margin=20)
    pdf.set_margins(20, 15, 20)

    # =========================================================
    # PAGE 1 - Title / Hero
    # =========================================================
    pdf.add_page()

    # Background fill
    pdf.set_fill_color(*DARK)
    pdf.rect(0, 0, 210, 297, "F")

    # "BB" logo text
    pdf.set_y(60)
    pdf.set_font("Helvetica", "B", 72)
    pdf.set_text_color(*ACCENT)
    pdf.cell(0, 30, "BB", align="C", new_x="LMARGIN", new_y="NEXT")

    # App name
    pdf.set_font("Helvetica", "B", 32)
    pdf.set_text_color(*WHITE)
    pdf.cell(0, 16, "Balanced Breakfast", align="C", new_x="LMARGIN", new_y="NEXT")

    # Tagline
    pdf.ln(6)
    pdf.set_font("Helvetica", "", 14)
    pdf.set_text_color(200, 190, 175)
    pdf.cell(0, 8, "When it comes to media and news,", align="C", new_x="LMARGIN", new_y="NEXT")
    pdf.cell(0, 8, "it's good to be a picky eater.", align="C", new_x="LMARGIN", new_y="NEXT")

    # Divider
    pdf.ln(12)
    pdf.set_draw_color(*ACCENT)
    pdf.set_line_width(1)
    pdf.line(70, pdf.get_y(), 140, pdf.get_y())
    pdf.ln(12)

    # Value props
    pdf.set_font("Helvetica", "", 11)
    pdf.set_text_color(210, 200, 185)
    props = [
        "RSS, Hacker News, arXiv -- all in one unified timeline",
        "Extend with custom sources via Rhai plugins -- no recompilation",
        "Your data stays local -- no accounts, no cloud, no tracking",
        "Native performance -- built with Rust and Tauri, not Electron",
        "Free with no limits -- no feed caps, no premium tiers",
        "macOS, Windows, and Linux",
    ]
    for prop in props:
        pdf.cell(0, 7, prop, align="C", new_x="LMARGIN", new_y="NEXT")

    # Bottom
    pdf.set_y(255)
    pdf.set_font("Helvetica", "I", 10)
    pdf.set_text_color(140, 128, 110)
    pdf.cell(0, 6, "Alpha Release  --  March 2026", align="C", new_x="LMARGIN", new_y="NEXT")
    pdf.cell(0, 6, "Source-available  --  PolyForm Noncommercial 1.0.0", align="C", new_x="LMARGIN", new_y="NEXT")

    # =========================================================
    # PAGE 2 - Core Features
    # =========================================================
    pdf.add_page()

    pdf.section_title("Core Features")

    pdf.feature_block("FEEDS", "Feed Aggregation", [
        "Subscribe to any RSS or Atom feed URL",
        "Follow Hacker News stories: Top, New, Best, Ask HN, Show HN, Jobs",
        "Track arXiv papers by category (cs.AI, cs.LG, cs.CL, cs.CV, stat.ML)",
        "Unified timeline across all sources with per-source emoji indicators",
        "Auto-fetch in background every 15 minutes (configurable per plugin)",
        "Manual refresh with Cmd+R for immediate fetch",
    ])

    pdf.feature_block("READING", "Reading & Browsing", [
        "Three-panel layout: sources sidebar, items list, detail panel",
        "Item metadata: title, author, body preview, score, published date",
        "HTML content converted to readable text",
        "Open original URLs in your browser with o or Enter",
        "Keyboard-driven: j/k navigate, s star, r read/unread, / search",
    ])

    pdf.feature_block("ORGANIZE", "Organization", [
        "Mark items as read/unread, star/favorite for later",
        "Filter views: All items, Unread only, Starred only",
        "Per-source filtering with unread counts in sidebar",
        "Sort: Newest first, By score, Unread first, Starred first",
        "Client-side text search across title, body, and author",
        "Paginated loading (50 items at a time)",
    ])

    pdf.feature_block("MANAGE", "Feed Management", [
        "Add new feeds from any loaded plugin without restarting",
        "Delete individual feeds or all feeds from a source",
        "OPML import (Cmd+I) to migrate from other feed readers",
        "OPML export (Cmd+E) for backup or migration",
        "Automatic duplicate feed detection on import",
        "Toast notifications on fetch errors",
    ])

    # =========================================================
    # PAGE 3 - Plugin System & Data
    # =========================================================
    pdf.add_page()

    pdf.section_title("Plugin System")

    pdf.set_font("Helvetica", "", 10.5)
    pdf.set_text_color(*TEXT)
    pdf.multi_cell(0, 5.5,
        "Balanced Breakfast uses a Rhai scripting engine for feed source plugins. "
        "Drop a .rhai file into the plugins directory and it loads on next launch -- "
        "no compilation, no app restart required."
    )
    pdf.ln(4)

    pdf.feature_block("PLUGINS", "How It Works", [
        "Three built-in plugins: RSS/Atom, Hacker News, arXiv",
        "Each plugin defines its own config schema (text, URL, number, toggle, select)",
        "Plugin capabilities: pagination, search, date filtering, custom fetch intervals",
        "Host functions for HTTP requests, feed parsing, string utilities, date/time",
        "100,000 max operations per execution (security sandbox)",
        "Config validation ensures correct inputs before fetching",
    ])

    pdf.feature_block("EXTEND", "Write Your Own", [
        "Four required functions: id(), name(), config_schema(), fetch()",
        "Optional capabilities() to declare what your plugin supports",
        "Use http_get() and parse_feed() for RSS sources",
        "Use http_get_json() for JSON API sources",
        "Full documentation and minimal examples included",
    ])

    pdf.ln(4)
    pdf.section_title("Keyboard Shortcuts")

    pdf.set_font("Helvetica", "", 10)
    pdf.set_text_color(*MUTED)
    shortcuts = [
        "j / k  --  Navigate items (vim-style)",
        "s  --  Star / unstar item",
        "r  --  Toggle read / unread",
        "/  --  Focus search",
        "o or Enter  --  Open original URL in browser",
        "Escape  --  Close detail panel",
        "Cmd+R  --  Refresh all feeds",
        "Cmd+N  --  Add new feed",
        "Cmd+I  --  Import OPML",
        "Cmd+E  --  Export OPML",
        "Cmd+1/2/3  --  View all / unread / starred",
    ]
    for s in shortcuts:
        pdf.cell(0, 5.5, f"  {s}", new_x="LMARGIN", new_y="NEXT")

    # =========================================================
    # PAGE 4 - Why BB + Comparison
    # =========================================================
    pdf.add_page()

    pdf.section_title("Why Balanced Breakfast?")

    pdf.set_font("Helvetica", "", 10.5)
    pdf.set_text_color(*TEXT)
    pdf.multi_cell(0, 5.5,
        "Most feed readers are cloud-only, subscription-based, and limited to RSS. "
        "Balanced Breakfast is a native desktop app that aggregates RSS, Hacker News, "
        "arXiv, and any source you can script -- all offline, all local, all yours."
    )
    pdf.ln(6)

    # Comparison table
    pdf.set_font("Helvetica", "B", 11)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "How it compares:", new_x="LMARGIN", new_y="NEXT")
    pdf.ln(2)

    headers = ["", "BB", "Feedly", "NNW", "Reeder", "Miniflux", "Newsboat"]
    col_w = [38, 22, 22, 22, 22, 22, 22]

    # Header row
    pdf.set_font("Helvetica", "B", 8)
    pdf.set_fill_color(*DARK)
    pdf.set_text_color(*WHITE)
    for i, h in enumerate(headers):
        pdf.cell(col_w[i], 7, h, border=1, fill=True, align="C")
    pdf.ln()

    rows = [
        ["Free (no limits)", "Yes", "100", "Yes", "10",  "Self", "Yes"],
        ["Native desktop",   "Yes", "No",  "Mac", "Mac", "No",   "Term"],
        ["Plugin system",    "Yes", "No",  "No",  "No",  "No",   "Macros"],
        ["HN first-class",   "Yes", "No",  "No",  "No",  "No",   "No"],
        ["arXiv first-class", "Yes","No",  "No",  "No",  "No",   "No"],
        ["Cross-platform",   "Yes", "Web", "Apple","Apple","Web", "Unix"],
        ["Local-only data",  "Yes", "No",  "Opt", "Opt", "Self", "Yes"],
        ["No account",       "Yes", "No",  "Opt", "Opt", "Self", "Yes"],
        ["OPML support",     "Yes", "Yes", "Yes", "No",  "Yes",  "Yes"],
        ["Keyboard-driven",  "Yes", "Yes", "Yes", "Yes", "Yes",  "Yes"],
    ]

    pdf.set_font("Helvetica", "", 8)
    for row in rows:
        for i, val in enumerate(row):
            if i == 0:
                pdf.set_font("Helvetica", "B", 8)
                pdf.set_text_color(*TEXT)
                pdf.set_fill_color(*SECTION_BG)
            else:
                pdf.set_font("Helvetica", "", 8)
                if val == "Yes" and i == 1:
                    pdf.set_text_color(*GREEN)
                    pdf.set_fill_color(*WHITE)
                elif val == "Yes":
                    pdf.set_text_color(80, 80, 80)
                    pdf.set_fill_color(*WHITE)
                elif val == "No":
                    pdf.set_text_color(180, 180, 180)
                    pdf.set_fill_color(*WHITE)
                else:
                    pdf.set_text_color(*MUTED)
                    pdf.set_fill_color(*WHITE)
            pdf.cell(col_w[i], 6, val, border=1, fill=True, align="C" if i > 0 else "L")
        pdf.ln()

    pdf.ln(8)

    # Pricing note
    pdf.set_font("Helvetica", "B", 12)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "Pricing", new_x="LMARGIN", new_y="NEXT")
    pdf.set_draw_color(*ACCENT)
    pdf.set_line_width(1.5)
    pdf.line(pdf.l_margin, pdf.get_y(), pdf.l_margin + 30, pdf.get_y())
    pdf.ln(4)

    pdf.set_font("Helvetica", "", 10.5)
    pdf.set_text_color(*TEXT)
    pdf.multi_cell(0, 5.5,
        "Balanced Breakfast is free during the alpha period with no feed limits "
        "and no feature gating. No accounts, no subscriptions, no data collection."
    )

    pdf.ln(8)

    # Tech
    pdf.set_font("Helvetica", "B", 12)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "Built With", new_x="LMARGIN", new_y="NEXT")
    pdf.set_draw_color(*ACCENT)
    pdf.line(pdf.l_margin, pdf.get_y(), pdf.l_margin + 30, pdf.get_y())
    pdf.ln(4)

    pdf.set_font("Helvetica", "", 10)
    pdf.set_text_color(*MUTED)
    tech_items = [
        "Rust backend (Tauri 2) -- native performance, small binary, low memory",
        "SQLite database -- content-addressable item storage, no duplicates",
        "Rhai scripting engine -- extensible plugin system, no recompilation",
        "Vanilla JavaScript frontend -- no framework bloat",
        "142+ automated tests across the workspace",
    ]
    for item in tech_items:
        pdf.cell(0, 6, f"  {item}", new_x="LMARGIN", new_y="NEXT")

    # =========================================================
    # PAGE 5 - Getting Started
    # =========================================================
    pdf.add_page()

    pdf.section_title("Get Balanced Breakfast")

    # macOS
    pdf.set_font("Helvetica", "B", 13)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "macOS", new_x="LMARGIN", new_y="NEXT")
    pdf.ln(2)

    pdf.set_font("Helvetica", "", 10.5)
    pdf.set_text_color(*MUTED)
    pdf.multi_cell(0, 5.5,
        "The macOS app will be shared with you directly as a .dmg file. "
        "Open the DMG and drag Balanced Breakfast to your Applications folder."
    )
    pdf.ln(2)
    pdf.set_font("Helvetica", "I", 9.5)
    pdf.multi_cell(0, 5,
        "Note: Since the app is not yet on the Mac App Store, macOS may show a security "
        "warning on first launch. Right-click the app and choose \"Open\" to bypass this, "
        "or go to System Settings > Privacy & Security and click \"Open Anyway\"."
    )

    pdf.ln(8)

    # Windows / Linux
    pdf.set_font("Helvetica", "B", 13)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "Windows & Linux", new_x="LMARGIN", new_y="NEXT")
    pdf.ln(2)

    pdf.set_font("Helvetica", "", 10.5)
    pdf.set_text_color(*MUTED)
    pdf.multi_cell(0, 5.5,
        "Balanced Breakfast is built with Tauri 2 and supports Windows and Linux. "
        "Binaries for these platforms will be available soon."
    )

    pdf.ln(8)

    # Getting started
    pdf.set_font("Helvetica", "B", 13)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "Quick Start", new_x="LMARGIN", new_y="NEXT")
    pdf.ln(2)

    steps = [
        ("1.", "Launch the app",
         "Open Balanced Breakfast. The three-panel layout appears: "
         "sources on the left, items in the center, detail on the right."),
        ("2.", "Add your first feed",
         "Press Cmd+N or click Add Feed. Choose a plugin (RSS, Hacker News, or arXiv), "
         "enter the configuration, and click Add."),
        ("3.", "Browse your feeds",
         "Items appear in the center panel. Use j/k to navigate, Enter to read, "
         "s to star, r to mark read/unread."),
        ("4.", "Import existing feeds",
         "Coming from another reader? Press Cmd+I to import an OPML file. "
         "All your RSS subscriptions transfer instantly."),
        ("5.", "Add custom sources",
         "Drop a .rhai plugin file into the plugins directory to add any source "
         "you can script -- news APIs, forums, journals, anything."),
    ]

    for num, title, desc in steps:
        pdf.set_font("Helvetica", "B", 11)
        pdf.set_text_color(*TEXT)
        pdf.cell(8, 6, num)
        pdf.cell(0, 6, title, new_x="LMARGIN", new_y="NEXT")
        pdf.set_x(pdf.l_margin + 8)
        pdf.set_font("Helvetica", "", 9.5)
        pdf.set_text_color(*MUTED)
        pdf.multi_cell(pdf.epw - 8, 4.8, desc, new_x="LMARGIN", new_y="NEXT")
        pdf.ln(3)

    pdf.ln(4)

    # Feedback
    pdf.set_font("Helvetica", "B", 13)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "Sending Feedback", new_x="LMARGIN", new_y="NEXT")
    pdf.ln(2)

    pdf.set_font("Helvetica", "", 10.5)
    pdf.set_text_color(*MUTED)
    pdf.multi_cell(0, 5.5,
        "Your feedback is invaluable during this alpha period. You can:"
    )
    pdf.ln(2)

    feedback = [
        "Take a screenshot in the app and share it with any notes",
        "Message me directly -- bug reports, feature ideas, plugin requests",
        "Export your OPML and share it if you hit import/export issues",
    ]
    for item in feedback:
        pdf.set_x(pdf.l_margin + 4)
        pdf.set_font("Helvetica", "", 10)
        pdf.multi_cell(pdf.epw - 4, 5, f"- {item}", new_x="LMARGIN", new_y="NEXT")

    pdf.ln(10)

    # Thank you
    pdf.set_font("Helvetica", "B", 14)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 10, "Thank you for trying Balanced Breakfast!", align="C", new_x="LMARGIN", new_y="NEXT")

    # =========================================================
    # OUTPUT
    # =========================================================
    output_path = "/Users/max/Git/active/balanced_breakfast/BalancedBreakfast-Pitch.pdf"
    pdf.output(output_path)
    print(f"PDF generated: {output_path}")


if __name__ == "__main__":
    build_pdf()
