#!/usr/bin/env python3
"""Build RAPS_Showcase_V1.pptx from scratch using python-pptx"""

import io
import os
import tempfile
from pptx import Presentation
from pptx.util import Inches, Pt, Emu, Cm
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN
from pptx.oxml.ns import qn
from pptx.oxml import parse_xml
import cairosvg
from PIL import Image

# ── Brand colors ──────────────────────────────────────────────────────
NAVY   = RGBColor(0x00, 0x55, 0x94)
AMBER  = RGBColor(0xfb, 0xc6, 0x24)
SKY    = RGBColor(0x76, 0xca, 0xdd)
ORANGE = RGBColor(0xF1, 0x6F, 0x12)
MID_BL = RGBColor(0x46, 0x6a, 0xb1)
WHITE  = RGBColor(0xFF, 0xFF, 0xFF)
DARK_TEXT = RGBColor(0x1d, 0x1c, 0x1d)
GREEN_POS = RGBColor(0x1a, 0x80, 0x40)
RED_NEG   = RGBColor(0xe7, 0x1f, 0x20)

# ── Slide dimensions: 33.87cm x 19.05cm (widescreen 16:9) ────────────
SW = Cm(33.87)
SH = Cm(19.05)

# Convert SVG logos to PNG in memory
LOGO_DARK_SVG  = "/home/ubuntu/.openclaw/workspace/design-system/assets/regdesk-logo-dark.svg"
LOGO_LIGHT_SVG = "/home/ubuntu/.openclaw/workspace/design-system/assets/regdesk-logo-light.svg"

def svg_to_png_bytes(svg_path, width=600):
    """Convert SVG to PNG bytes"""
    return cairosvg.svg2png(url=svg_path, output_width=width)

# Pre-convert logos
logo_dark_png  = svg_to_png_bytes(LOGO_DARK_SVG, width=400)
logo_light_png = svg_to_png_bytes(LOGO_LIGHT_SVG, width=400)

# ── Helper functions ──────────────────────────────────────────────────

def new_prs():
    prs = Presentation()
    prs.slide_width  = SW
    prs.slide_height = SH
    return prs

def add_slide(prs, layout_idx=6):
    layout = prs.slide_layouts[layout_idx]  # blank layout
    return prs.slides.add_slide(layout)

def fill_slide_bg(slide, r, g, b):
    """Fill slide background with solid color"""
    bg = slide.background
    fill = bg.fill
    fill.solid()
    fill.fore_color.rgb = RGBColor(r, g, b)

def add_text_box(slide, text, left, top, width, height,
                 font_name="Arial", font_size=18, bold=False, italic=False,
                 color=WHITE, align=PP_ALIGN.LEFT, word_wrap=True):
    txBox = slide.shapes.add_textbox(left, top, width, height)
    tf = txBox.text_frame
    tf.word_wrap = word_wrap
    p = tf.paragraphs[0]
    p.alignment = align
    run = p.add_run()
    run.text = text
    run.font.name = font_name
    run.font.size = Pt(font_size)
    run.font.bold = bold
    run.font.italic = italic
    run.font.color.rgb = color
    return txBox, tf

def add_gradient_stripe(slide, top, height=Cm(0.2)):
    """Add gradient accent bar as 4 colored rectangles"""
    segment_w = SW / 4
    colors = [MID_BL, SKY, AMBER, ORANGE]
    for i, col in enumerate(colors):
        shape = slide.shapes.add_shape(
            1,  # MSO_SHAPE_TYPE.RECTANGLE
            int(segment_w * i), int(top), int(segment_w), int(height)
        )
        shape.fill.solid()
        shape.fill.fore_color.rgb = col
        shape.line.fill.background()  # no border

def add_logo(slide, png_bytes, left, top, height):
    img_stream = io.BytesIO(png_bytes)
    slide.shapes.add_picture(img_stream, left, top, height=height)

