use pdfmake_rust::{Document, DocumentNode, TextNode, PdfMake, PageSize};

#[test]
fn page_size_has_letter_preset() {
    let ps = PageSize::letter();
    assert!((ps.width - 612.0).abs() < 0.1);
    assert!((ps.height - 792.0).abs() < 0.1);
}

#[test]
fn page_size_has_legal_preset() {
    let ps = PageSize::legal();
    assert!((ps.width - 612.0).abs() < 0.1);
    assert!((ps.height - 1008.0).abs() < 0.1);
}

#[test]
fn page_size_has_a3_preset() {
    let ps = PageSize::a3();
    assert!((ps.width - 841.89).abs() < 0.1);
    assert!((ps.height - 1190.55).abs() < 0.1);
}

#[test]
fn page_size_has_a5_preset() {
    let ps = PageSize::a5();
    assert!((ps.width - 419.53).abs() < 0.1);
    assert!((ps.height - 595.28).abs() < 0.1);
}

#[test]
fn page_size_has_b5_preset() {
    let ps = PageSize::b5();
    assert!((ps.width - 498.90).abs() < 0.1);
    assert!((ps.height - 708.66).abs() < 0.1);
}

#[test]
fn landscape_orientation_swaps_dimensions() {
    let doc = Document::builder()
        .page_size(PageSize::a4().landscape())
        .content(DocumentNode::Text(TextNode::new("hello")))
        .build();

    assert!((doc.page_size.width - 841.89).abs() < 0.1);
    assert!((doc.page_size.height - 595.28).abs() < 0.1);
}

#[test]
fn landscape_is_idempotent() {
    let ps = PageSize::a4().landscape().landscape();
    assert!((ps.width - 841.89).abs() < 0.1);
    assert!((ps.height - 595.28).abs() < 0.1);
}

#[test]
fn portrait_restores_dimensions() {
    let ps = PageSize::a4().landscape().portrait();
    assert!((ps.width - 595.28).abs() < 0.1);
    assert!((ps.height - 841.89).abs() < 0.1);
}

#[test]
fn is_landscape_reports_correctly() {
    assert!(!PageSize::a4().is_landscape());
    assert!(PageSize::a4().landscape().is_landscape());
    assert!(!PageSize::letter().is_landscape());
    assert!(PageSize::letter().landscape().is_landscape());
}

#[test]
fn render_landscape_produces_valid_pdf() {
    let doc = Document::builder()
        .page_size(PageSize::a4().landscape())
        .content(DocumentNode::Text(TextNode::new("landscape test")))
        .build();
    let pdf = PdfMake::new().render(&doc).unwrap();
    assert!(pdf.len() > 100);
    let text = String::from_utf8_lossy(&pdf);
    assert!(text.contains("841.89"));
    assert!(text.contains("595.28"));
}
