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

from fpdf import FPDF

# Colors
BG = (232, 244, 248)       # Light cyan background
TEXT = (27, 54, 93)         # Dark blue text
ACCENT = (247, 209, 84)    # Yellow accent
MUTED = (100, 120, 145)    # Muted text
WHITE = (255, 255, 255)
DARK = (30, 40, 55)
GREEN = (92, 184, 92)
SECTION_BG = (245, 250, 252)


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"GoingsOn  |  goingson.app  |  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")
        # Yellow 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 two_col_features(self, left, right):
        col_w = (self.epw - 8) / 2
        y_start = self.get_y()

        # Left column
        self.set_x(self.l_margin)
        self.feature_col(left, col_w)
        y_left = self.get_y()

        # Right column
        self.set_y(y_start)
        self.set_x(self.l_margin + col_w + 8)
        self.feature_col(right, col_w)
        y_right = self.get_y()

        self.set_y(max(y_left, y_right))

    def feature_col(self, items, width):
        x_start = self.get_x()
        for icon, title, bullets in items:
            self.set_x(x_start)
            self.set_font("Helvetica", "B", 11)
            self.set_text_color(*TEXT)
            self.cell(width, 7, f"{icon}  {title}", new_x="LMARGIN", new_y="NEXT")
            self.ln(1)
            self.set_font("Helvetica", "", 9)
            self.set_text_color(*MUTED)
            for bullet in bullets:
                self.set_x(x_start + 5)
                self.multi_cell(width - 5, 4.2, 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")

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

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

    # Tagline
    pdf.ln(6)
    pdf.set_font("Helvetica", "", 14)
    pdf.set_text_color(180, 195, 210)
    pdf.cell(0, 8, "Tasks. Email. Calendar. Contacts.", align="C", new_x="LMARGIN", new_y="NEXT")
    pdf.cell(0, 8, "One app. Offline-first. Native.", 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(200, 210, 220)
    props = [
        "Everything in one place -- no more juggling 5 apps",
        "Your data stays on your machine -- offline-first with local storage",
        "Native performance -- built with Rust, not Electron",
        "Free during alpha -- no subscriptions, no lock-in",
        "macOS + iOS -- with Windows and Linux support",
    ]
    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(120, 135, 155)
    pdf.cell(0, 6, "Alpha Release  --  March 2026", align="C", new_x="LMARGIN", new_y="NEXT")
    pdf.cell(0, 6, "goingson.app", align="C", new_x="LMARGIN", new_y="NEXT")

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

    pdf.section_title("Core Features")

    pdf.feature_block("TASKS", "Task Management", [
        "Automatic urgency scoring based on priority, due date, and age",
        "Quick-add with natural language: \"Fix bug due:tomorrow +urgent @Project\"",
        "Subtasks, annotations, and milestone tracking",
        "Recurrence (daily, weekly, monthly) with auto-creation on completion",
        "Snooze tasks until later today, tomorrow, weekend, or next week",
        "Bulk operations: complete, snooze, or delete multiple tasks at once",
        "Saved views and named filters for any combination of criteria",
    ])

    pdf.feature_block("EMAIL", "Email Integration", [
        "Full IMAP/SMTP client built in -- connect any email account",
        "OAuth2 for Fastmail, Google, Microsoft, and Yahoo",
        "Native JMAP protocol support for Fastmail",
        "Threaded conversations with reader mode (strips HTML clutter)",
        "Create tasks directly from emails with source linking",
        "Auto-sync on configurable intervals (5/15/30/60 min)",
        "Compose, reply, archive, snooze -- all without leaving the app",
    ])

    pdf.feature_block("CALENDAR", "Calendar & Events", [
        "Create events with title, description, location, and time range",
        "Link events to projects and contacts",
        "Recurrence support (daily, weekly, monthly, yearly)",
        "Time block types: Focus, Personal, Vacation, Free Time",
        "Event reminders with configurable lead time",
        "Conflict detection with existing time blocks",
    ])

    pdf.feature_block("CONTACTS", "Contacts", [
        "Store names, companies, emails, phones, social handles, custom fields",
        "Full-text search across all contact fields",
        "Auto-suggest contacts from email senders",
        "Link contacts to tasks, events, and email threads",
    ])

    # =========================================================
    # PAGE 3 - Planning & Extras
    # =========================================================
    pdf.add_page()

    pdf.section_title("Planning & Workflow")

    pdf.feature_block("PLAN", "Day Planning & Time Blocking", [
        "Visual hourly timeline for your day",
        "Drag unscheduled tasks onto the timeline to create time blocks",
        "Events display alongside tasks on the same timeline",
        "Current time indicator and day navigation with keyboard shortcuts",
        "Collapsible sidebar with unscheduled tasks",
    ])

    pdf.feature_block("REVIEW", "Weekly Review", [
        "Guided review: see what you completed last week",
        "Set focus tasks and projects for the current week",
        "Track vacation days with visual banners",
        "Monday nudge: gentle reminder when review is overdue",
    ])

    pdf.ln(4)
    pdf.section_title("Power Features")

    pdf.feature_block("SEARCH", "Search & Saved Views", [
        "Full-text search across tasks, projects, emails, and contacts",
        "Date range syntax: after:, before:, from:, to:",
        "Save any filter as a named view; pin favorites for quick access",
    ])

    pdf.feature_block("KEYBOARD", "Keyboard-Driven", [
        "Vim-style navigation (j/k for items, g+key to jump between views)",
        "Quick-add from anywhere with 'q', shortcuts overlay with '?'",
        "Native menu bar with standard macOS shortcuts",
    ])

    pdf.feature_block("EXPORT", "Export & Backup", [
        "Export all data as JSON, tasks as CSV, events as ICS",
        "Automated daily backups with configurable retention",
        "Restore from any backup point",
    ])

    pdf.feature_block("THEMES", "Themes", [
        "10 built-in themes: Catppuccin, Dracula, Nord, Tokyo Night, and more",
        "Follow System for automatic light/dark switching",
    ])

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

    pdf.section_title("Why GoingsOn?")

    pdf.set_font("Helvetica", "", 10.5)
    pdf.set_text_color(*TEXT)
    pdf.multi_cell(0, 5.5,
        "Most productivity tools are cloud-only, subscription-based, and spread across "
        "multiple apps. GoingsOn brings tasks, email, calendar, and contacts into one "
        "native application that works offline and keeps your data on your machine."
    )
    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 = ["", "GoingsOn", "Todoist", "Things 3", "Fantastical", "Spark"]
    col_w = [38, 28, 28, 28, 28, 28]

    # 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 = [
        ["Tasks",         "Yes", "Yes", "Yes", "No",  "No"],
        ["Email",         "Yes", "No",  "No",  "No",  "Yes"],
        ["Calendar",      "Yes", "No",  "No",  "Yes", "No"],
        ["Contacts",      "Yes", "No",  "No",  "No",  "No"],
        ["Weekly Review", "Yes", "No",  "No",  "No",  "No"],
        ["Offline-first", "Yes", "No",  "Yes", "Yes", "No"],
        ["Cross-platform","Yes", "Yes", "Apple","Apple","Yes"],
        ["Free option",   "Yes", "Ltd", "No",  "Ltd", "Ltd"],
    ]

    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,
        "GoingsOn is free during the alpha period. After launch, we plan to offer a "
        "one-time purchase model -- no subscriptions. Your data, your app, forever."
    )

    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 -- your data in a single local file",
        "Vanilla JavaScript frontend -- no framework bloat",
        "205+ automated tests across the codebase",
    ]
    for item in tech_items:
        pdf.cell(0, 6, f"  {item}", new_x="LMARGIN", new_y="NEXT")

    # =========================================================
    # PAGE 5 - Installing via TestFlight
    # =========================================================
    pdf.add_page()

    pdf.section_title("Get GoingsOn")

    # 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 GoingsOn 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)

    # iOS TestFlight
    pdf.set_font("Helvetica", "B", 13)
    pdf.set_text_color(*TEXT)
    pdf.cell(0, 8, "iOS (via TestFlight)", 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,
        "GoingsOn for iOS is distributed through Apple's TestFlight, "
        "which lets you install pre-release apps directly on your iPhone or iPad."
    )
    pdf.ln(4)

    steps = [
        ("1.", "Install TestFlight",
         "Download \"TestFlight\" from the App Store if you don't have it already. "
         "It's a free app by Apple."),
        ("2.", "Open the invitation link",
         "You'll receive a TestFlight invitation link (via email or message). "
         "Tap the link on your iPhone or iPad."),
        ("3.", "Accept the invitation",
         "The link opens TestFlight. Tap \"Accept\" to join the GoingsOn beta, "
         "then tap \"Install\" to download the app."),
        ("4.", "Open GoingsOn",
         "Once installed, GoingsOn appears on your home screen like any other app. "
         "You can also launch it from inside TestFlight."),
        ("5.", "Updates are automatic",
         "When a new build is available, TestFlight will notify you. "
         "Updates install with one tap -- no need to uninstall and reinstall."),
    ]

    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",
        "Use TestFlight's built-in feedback tool (shake your iPhone or tap \"Send Beta Feedback\" in TestFlight)",
        "Message me directly -- anything from bug reports to feature ideas",
    ]
    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 testing GoingsOn!", align="C", new_x="LMARGIN", new_y="NEXT")


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


if __name__ == "__main__":
    build_pdf()