def add_footer(slide, dark=True, text="© RegDesk 2026 · regdesk.co"):
    """Add footer bar at bottom"""
    footer_h = Cm(1.5)
    footer_top = SH - footer_h
    
    # Background
    shape = slide.shapes.add_shape(1, 0, int(footer_top), int(SW), int(footer_h))
    if dark:
        shape.fill.solid()
        shape.fill.fore_color.rgb = RGBColor(0x00, 0x00, 0x00)
        shape.fill.fore_color.rgb  # 25% black
        shape.fill.solid()
        shape.fill.fore_color.rgb = RGBColor(0x00, 0x33, 0x55)
    else:
        shape.fill.solid()
        shape.fill.fore_color.rgb = NAVY
    shape.line.fill.background()
    
    # Footer text
    txt_color = RGBColor(0xff, 0xff, 0xff)
    add_text_box(slide, text,
                 Cm(2), int(footer_top + Cm(0.3)), SW - Cm(4), footer_h,
                 font_size=11, color=txt_color, align=PP_ALIGN.CENTER)

def add_rect(slide, left, top, width, height, fill_color, border=False, border_color=None, border_width=Pt(1)):
    from pptx.util import Pt
    shape = slide.shapes.add_shape(1, int(left), int(top), int(width), int(height))
    shape.fill.solid()
    shape.fill.fore_color.rgb = fill_color
    if border and border_color:
        shape.line.color.rgb = border_color
        shape.line.width = border_width
    else:
        shape.line.fill.background()
    return shape

# ═══════════════════════════════════════════════════════════════════════
# BUILD PRESENTATION
# ═══════════════════════════════════════════════════════════════════════

prs = new_prs()

# ─── SLIDE 1 — HERO ──────────────────────────────────────────────────
slide1 = add_slide(prs)
fill_slide_bg(slide1, 0x00, 0x55, 0x94)

# Top gradient stripe
add_gradient_stripe(slide1, top=0, height=Cm(0.25))

# Logo (centered)
logo_h = Cm(2.2)
logo_stream = io.BytesIO(logo_dark_png)
# Calculate logo width from PNG aspect
img = Image.open(io.BytesIO(logo_dark_png))
aspect = img.width / img.height
logo_w = logo_h * aspect
logo_left = (SW - logo_w) / 2
slide1.shapes.add_picture(io.BytesIO(logo_dark_png), int(logo_left), int(Cm(2.0)), height=int(logo_h))

# RAPS badge
add_text_box(slide1, "RAPS CONVERGENCE 2026",
             Cm(3), Cm(5.2), SW - Cm(6), Cm(1.0),
             font_size=14, bold=True, color=AMBER, align=PP_ALIGN.CENTER)

# Main headline
add_text_box(slide1, "Regulatory Intelligence.",
             Cm(2), Cm(6.4), SW - Cm(4), Cm(2.2),
             font_size=54, bold=True, color=WHITE, align=PP_ALIGN.CENTER)
add_text_box(slide1, "Reimagined.",
             Cm(2), Cm(8.2), SW - Cm(4), Cm(2.0),
             font_size=54, bold=True, color=AMBER, align=PP_ALIGN.CENTER)

# Tagline
add_text_box(slide1, "The only platform purpose-built for medical device regulatory professionals — from first market to global scale.",
             Cm(4), Cm(10.5), SW - Cm(8), Cm(2.0),
             font_size=18, color=RGBColor(0xcc, 0xdd, 0xee), align=PP_ALIGN.CENTER)

# Stats badge
add_text_box(slide1, "🏛️  RAPS Convergence 2026          ✦          regdesk.co",
             Cm(4), Cm(13.2), SW - Cm(8), Cm(1.2),
             font_size=16, color=WHITE, align=PP_ALIGN.CENTER)

# Bottom gradient stripe
add_gradient_stripe(slide1, top=SH - Cm(2.0), height=Cm(0.25))
add_footer(slide1, dark=True)

# ─── SLIDE 2 — 4 PILLARS ─────────────────────────────────────────────
slide2 = add_slide(prs)
fill_slide_bg(slide2, 0xFF, 0xFF, 0xFF)

add_gradient_stripe(slide2, top=0, height=Cm(0.25))

