use pdfmake_rust::{normalize_document, Document, DocumentNode, Margins, NormalizedBlock, PageSize, PdfMakeError, Style, TextNode};

#[test]
fn document_builder_creates_text_document() {
    let document = Document::builder()
        .content(DocumentNode::text("Hello Rust PDF"))
        .build();
    assert_eq!(document.content.len(), 1);
}

#[test]
fn margin_from_single_value_expands_to_all_sides() {
    let margins = Margins::all(40.0);
    assert_eq!((margins.left, margins.top, margins.right, margins.bottom), (40.0, 40.0, 40.0, 40.0));
}

#[test]
fn a4_page_size_matches_pdfmake_points() {
    let page = PageSize::a4();
    assert_eq!((page.width, page.height), (595.28, 841.89));
}

#[test]
fn text_node_stores_text_and_style_names() {
    let node = TextNode::new("Hello").style("headline");
    assert_eq!(node.inlines[0].text, "Hello");
    assert_eq!(node.style_names, vec!["headline"]);
}

#[test]
fn style_builder_sets_font_size_and_bold() {
    let style = Style::new().font_size(18.0).bold(true);
    assert_eq!(style.font_size, Some(18.0));
    assert_eq!(style.bold, Some(true));
}

#[test]
fn named_style_overrides_default_style() {
    let document = Document::builder()
        .default_style(Style::default().font_size(12.0))
        .style("headline", Style::new().font_size(20.0).bold(true))
        .content(DocumentNode::Text(TextNode::new("Title").style("headline")))
        .build();

    let normalized = normalize_document(&document).unwrap();
    match &normalized.blocks[0] {
        NormalizedBlock::Text(block) => {
            assert_eq!(block.style.font_size, 20.0);
            assert_eq!(block.style.bold, true);
        }
        NormalizedBlock::Table(_) => panic!("expected text block"),
        _ => panic!("expected text block"),
    }
}

#[test]
fn unknown_style_returns_error() {
    let document = Document::builder()
        .content(DocumentNode::Text(TextNode::new("Title").style("missing")))
        .build();

    let result = normalize_document(&document);
    match result.unwrap_err() {
        PdfMakeError::UnknownStyle(name) => assert_eq!(name, "missing"),
        other => panic!("expected UnknownStyle, got {other:?}"),
    }
}
