use pdfmake_rust::{
    Document, DocumentNode, Margins, PageSize, PdfMake, Style, TableCell, TableNode, TextNode,
};
use std::fs;

fn main() {
    let doc = Document::builder()
        .page_size(PageSize::a4())
        .page_margins(Margins::all(40.0))
        .content(DocumentNode::Text(
            TextNode::new("INVOICE").inline_style(Style::new().font_size(24.0).bold(true)),
        ))
        .content(DocumentNode::text("Invoice #12345"))
        .content(DocumentNode::text("Date: 2024-01-15"))
        .content(create_items_table())
        .content(create_total_section())
        .build();

    let pdf = PdfMake::new();
    let bytes = pdf.render(&doc).expect("Failed to render PDF");

    fs::write("examples/pdfs/invoice.pdf", &bytes).expect("Failed to write PDF");
    println!("Generated examples/pdfs/invoice.pdf ({} bytes)", bytes.len());
}

fn create_items_table() -> DocumentNode {
    let header = vec![
        TableCell::text("Item"),
        TableCell::text("Qty"),
        TableCell::text("Price"),
        TableCell::text("Total"),
    ];

    let rows = vec![
        vec![
            TableCell::text("Widget A"),
            TableCell::text("10"),
            TableCell::text("$5.00"),
            TableCell::text("$50.00"),
        ],
        vec![
            TableCell::text("Widget B"),
            TableCell::text("5"),
            TableCell::text("$10.00"),
            TableCell::text("$50.00"),
        ],
    ];

    let mut body = vec![header];
    body.extend(rows);

    DocumentNode::table(TableNode::new(body))
}

fn create_total_section() -> DocumentNode {
    DocumentNode::Text(
        TextNode::new("Total: $100.00").inline_style(Style::new().font_size(18.0).bold(true)),
    )
}