# Header
add_text_box(slide2, "THE REGDESK PLATFORM — FOUR PILLARS OF REGULATORY EXCELLENCE",
             Cm(1.5), Cm(0.8), SW - Cm(3), Cm(0.9),
             font_size=13, bold=True, color=ORANGE, align=PP_ALIGN.LEFT)

pillars = [
    ("🗺️", "Plan", NAVY, "Map regulatory pathways across 120+ markets. AI-powered classification and submission strategy — before you commit resources."),
    ("🔧", "Build", RGBColor(0x1a, 0x5f, 0x2e), "Compile compliant submissions with labeling requirements, UDI management, and automated document workflows."),
    ("📊", "Track", RGBColor(0x8b, 0x4a, 0x00), "Monitor registrations, renewals, and approvals across your entire portfolio. Real-time dashboards and renewal alerts."),
    ("🛡️", "Maintain", RGBColor(0x5a, 0x00, 0x80), "Stay ahead of regulatory changes with automated impact assessments. Audit-ready documentation at every step."),
]

card_w = (SW - Cm(5.5)) / 4
card_h = Cm(12.5)
card_top = Cm(2.0)

for i, (icon, title, bg_color, desc) in enumerate(pillars):
    cx = Cm(1.5) + i * (card_w + Cm(0.5))
    
    # Card background
    add_rect(slide2, cx, card_top, card_w, card_h, bg_color)
    
    # Icon
    add_text_box(slide2, icon, cx + Cm(0.5), card_top + Cm(0.8), card_w - Cm(1), Cm(1.5),
                 font_size=32, color=WHITE, align=PP_ALIGN.LEFT)
    
    # Title
    add_text_box(slide2, title, cx + Cm(0.5), card_top + Cm(2.5), card_w - Cm(1), Cm(1.0),
                 font_size=22, bold=True, color=AMBER, align=PP_ALIGN.LEFT)
    
    # Description
    add_text_box(slide2, desc, cx + Cm(0.5), card_top + Cm(3.8), card_w - Cm(1.0), Cm(7.0),
                 font_size=13, color=RGBColor(0xdd, 0xee, 0xff), align=PP_ALIGN.LEFT)
    
    # Bottom accent stripe
    add_gradient_stripe(slide2, top=card_top + card_h - Cm(0.2), height=Cm(0.2))

add_gradient_stripe(slide2, top=SH - Cm(1.6), height=Cm(0.15))
add_footer(slide2, dark=False, text="© RegDesk 2026 · Plan · Build · Track · Maintain · regdesk.co")

# ─── SLIDE 3 — AI-POWERED INTELLIGENCE ───────────────────────────────
slide3 = add_slide(prs)
fill_slide_bg(slide3, 0x00, 0x55, 0x94)
add_gradient_stripe(slide3, top=0, height=Cm(0.25))

add_text_box(slide3, "INTELLIGENT RIM",
             Cm(2), Cm(0.8), SW - Cm(4), Cm(0.7),
             font_size=13, bold=True, color=SKY)
add_text_box(slide3, "AI-Powered Regulatory Intelligence Engine",
             Cm(2), Cm(1.7), SW - Cm(4), Cm(1.8),
             font_size=38, bold=True, color=WHITE)

ai_cards = [
    ("01", "Change Impact Assessment", "Automatically detect regulatory changes and instantly assess impact across every product, SKU, and market in your portfolio. No manual monitoring needed."),
    ("02", "Smart Classification", "AI-driven device classification across 120+ markets. Instantly determine your regulatory pathway before committing engineering resources."),
    ("03", "Automated Alerts", "Never miss a renewal or regulatory update. Real-time monitoring with intelligent notifications mapped to your specific device portfolio."),
    ("04", "Regulatory Co-Pilot", "AI-assisted labeling review, document summarization, and workflow automation — your regulatory team, supercharged."),
]

card_w2 = (SW - Cm(5)) / 4
card_h2 = Cm(9.5)
card_top2 = Cm(4.0)

