from fpdf import FPDF
import os

class GoProductionPDF(FPDF):
    def __init__(self):
        super().__init__()
        self.set_margins(25, 25, 25)
        self.set_auto_page_break(auto=True, margin=25)
        self.brand_color = (128, 0, 32) # Deep Burgundy
        self.secondary_color = (60, 60, 60) # Charcoal Grey
        self.text_color = (40, 40, 40)

    def header(self):
        if self.page_no() > 1:
            self.set_font('helvetica', 'I', 9)
            self.set_text_color(150, 150, 150)
            self.set_xy(25, 15)
            self.cell(0, 10, 'GO-Production: System Features & Benefits', 0, 0, 'L')
            self.set_xy(25, 15)
            self.cell(0, 10, f'Page {self.page_no()}', 0, 1, 'R')
            self.set_draw_color(220, 220, 220)
            self.line(25, 25, 185, 25)

    def footer(self):
        if self.page_no() > 1:
            self.set_y(-20)
            self.set_font('helvetica', 'I', 8)
            self.set_text_color(128, 128, 128)
            self.set_draw_color(220, 220, 220)
            self.line(25, 275, 185, 275)
            self.set_x(25)
            self.cell(0, 10, 'GO-Production - Advanced Production & Intelligence System', 0, 0, 'C')

    def cover_page(self):
        self.add_page()
        self.set_fill_color(*self.brand_color)
        self.rect(0, 0, 210, 120, 'F')
        
        self.set_y(50)
        self.set_font('helvetica', 'B', 40)
        self.set_text_color(255, 255, 255)
        self.cell(0, 25, 'GO-Production', 0, 1, 'C')
        
        self.set_font('helvetica', '', 16)
        self.cell(0, 10, 'System Features & Benefits Documentation', 0, 1, 'C')
        
        self.set_y(160)
        self.set_font('helvetica', 'B', 24)
        self.set_text_color(*self.secondary_color)
        self.cell(0, 20, 'Enterprise-Grade', 0, 1, 'C')
        self.cell(0, 20, 'Manufacturing Intelligence', 0, 1, 'C')
        
        self.set_y(260)
        self.set_font('helvetica', 'I', 10)
        self.set_text_color(120, 120, 120)
        self.cell(0, 10, 'Powered by GO-Production Intelligence Engine', 0, 1, 'C')

def sanitize_text(text):
    return "".join(c for c in text if ord(c) < 256)

def generate_pdf(input_file, output_file):
    if not os.path.exists(input_file):
        print(f"Error: {input_file} not found.")
        return

    pdf = GoProductionPDF()
    pdf.cover_page()
    pdf.add_page()
    pdf.set_y(35) # Start below header
    
    epw = pdf.w - 2 * pdf.l_margin

    with open(input_file, 'r', encoding='utf-8') as f:
        lines = f.readlines()

    for line in lines:
        line = sanitize_text(line.strip())
        if not line:
            pdf.ln(5)
            continue
        
        # Reset X position for every line to prevent horizontal drift
        pdf.set_x(pdf.l_margin)

        if line.startswith('---'):
            pdf.ln(8)
            pdf.set_draw_color(220, 220, 220)
            pdf.line(pdf.l_margin, pdf.get_y(), pdf.l_margin + epw, pdf.get_y())
            pdf.ln(8)
            continue

        if line.startswith('# '):
            pdf.ln(10)
            pdf.set_font('helvetica', 'B', 24)
            pdf.set_text_color(*pdf.brand_color)
            pdf.multi_cell(epw, 14, line[2:].upper(), align='L')
            pdf.ln(4)
        elif line.startswith('## '):
            pdf.ln(12)
            pdf.set_font('helvetica', 'B', 18)
            pdf.set_text_color(*pdf.secondary_color)
            pdf.multi_cell(epw, 12, line[3:], align='L')
            pdf.set_draw_color(*pdf.brand_color)
            pdf.line(pdf.l_margin, pdf.get_y(), pdf.l_margin + 50, pdf.get_y())
            pdf.ln(6)
        elif line.startswith('### '):
            pdf.ln(8)
            pdf.set_font('helvetica', 'B', 14)
            pdf.set_text_color(100, 100, 100)
            pdf.multi_cell(epw, 9, line[4:], align='L')
            pdf.ln(2)
        elif line.startswith('- '):
            pdf.set_font('helvetica', '', 11)
            pdf.set_text_color(60, 60, 60)
            # Bullet point with consistent indentation
            pdf.set_x(pdf.l_margin + 5)
            pdf.cell(5, 7, "-", 0, 0) 
            pdf.multi_cell(epw - 5, 7, line[2:], align='L')
        else:
            pdf.set_font('helvetica', '', 11)
            pdf.set_text_color(40, 40, 40)
            # Mixed bold handling
            if '**' in line:
                parts = line.split('**')
                for i, part in enumerate(parts):
                    if i % 2 == 1:
                        pdf.set_font('helvetica', 'B', 11)
                    else:
                        pdf.set_font('helvetica', '', 11)
                    pdf.write(7, part)
                pdf.ln(8)
            else:
                pdf.multi_cell(epw, 7, line, align='L')

    pdf.output(output_file)
    print(f"Successfully generated {output_file}")

if __name__ == "__main__":
    generate_pdf('SYSTEM_FEATURES.md', 'GO_PRODUCTION_FEATURES_AND_BENEFITS.pdf')