for i, (num, title, desc) in enumerate(ai_cards):
    cx = Cm(1.2) + i * (card_w2 + Cm(0.5))
    add_rect(slide3, cx, card_top2, card_w2, card_h2, RGBColor(0x00, 0x3d, 0x6e))
    # Number watermark
    add_text_box(slide3, num, cx + card_w2 - Cm(2.0), card_top2 + Cm(0.2), Cm(2.0), Cm(2.0),
                 font_size=48, bold=True, color=RGBColor(0x00, 0x45, 0x78))
    # Title
    add_text_box(slide3, title, cx + Cm(0.5), card_top2 + Cm(0.8), card_w2 - Cm(1.0), Cm(1.8),
                 font_size=15, bold=True, color=AMBER)
    # Description
    add_text_box(slide3, desc, cx + Cm(0.5), card_top2 + Cm(2.8), card_w2 - Cm(1.0), Cm(6.0),
                 font_size=12, color=RGBColor(0xcc, 0xdd, 0xee))

add_gradient_stripe(slide3, top=SH - Cm(1.6), height=Cm(0.25))
add_footer(slide3, dark=True, text="© RegDesk 2026 · AI-Driven Regulatory Intelligence · regdesk.co")

# ─── SLIDE 4 — ENTERPRISE VALIDATION ─────────────────────────────────
slide4 = add_slide(prs)
fill_slide_bg(slide4, 0xFF, 0xFF, 0xFF)
add_gradient_stripe(slide4, top=0, height=Cm(0.25))

add_text_box(slide4, "ENTERPRISE-GRADE VALIDATION",
             Cm(1.5), Cm(0.8), SW - Cm(3), Cm(0.7),
             font_size=12, bold=True, color=ORANGE)
add_text_box(slide4, "Built for Audit. Ready for Review.",
             Cm(1.5), Cm(1.7), Cm(14), Cm(3.0),
             font_size=32, bold=True, color=NAVY)

features = [
    "Full audit trails — Every action, document, and approval automatically captured",
    "GxP compliance — Supports Good Practice guidelines including electronic records",
    "21 CFR Part 11 — Electronic signature and records compliance built-in",
    "Change management — 4-stage validated workflow from assessment to approval",
    "Role-based access control — Enterprise identity and permissions management",
    "SSO & multi-tenant workspaces — Secure collaboration across your organization",
]

for i, feat in enumerate(features):
    add_text_box(slide4, "▸  " + feat,
                 Cm(1.5), Cm(5.2) + i * Cm(1.35), Cm(15), Cm(1.3),
                 font_size=13, color=DARK_TEXT)

# Right side visual
add_rect(slide4, Cm(17.5), Cm(1.8), Cm(14.5), Cm(6.5), NAVY)
add_text_box(slide4, "✓", Cm(22), Cm(2.5), Cm(5), Cm(3.0),
             font_size=80, bold=True, color=AMBER, align=PP_ALIGN.CENTER)
add_text_box(slide4, "Audit-Ready by Design",
             Cm(17.8), Cm(5.8), Cm(14.0), Cm(0.9),
             font_size=20, bold=True, color=WHITE, align=PP_ALIGN.CENTER)

boxes = [("100%", "Automated Audit Trail", GREEN_POS), ("GxP", "Compliance Supported", RGBColor(0xb8, 0x8a, 0x00)), ("Part 11", "21 CFR Ready", NAVY)]
for i, (num, lab, col) in enumerate(boxes):
    bx = Cm(17.5) + i * Cm(4.8)
    add_rect(slide4, bx, Cm(9.2), Cm(4.5), Cm(3.8), RGBColor(0xf1, 0xfa, 0xfc))
    add_text_box(slide4, num, bx + Cm(0.3), Cm(9.5), Cm(4.0), Cm(1.5),
                 font_size=22, bold=True, color=col, align=PP_ALIGN.CENTER)
    add_text_box(slide4, lab, bx + Cm(0.3), Cm(11.2), Cm(4.0), Cm(1.5),
                 font_size=11, bold=True, color=col, align=PP_ALIGN.CENTER)

add_gradient_stripe(slide4, top=SH - Cm(1.6), height=Cm(0.15))
add_footer(slide4, dark=False, text="© RegDesk 2026 · Enterprise Validation & Compliance · regdesk.co")

# ─── SLIDE 5 — SECURITY & COMPLIANCE ─────────────────────────────────
slide5 = add_slide(prs)
fill_slide_bg(slide5, 0x00, 0x55, 0x94)
add_gradient_stripe(slide5, top=0, height=Cm(0.25))

add_text_box(slide5, "TRUST & COMPLIANCE",
             Cm(2), Cm(0.8), SW - Cm(4), Cm(0.7),
             font_size=13, bold=True, color=SKY, align=PP_ALIGN.CENTER)
add_text_box(slide5, "Security You Can Count On. Compliance You Can Prove.",
             Cm(2), Cm(1.7), SW - Cm(4), Cm(2.0),
             font_size=30, bold=True, color=WHITE, align=PP_ALIGN.CENTER)
add_text_box(slide5, "Enterprise-grade security infrastructure protecting your most sensitive regulatory data — certified, validated, and auditable.",
             Cm(3), Cm(4.0), SW - Cm(6), Cm(1.5),
             font_size=16, color=RGBColor(0xcc, 0xdd, 0xee), align=PP_ALIGN.CENTER)

certs = [
    ("🔒", "SOC 2 Type II", AMBER, "Independently audited security controls. Your data is protected by the gold standard in enterprise security."),
    ("🛡️", "ISO 27001", SKY, "International standard for information security management systems — verified and maintained."),
    ("📋", "21 CFR Part 11", ORANGE, "Electronic records and electronic signatures compliance for FDA-regulated environments."),
    ("✅", "GxP Compliant", RGBColor(0x4a, 0xde, 0x80), "Supports Good Practice guidelines including full audit trails and electronic records management."),
]

card_w5 = (SW - Cm(5)) / 4
card_h5 = Cm(8.0)
card_top5 = Cm(6.0)

for i, (icon, title, accent, desc) in enumerate(certs):
    cx = Cm(1.2) + i * (card_w5 + Cm(0.5))
    add_rect(slide5, cx, card_top5, card_w5, card_h5, RGBColor(0x00, 0x3d, 0x6e))
    # Top accent
    add_rect(slide5, cx, card_top5, card_w5, Cm(0.3), accent)
    add_text_box(slide5, icon, cx + Cm(0.5), card_top5 + Cm(0.8), card_w5 - Cm(1), Cm(2.0),
                 font_size=40, align=PP_ALIGN.CENTER, color=WHITE)
    add_text_box(slide5, title, cx + Cm(0.3), card_top5 + Cm(3.2), card_w5 - Cm(0.6), Cm(1.0),
                 font_size=16, bold=True, color=accent, align=PP_ALIGN.CENTER)
    add_text_box(slide5, desc, cx + Cm(0.3), card_top5 + Cm(4.4), card_w5 - Cm(0.6), Cm(3.2),
                 font_size=11, color=RGBColor(0xcc, 0xdd, 0xee), align=PP_ALIGN.CENTER)

add_gradient_stripe(slide5, top=SH - Cm(1.6), height=Cm(0.25))
add_footer(slide5, dark=True, text="© RegDesk 2026 · SOC 2 · ISO 27001 · 21 CFR Part 11 · GxP · regdesk.co")

# ─── SLIDE 6 — SCALE / KEY STATS ─────────────────────────────────────
slide6 = add_slide(prs)
fill_slide_bg(slide6, 0xFF, 0xFF, 0xFF)
add_gradient_stripe(slide6, top=0, height=Cm(0.25))

add_text_box(slide6, "GLOBAL SCALE",
             Cm(2), Cm(0.8), SW - Cm(4), Cm(0.7),
             font_size=12, bold=True, color=ORANGE, align=PP_ALIGN.CENTER)
add_text_box(slide6, "Trusted at Scale",
             Cm(2), Cm(1.7), SW - Cm(4), Cm(1.5),
             font_size=38, bold=True, color=NAVY, align=PP_ALIGN.CENTER)

stats = [
    ("120+", "Markets Covered", "Full regulatory depth, not just alerts"),
    ("250K+", "Products Managed", "Across global portfolios"),
    ("$2.2B", "Market Size (2024)", "Growing to $6.2B by 2034"),
    ("11%", "CAGR 2024-2034", "Fastest-growing RIM segment"),
]

card_w6 = (SW - Cm(5)) / 4
card_h6 = Cm(5.5)
card_top6 = Cm(3.8)

for i, (num, label, sub) in enumerate(stats):
    cx = Cm(1.2) + i * (card_w6 + Cm(0.5))
    add_rect(slide6, cx, card_top6, card_w6, card_h6, RGBColor(0xf1, 0xfa, 0xfc))
    # Top accent
    add_rect(slide6, cx, card_top6, card_w6, Cm(0.3), NAVY)
    add_text_box(slide6, num, cx + Cm(0.3), card_top6 + Cm(0.6), card_w6 - Cm(0.6), Cm(2.2),
                 font_size=38, bold=True, color=NAVY, align=PP_ALIGN.CENTER)
    add_text_box(slide6, label, cx + Cm(0.3), card_top6 + Cm(3.0), card_w6 - Cm(0.6), Cm(0.9),
                 font_size=13, bold=True, color=DARK_TEXT, align=PP_ALIGN.CENTER)
    add_text_box(slide6, sub, cx + Cm(0.3), card_top6 + Cm(4.0), card_w6 - Cm(0.6), Cm(1.0),
                 font_size=10, color=RGBColor(0x99, 0x99, 0x99), align=PP_ALIGN.CENTER)

differentiators = [
    ("120+ Market Content Database", NAVY, "Deep regulatory guidance — not surface-level alerts. The only platform with this level of device-specific depth."),
    ("Device-Focused by Design", ORANGE, "Purpose-built for medical device manufacturers. Every feature built for device regulatory teams."),
    ("Distributor Collaboration Tools", AMBER, "Unique in-platform distributor workflow management. A genuine differentiator no competitor has matched."),
]

row_top = Cm(10.5)
col_w = (SW - Cm(4)) / 3

for i, (title, accent, desc) in enumerate(differentiators):
    cx = Cm(1.2) + i * (col_w + Cm(0.5))
    add_rect(slide6, cx, row_top, col_w, Cm(4.0), RGBColor(0xf1, 0xfa, 0xfc))
    add_rect(slide6, cx, row_top, Cm(0.4), Cm(4.0), accent)
    add_text_box(slide6, title, cx + Cm(0.8), row_top + Cm(0.4), col_w - Cm(1.2), Cm(0.9),
                 font_size=14, bold=True, color=NAVY)
    add_text_box(slide6, desc, cx + Cm(0.8), row_top + Cm(1.5), col_w - Cm(1.2), Cm(2.2),
                 font_size=12, color=DARK_TEXT)

add_gradient_stripe(slide6, top=SH - Cm(1.6), height=Cm(0.15))
add_footer(slide6, dark=False, text="© RegDesk 2026 · 120+ Markets · 250K+ Products · Purpose-Built for MedDev · regdesk.co")

# ─── SLIDE 7 — WHY REGDESK ───────────────────────────────────────────
slide7 = add_slide(prs)
fill_slide_bg(slide7, 0x00, 0x55, 0x94)
add_gradient_stripe(slide7, top=0, height=Cm(0.25))

# Left panel
add_text_box(slide7, "THE REGDESK DIFFERENCE",
             Cm(1.5), Cm(0.8), Cm(15), Cm(0.7),
             font_size=13, bold=True, color=AMBER)
add_text_box(slide7, "From First Market to Global Scale —",
             Cm(1.5), Cm(1.8), Cm(16), Cm(1.4),
             font_size=28, bold=True, color=WHITE)
add_text_box(slide7, "In One Platform.",
             Cm(1.5), Cm(3.1), Cm(16), Cm(1.3),
             font_size=28, bold=True, color=AMBER)
add_text_box(slide7, "Most teams manage regulatory with spreadsheets, email threads, and outdated PDFs. RegDesk replaces all of it — with a system that scales with you.",
             Cm(1.5), Cm(4.8), Cm(15), Cm(2.5),
             font_size=14, color=RGBColor(0xcc, 0xdd, 0xee))

# Right panel — comparison
add_text_box(slide7, "Without RegDesk  →  With RegDesk",
             Cm(17.5), Cm(0.8), Cm(15), Cm(0.8),
             font_size=13, bold=True, color=SKY)

comparisons = [
    ("❌  Manual tracking in spreadsheets — error-prone, not scalable",
     "✅  Real-time portfolio dashboard across all markets"),
    ("❌  Missed renewals discovered late — costly delays",
     "✅  Automated alerts months in advance, every time"),
    ("❌  Regulatory changes discovered months after the fact",
     "✅  AI impact assessment the moment a change is published"),
]

for i, (before, after) in enumerate(comparisons):
    row_y = Cm(2.0) + i * Cm(4.5)
    # Before
    add_rect(slide7, Cm(17.5), row_y, Cm(7.0), Cm(3.8), RGBColor(0x44, 0x00, 0x00))
    add_text_box(slide7, before, Cm(18.0), row_y + Cm(0.3), Cm(6.0), Cm(3.2),
                 font_size=12, color=RGBColor(0xff, 0x80, 0x80))
    # After
    add_rect(slide7, Cm(25.0), row_y, Cm(7.0), Cm(3.8), RGBColor(0x00, 0x33, 0x11))
    add_text_box(slide7, after, Cm(25.5), row_y + Cm(0.3), Cm(6.0), Cm(3.2),
                 font_size=12, color=RGBColor(0x4a, 0xde, 0x80))

add_gradient_stripe(slide7, top=SH - Cm(1.6), height=Cm(0.25))
add_footer(slide7, dark=True, text="© RegDesk 2026 · Regulatory Intelligence Redefined · regdesk.co")

# ─── SLIDE 8 — INTEGRATIONS ──────────────────────────────────────────
slide8 = add_slide(prs)
fill_slide_bg(slide8, 0xFF, 0xFF, 0xFF)
add_gradient_stripe(slide8, top=0, height=Cm(0.25))

add_text_box(slide8, "INTEGRATIONS & ECOSYSTEM",
             Cm(1.5), Cm(0.8), Cm(14), Cm(0.7),
             font_size=12, bold=True, color=ORANGE)
add_text_box(slide8, "Fits Into Your Existing Stack.",
             Cm(1.5), Cm(1.7), Cm(15), Cm(2.0),
             font_size=30, bold=True, color=NAVY)

int_features = [
    "🔗  API access — Read/write API to integrate RegDesk into your existing tech stack",
    "⚙️  ERP / PLM integration — SAP, PLM, eDMS, and enterprise systems",
    "✅  QMS / eQMS integration — Bi-directional sync with quality management systems",
    "🔐  SSO / enterprise auth — SAML, OIDC, and enterprise identity providers",
    "🏷️  UDI management — GUDID and EUDAMED via Reed Tech partnership",
]

for i, feat in enumerate(int_features):
    add_text_box(slide8, feat,
                 Cm(1.5), Cm(4.0) + i * Cm(1.4), Cm(15), Cm(1.3),
                 font_size=12, color=DARK_TEXT)

# Self-service box
add_rect(slide8, Cm(1.5), Cm(11.5), Cm(15), Cm(3.2), RGBColor(0xf1, 0xfa, 0xfc))
add_text_box(slide8, "Self-Service Onboarding",
             Cm(2.0), Cm(11.9), Cm(14), Cm(0.9),
             font_size=16, bold=True, color=NAVY)
add_text_box(slide8, "No 6-month implementation. No dedicated consultant required. Get your team onboarded and productive in days — not quarters.",
             Cm(2.0), Cm(13.0), Cm(14), Cm(1.5),
             font_size=13, color=DARK_TEXT)

# Right grid
int_boxes = [
    ("🔗", "REST API", "Full read/write access"),
    ("⚙️", "ERP / PLM", "SAP & enterprise systems"),
    ("✅", "QMS", "Quality management sync"),
    ("🔐", "SSO", "Enterprise identity"),
    ("🏷️", "UDI Management", "GUDID + EUDAMED via Reed Tech"),
]

box_w = Cm(6.5)
box_h = Cm(3.5)
wide_w = Cm(13.5)

positions = [
    (Cm(17.5), Cm(1.8)),
    (Cm(24.5), Cm(1.8)),
    (Cm(17.5), Cm(5.8)),
    (Cm(24.5), Cm(5.8)),
    (Cm(17.5), Cm(9.8)),  # wide
]

for i, (icon, title, sub) in enumerate(int_boxes):
    x, y = positions[i]
    w = wide_w if i == 4 else box_w
    add_rect(slide8, x, y, w, box_h, RGBColor(0xf1, 0xfa, 0xfc))
    add_text_box(slide8, icon + "  " + title,
                 x + Cm(0.5), y + Cm(0.5), w - Cm(1), Cm(1.0),
                 font_size=16, bold=True, color=NAVY)
    add_text_box(slide8, sub,
                 x + Cm(0.5), y + Cm(1.8), w - Cm(1), Cm(1.2),
                 font_size=11, color=RGBColor(0x55, 0x55, 0x55))

add_gradient_stripe(slide8, top=SH - Cm(1.6), height=Cm(0.15))
add_footer(slide8, dark=False, text="© RegDesk 2026 · API · ERP/PLM · QMS · SSO · UDI · regdesk.co")

# ─── SLIDE 9 — CLOSING CTA ───────────────────────────────────────────
slide9 = add_slide(prs)
fill_slide_bg(slide9, 0x00, 0x55, 0x94)
add_gradient_stripe(slide9, top=0, height=Cm(0.25))

# Logo
logo_stream9 = io.BytesIO(logo_dark_png)
img9 = Image.open(io.BytesIO(logo_dark_png))
aspect9 = img9.width / img9.height
lh9 = Cm(2.5)
lw9 = lh9 * aspect9
slide9.shapes.add_picture(io.BytesIO(logo_dark_png), int((SW - lw9)/2), int(Cm(1.8)), height=int(lh9))

add_text_box(slide9, "Meet Us at RAPS 2026",
             Cm(3), Cm(5.0), SW - Cm(6), Cm(0.9),
             font_size=18, bold=True, color=AMBER, align=PP_ALIGN.CENTER)

add_text_box(slide9, "Regulatory Intelligence.\nReimagined.",
             Cm(2), Cm(6.2), SW - Cm(4), Cm(3.5),
             font_size=52, bold=True, color=WHITE, align=PP_ALIGN.CENTER)

add_text_box(slide9, "The only platform purpose-built for medical device\nregulatory professionals — from first market to global scale.",
             Cm(3.5), Cm(10.2), SW - Cm(7), Cm(2.0),
             font_size=18, color=RGBColor(0xcc, 0xdd, 0xee), align=PP_ALIGN.CENTER)

# CTA buttons area
add_rect(slide9, Cm(10), Cm(12.8), Cm(8), Cm(1.5), AMBER)
add_text_box(slide9, "regdesk.co", Cm(10), Cm(12.9), Cm(8), Cm(1.3),
             font_size=20, bold=True, color=NAVY, align=PP_ALIGN.CENTER)

add_rect(slide9, Cm(19.5), Cm(12.8), Cm(8), Cm(1.5), RGBColor(0x00, 0x3d, 0x6e))
add_text_box(slide9, "🏛️  RAPS Convergence 2026", Cm(19.5), Cm(12.9), Cm(8), Cm(1.3),
             font_size=16, color=WHITE, align=PP_ALIGN.CENTER)

# Bottom stripe
add_gradient_stripe(slide9, top=SH - Cm(1.6), height=Cm(0.25))
add_footer(slide9, dark=True, text="© RegDesk 2026 · regdesk.co · RAPS Convergence 2026")

# ─── Save ─────────────────────────────────────────────────────────────
OUTPUT = "/home/ubuntu/caddy/public/RAPS_Showcase_V1.pptx"
prs.save(OUTPUT)
print(f"✅  Saved: {OUTPUT}")
print(f"   Slides: {len(prs.slides)}")
