fop-cli 0.1.1

Command-line interface for Apache FOP - XSL-FO to PDF converter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
//! Apache FOP Command-Line Interface
//!
//! This is the main entry point for the FOP CLI tool that converts
//! XSL-FO documents to PDF format.

mod cli;
mod progress;

use anyhow::{Context, Result};
use clap::Parser;
use cli::Cli;
use console::style;
use fop_core::FoTreeBuilder;
use fop_layout::LayoutEngine;
use fop_render::{EncryptionAlgorithm, PdfPermissions, PdfSecurity};
use fop_render::{
    ParallelRenderer, PdfCompliance, PdfRenderer, PdfValidator, PsRenderer, RasterFormat,
    RasterRenderer, SvgRenderer, TextRenderer,
};
use log::{debug, error, info};
use progress::{ProcessingStats, ProgressReporter};
use std::fs;
use std::io::{self, Cursor, Read as _, Write as _};
use std::path::Path;
use std::process::Command;
use std::time::Instant;

fn main() {
    // Parse command-line arguments
    let cli = Cli::parse();

    // Initialize logging based on verbosity
    init_logging(&cli);

    // Run the application and handle errors
    if let Err(err) = run(cli) {
        error!("Error: {}", err);

        // Print error chain
        if let Some(cause) = err.source() {
            eprintln!("\nCaused by:");
            for (i, e) in std::iter::successors(Some(cause), |e| e.source()).enumerate() {
                eprintln!("  {}: {}", i, e);
            }
        }

        std::process::exit(1);
    }
}

/// Initialize logging system
fn init_logging(cli: &Cli) {
    let log_level = if cli.is_verbose() { "debug" } else { "info" };

    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_level))
        .format_timestamp(None)
        .format_module_path(false)
        .init();
}

/// Main application logic
fn run(cli: Cli) -> Result<()> {
    let start_time = Instant::now();

    // Validate arguments
    cli.validate().context("Invalid command-line arguments")?;

    // Handle --version flag
    if cli.show_version {
        print_version_info();
        return Ok(());
    }

    // Print banner
    if !cli.is_quiet() {
        print_banner();
    }

    // Get effective input file
    let input = cli
        .get_input()
        .ok_or_else(|| anyhow::anyhow!("No input file specified"))?;

    // Create progress reporter (disable for stdout output to avoid mixing)
    let show_progress = cli.show_progress() && !cli.is_stdout();
    let mut progress = ProgressReporter::new(show_progress);
    let mut stats = ProcessingStats::new();

    // If XSLT transformation is needed, perform it first
    let input_data = if cli.needs_xslt_transform() {
        perform_xslt_transform(&cli, &mut progress, &mut stats)?
    } else if cli.is_stdin() {
        // Read from stdin
        progress.start_phase("Reading from stdin...");
        let phase_start = Instant::now();

        let mut data = Vec::new();
        io::stdin()
            .read_to_end(&mut data)
            .context("Failed to read from stdin")?;

        stats.input_size = data.len() as u64;
        debug!("Read {} bytes from stdin", data.len());
        progress.finish_phase(
            &format!("Read from stdin ({} bytes)", data.len()),
            phase_start.elapsed(),
        );

        data
    } else {
        // Phase 1: Read input file
        progress.start_phase("Reading input file...");
        let phase_start = Instant::now();

        let data = fs::read(input)
            .with_context(|| format!("Failed to read input file: {}", input.display()))?;

        stats.input_size = data.len() as u64;

        debug!("Read {} bytes from {}", data.len(), input.display());
        progress.finish_phase(
            &format!("Read input file: {}", input.display()),
            phase_start.elapsed(),
        );

        data
    };

    // Phase 2: Parse XSL-FO document
    progress.start_phase("Parsing XSL-FO document...");
    let phase_start = Instant::now();

    let builder = FoTreeBuilder::new();
    let cursor = Cursor::new(&input_data);

    let arena = builder
        .parse(cursor)
        .context("Failed to parse XSL-FO document")?;

    stats.nodes_parsed = arena.len();
    stats.parse_duration = phase_start.elapsed();

    info!("Parsed {} FO nodes", stats.nodes_parsed);
    progress.finish_phase(
        &format!("Parsed FO tree ({} nodes)", stats.nodes_parsed),
        stats.parse_duration,
    );

    // If validation-only mode, stop here
    if cli.validate_only {
        if !cli.is_quiet() {
            println!("\n{} Document is valid!", style("").green().bold());
        }

        if cli.stats {
            stats.total_duration = start_time.elapsed();
            stats.display();
        }

        return Ok(());
    }

    // Phase 3: Layout processing
    progress.start_phase("Processing layout...");
    let phase_start = Instant::now();

    let engine = LayoutEngine::new();
    let area_tree = engine.layout(&arena).context("Failed to process layout")?;

    stats.areas_created = area_tree.len();
    stats.layout_duration = phase_start.elapsed();

    info!("Created {} areas", stats.areas_created);
    progress.finish_phase(
        &format!("Layout complete ({} areas)", stats.areas_created),
        stats.layout_duration,
    );

    // Phase 4: Rendering
    let output = cli
        .get_output()
        .context("Output path not specified; pass an output file path")?;
    let output_format = cli.get_output_format();

    let final_bytes = match output_format {
        cli::OutputFormat::Svg => {
            progress.start_phase("Rendering SVG...");
            let phase_start = Instant::now();

            let renderer = SvgRenderer::new();
            let svg_content = renderer
                .render_to_svg(&area_tree)
                .context("Failed to render SVG")?;

            // Count pages in area tree
            let page_count = area_tree
                .iter()
                .filter(|(_, node)| matches!(node.area.area_type, fop_layout::AreaType::Page))
                .count();
            stats.pages_generated = page_count;

            info!("Generated {} pages", stats.pages_generated);

            let svg_bytes = svg_content.into_bytes();
            stats.output_size = svg_bytes.len() as u64;
            stats.render_duration = phase_start.elapsed();

            progress.finish_phase(
                &format!("SVG rendered ({} pages)", stats.pages_generated),
                stats.render_duration,
            );

            svg_bytes
        }
        cli::OutputFormat::Png => {
            progress.start_phase("Rendering PNG...");
            let phase_start = Instant::now();

            let dpi = cli.dpi.unwrap_or(96);
            let renderer = RasterRenderer::new(dpi);
            let pages = renderer
                .render_to_raster(&area_tree, RasterFormat::Png)
                .context("Failed to render PNG")?;

            stats.pages_generated = pages.len();
            info!("Generated {} page(s)", stats.pages_generated);

            // For multi-page documents, save each page separately
            if pages.len() > 1 {
                let output_path = output;
                let base_name = output_path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("output");
                let parent = output_path
                    .parent()
                    .unwrap_or_else(|| std::path::Path::new("."));

                for (i, page_data) in pages.iter().enumerate() {
                    let page_path = parent.join(format!("{}-page{}.png", base_name, i + 1));
                    fs::write(&page_path, page_data)
                        .with_context(|| format!("Failed to write page {}", i + 1))?;
                    info!("Saved page {} to {}", i + 1, page_path.display());
                }

                stats.output_size = pages.iter().map(|p| p.len() as u64).sum();
                stats.render_duration = phase_start.elapsed();

                progress.finish_phase(
                    &format!("PNG rendered ({} pages)", stats.pages_generated),
                    stats.render_duration,
                );

                // Return the first page for final stats
                pages.into_iter().next().unwrap_or_default()
            } else {
                let page_data = pages.into_iter().next().unwrap_or_default();
                stats.output_size = page_data.len() as u64;
                stats.render_duration = phase_start.elapsed();

                progress.finish_phase(
                    &format!("PNG rendered ({} pages)", stats.pages_generated),
                    stats.render_duration,
                );

                page_data
            }
        }
        cli::OutputFormat::Jpeg => {
            progress.start_phase("Rendering JPEG...");
            let phase_start = Instant::now();

            let dpi = cli.dpi.unwrap_or(96);
            let quality = cli.jpeg_quality;
            let renderer = RasterRenderer::new(dpi);
            let pages = renderer
                .render_to_raster(&area_tree, RasterFormat::Jpeg { quality })
                .context("Failed to render JPEG")?;

            stats.pages_generated = pages.len();
            info!("Generated {} page(s)", stats.pages_generated);

            // For multi-page documents, save each page separately
            if pages.len() > 1 {
                let output_path = output;
                let base_name = output_path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("output");
                let parent = output_path
                    .parent()
                    .unwrap_or_else(|| std::path::Path::new("."));

                for (i, page_data) in pages.iter().enumerate() {
                    let page_path = parent.join(format!("{}-page{}.jpg", base_name, i + 1));
                    fs::write(&page_path, page_data)
                        .with_context(|| format!("Failed to write page {}", i + 1))?;
                    info!("Saved page {} to {}", i + 1, page_path.display());
                }

                stats.output_size = pages.iter().map(|p| p.len() as u64).sum();
                stats.render_duration = phase_start.elapsed();

                progress.finish_phase(
                    &format!("JPEG rendered ({} pages)", stats.pages_generated),
                    stats.render_duration,
                );

                // Return the first page for final stats
                pages.into_iter().next().unwrap_or_default()
            } else {
                let page_data = pages.into_iter().next().unwrap_or_default();
                stats.output_size = page_data.len() as u64;
                stats.render_duration = phase_start.elapsed();

                progress.finish_phase(
                    &format!("JPEG rendered ({} pages)", stats.pages_generated),
                    stats.render_duration,
                );

                page_data
            }
        }
        cli::OutputFormat::PostScript => {
            progress.start_phase("Rendering PostScript...");
            let phase_start = Instant::now();

            let renderer = PsRenderer::new();
            let ps_content = renderer
                .render_to_ps(&area_tree)
                .context("Failed to render PostScript")?;

            // Count pages in area tree
            let page_count = area_tree
                .iter()
                .filter(|(_, node)| matches!(node.area.area_type, fop_layout::AreaType::Page))
                .count();
            stats.pages_generated = page_count;

            info!("Generated {} pages", stats.pages_generated);

            let ps_bytes = ps_content.into_bytes();
            stats.output_size = ps_bytes.len() as u64;
            stats.render_duration = phase_start.elapsed();

            progress.finish_phase(
                &format!("PostScript rendered ({} pages)", stats.pages_generated),
                stats.render_duration,
            );

            ps_bytes
        }
        cli::OutputFormat::Text => {
            progress.start_phase("Rendering text...");
            let phase_start = Instant::now();

            let renderer = TextRenderer::new();
            let text_content = renderer
                .render_to_text(&area_tree)
                .context("Failed to render text")?;

            // Count pages in area tree
            let page_count = area_tree
                .iter()
                .filter(|(_, node)| matches!(node.area.area_type, fop_layout::AreaType::Page))
                .count();
            stats.pages_generated = page_count;

            info!("Generated {} pages", stats.pages_generated);

            let text_bytes = text_content.into_bytes();
            stats.output_size = text_bytes.len() as u64;
            stats.render_duration = phase_start.elapsed();

            progress.finish_phase(
                &format!("Text rendered ({} pages)", stats.pages_generated),
                stats.render_duration,
            );

            text_bytes
        }
        _ => {
            // Default to PDF rendering
            progress.start_phase("Rendering PDF...");
            let phase_start = Instant::now();

            // Use parallel renderer if --jobs is specified and > 1
            let mut pdf_doc = if let Some(jobs) = cli.jobs {
                if jobs > 1 && area_tree.len() > 1 {
                    info!("Using parallel renderer with {} threads", jobs);
                    let renderer = ParallelRenderer::new(jobs);
                    renderer
                        .render(&area_tree)
                        .context("Failed to render PDF (parallel)")?
                } else {
                    let renderer = PdfRenderer::new();
                    renderer
                        .render(&area_tree)
                        .context("Failed to render PDF")?
                }
            } else {
                let renderer = PdfRenderer::new();
                renderer
                    .render(&area_tree)
                    .context("Failed to render PDF")?
            };

            stats.pages_generated = pdf_doc.pages.len();

            info!("Generated {} pages", stats.pages_generated);
            progress.update_message(&format!(
                "Rendering PDF ({} pages)...",
                stats.pages_generated
            ));

            // Apply compliance mode (PDF/A-1b and/or PDF/UA-1) if requested
            let compliance = match (cli.pdfa, cli.pdfua) {
                (true, true) => Some(PdfCompliance::PdfA1bUA1),
                (true, false) => Some(PdfCompliance::PdfA1b),
                (false, true) => Some(PdfCompliance::PdfUA1),
                (false, false) => None,
            };
            if let Some(c) = compliance {
                pdf_doc
                    .set_compliance(c)
                    .context("Failed to apply PDF compliance mode")?;
                info!("PDF compliance mode: {:?}", c);
            }

            // Apply PDF encryption before serialization if security options are set
            if cli.has_security() {
                let permissions = PdfPermissions {
                    allow_print: !cli.noprint,
                    allow_copy: !cli.nocopy,
                    allow_modify: !cli.noedit,
                    allow_annotations: !cli.noannotations,
                    ..Default::default()
                };

                let owner_pwd = cli.owner_password.as_deref().unwrap_or("");
                let user_pwd = cli.user_password.as_deref().unwrap_or("");

                // Select encryption algorithm from CLI flag
                let security = match cli.encryption.as_str() {
                    "aes256" => PdfSecurity::new_aes256(owner_pwd, user_pwd, permissions),
                    _ => {
                        // Default: rc4
                        PdfSecurity::new(owner_pwd, user_pwd, permissions)
                    }
                };

                let algo_name = match security.algorithm {
                    EncryptionAlgorithm::Aes256 => "AES-256",
                    EncryptionAlgorithm::Rc4128 => "RC4-128",
                };

                let file_id = fop_render::pdf::security::generate_file_id(&format!(
                    "fop-{}-{}",
                    env!("CARGO_PKG_VERSION"),
                    stats.pages_generated
                ));

                let encryption_dict = security.compute_encryption_dict(&file_id);

                info!(
                    "PDF encryption applied ({}, permissions: P={})",
                    algo_name, encryption_dict.p_value
                );

                pdf_doc
                    .set_encryption(encryption_dict, file_id)
                    .context("Failed to apply PDF encryption")?;
            }

            // Serialize PDF
            progress.update_message("Serializing PDF...");

            let pdf_bytes = pdf_doc.to_bytes().context("Failed to serialize PDF")?;

            stats.output_size = pdf_bytes.len() as u64;
            stats.render_duration = phase_start.elapsed();

            progress.finish_phase(
                &format!("PDF rendered ({} pages)", stats.pages_generated),
                stats.render_duration,
            );

            // Handle other output formats
            match output_format {
                cli::OutputFormat::Pdf => pdf_bytes,
                cli::OutputFormat::Pcl => {
                    if !cli.is_quiet() {
                        eprintln!(
                            "Warning: PCL output not yet implemented, generating PDF instead"
                        );
                    }
                    pdf_bytes
                }
                cli::OutputFormat::Custom => pdf_bytes,
                cli::OutputFormat::Svg
                | cli::OutputFormat::Text
                | cli::OutputFormat::PostScript
                | cli::OutputFormat::Png
                | cli::OutputFormat::Jpeg => unreachable!(),
            }
        }
    };

    // Phase 5: Write output
    if cli.is_stdout() {
        progress.start_phase("Writing to stdout...");
        let phase_start = Instant::now();

        io::stdout()
            .write_all(&final_bytes)
            .context("Failed to write to stdout")?;
        io::stdout().flush().context("Failed to flush stdout")?;

        progress.finish_phase("Written to stdout", phase_start.elapsed());
    } else {
        progress.start_phase("Writing output file...");
        let phase_start = Instant::now();

        fs::write(output, &final_bytes)
            .with_context(|| format!("Failed to write output file: {}", output.display()))?;

        progress.finish_phase(
            &format!("Saved to {}", output.display()),
            phase_start.elapsed(),
        );
    }

    // Phase 6: Validate PDF if requested
    if cli.validate && matches!(output_format, cli::OutputFormat::Pdf) {
        progress.start_phase("Validating PDF...");
        let phase_start = Instant::now();

        let validator = if cli.strict {
            PdfValidator::new_strict()
        } else {
            PdfValidator::new()
        };

        let validation_result = validator.validate_pdf(&final_bytes);

        progress.finish_phase("PDF validation complete", phase_start.elapsed());

        // Display validation results
        if !cli.is_quiet() {
            match &validation_result {
                fop_render::ValidationResult::Valid => {
                    println!("\n{} PDF validation passed!", style("").green().bold());
                }
                fop_render::ValidationResult::Warning(warnings) => {
                    println!(
                        "\n{} PDF validation passed with warnings:",
                        style("").yellow().bold()
                    );
                    for warning in warnings {
                        println!("{}", style(warning).yellow());
                    }
                }
                fop_render::ValidationResult::Error(errors) => {
                    println!("\n{} PDF validation failed:", style("").red().bold());
                    for error in errors {
                        eprintln!("{}", style(error).red());
                    }
                }
            }
        }

        // JSON output format
        if cli.output_format == "json" {
            let json_result = match &validation_result {
                fop_render::ValidationResult::Valid => {
                    serde_json::json!({
                        "status": "valid",
                        "issues": []
                    })
                }
                fop_render::ValidationResult::Warning(warnings) => {
                    serde_json::json!({
                        "status": "warning",
                        "issues": warnings
                    })
                }
                fop_render::ValidationResult::Error(errors) => {
                    serde_json::json!({
                        "status": "error",
                        "issues": errors
                    })
                }
            };
            println!(
                "{}",
                serde_json::to_string_pretty(&json_result)
                    .context("Failed to serialize validation result to JSON")?
            );
        }

        // Exit with error code if validation failed
        if validation_result.has_errors() || (cli.strict && !validation_result.is_ok()) {
            std::process::exit(1);
        }
    }

    // Phase 7: Render-verify — re-parse and rasterize the generated PDF as a self-check
    if cli.render_verify && matches!(output_format, cli::OutputFormat::Pdf) {
        progress.start_phase("Render-verifying PDF...");
        let phase_start = Instant::now();

        let dpi = cli.dpi.map(|d| d as f32).unwrap_or(72.0_f32);

        match fop_pdf_renderer::PdfRenderer::from_bytes(&final_bytes) {
            Ok(renderer) => {
                let page_count = renderer.page_count();
                if !cli.is_quiet() {
                    eprintln!("Render-verify: {} page(s)", page_count);
                }
                for i in 0..page_count {
                    match renderer.render_page(i, dpi) {
                        Ok(page) => {
                            if !cli.is_quiet() {
                                eprintln!("  Page {}: {}x{} px", i + 1, page.width, page.height);
                            }
                        }
                        Err(e) => {
                            eprintln!("render-verify: page {} rasterization failed: {}", i, e);
                            std::process::exit(1);
                        }
                    }
                }
                progress.finish_phase(
                    &format!("Render-verify complete ({} pages)", page_count),
                    phase_start.elapsed(),
                );
            }
            Err(e) => {
                eprintln!("render-verify: failed to parse generated PDF: {}", e);
                std::process::exit(1);
            }
        }
    }
    // Non-PDF: silently no-op

    // Final summary
    stats.total_duration = start_time.elapsed();

    if !cli.is_quiet() && !cli.is_stdout() {
        print_success_summary(&cli, &stats, output);
    }

    // Display statistics if requested
    if cli.stats {
        if cli.output_format == "json" {
            stats.display_json();
        } else {
            stats.display();
        }
    }

    Ok(())
}

/// Print detailed version information
fn print_version_info() {
    println!("Apache FOP (Formatting Objects Processor) Rust Implementation");
    println!("Version: {}", env!("CARGO_PKG_VERSION"));
    println!("Edition: Rust 2021");
    println!();
    println!("Supported output formats:");
    println!("  PDF       (Portable Document Format)");
    println!("  SVG       (Scalable Vector Graphics)");
    println!("  PS        (PostScript)");
    println!("  PNG       (Portable Network Graphics)");
    println!("  JPEG      (Joint Photographic Experts Group)");
    println!("  TXT       (Plain Text)");
    println!();
    println!("Features:");
    println!("  - XSL-FO 1.1 compliance (29 elements, 294 properties)");
    println!("  - Knuth-Plass line breaking algorithm");
    println!("  - i18n support (16+ languages, automatic font fallback)");
    println!("  - PDF encryption (RC4-128, AES-256)");
    println!("  - Font embedding (Type 0 composite fonts, CIDFontType2)");
}

/// Print application banner
fn print_banner() {
    println!("{}", style("Apache FOP").cyan().bold());
    println!("{}", style("Rust Implementation").dim());
    println!();
}

/// Transform XML using XSLT stylesheet via xsltproc command
fn transform_with_xsltproc(
    xml_file: &Path,
    xsl_file: &Path,
    params: &[(&str, &str)],
) -> Result<Vec<u8>> {
    debug!("Using xsltproc for XSLT transformation");

    let mut cmd = Command::new("xsltproc");

    // Add parameters
    for (name, value) in params {
        cmd.arg("--stringparam");
        cmd.arg(name);
        cmd.arg(value);
    }

    // Add stylesheet and input file
    cmd.arg(xsl_file);
    cmd.arg(xml_file);

    // Execute the command
    let output = cmd
        .output()
        .with_context(|| "Failed to execute xsltproc. Please ensure xsltproc is installed.")?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("xsltproc failed: {}", stderr);
    }

    Ok(output.stdout)
}

/// Perform XSLT transformation
fn perform_xslt_transform(
    cli: &Cli,
    progress: &mut ProgressReporter,
    stats: &mut ProcessingStats,
) -> Result<Vec<u8>> {
    progress.start_phase("Performing XSLT transformation...");
    let phase_start = Instant::now();

    let xml_file = cli
        .xml
        .as_ref()
        .context("XML input file not specified; use --xml <file>")?;
    let xsl_file = cli
        .xsl
        .as_ref()
        .context("XSL stylesheet not specified; use --xsl <file>")?;

    debug!("XML input: {}", xml_file.display());
    debug!("XSLT stylesheet: {}", xsl_file.display());

    // Get the XML file size for stats
    let xml_metadata = fs::metadata(xml_file)
        .with_context(|| format!("Failed to read XML file metadata: {}", xml_file.display()))?;
    stats.input_size = xml_metadata.len();

    // Validate XSLT file is readable
    fs::metadata(xsl_file)
        .with_context(|| format!("Failed to read XSLT file: {}", xsl_file.display()))?;

    // Log XSLT parameters
    let params = cli.get_xslt_params();
    if !params.is_empty() {
        debug!("XSLT parameters:");
        for (name, value) in &params {
            debug!("  {} = {}", name, value);
        }
    }

    // Perform XSLT transformation using xsltproc
    let transformed_data = transform_with_xsltproc(xml_file, xsl_file, &params)
        .with_context(|| "Failed to perform XSLT transformation")?;

    progress.finish_phase("XSLT transformation complete", phase_start.elapsed());

    Ok(transformed_data)
}

/// Print success summary
fn print_success_summary(cli: &Cli, stats: &ProcessingStats, output: &Path) {
    println!();
    println!(
        "{} {}",
        style("").green().bold(),
        style("Success!").green().bold()
    );
    println!();

    let input = cli
        .get_input()
        .expect("invariant: input path must be set before print_success_summary is called");
    println!("  Input:   {}", input.display());
    println!("  Output:  {}", output.display());
    println!(
        "  Size:    {}{}",
        format_bytes(stats.input_size),
        format_bytes(stats.output_size)
    );
    println!("  Pages:   {}", stats.pages_generated);
    println!("  Time:    {}", format_duration(stats.total_duration));

    println!();
}

/// Format bytes in human-readable form
fn format_bytes(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes < KB {
        format!("{} B", bytes)
    } else if bytes < MB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else if bytes < GB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    }
}

/// Format duration in human-readable form
fn format_duration(duration: std::time::Duration) -> String {
    let millis = duration.as_millis();
    if millis < 1000 {
        format!("{}ms", millis)
    } else if millis < 60_000 {
        format!("{:.2}s", duration.as_secs_f64())
    } else {
        let mins = millis / 60_000;
        let secs = (millis % 60_000) / 1000;
        format!("{}m {}s", mins, secs)
    }
}

/// Process an XSL-FO document string and return rendered output bytes.
///
/// This is a thin wrapper around the parse → layout → render pipeline that
/// makes the individual pipeline stages testable without invoking the full CLI.
pub fn process_document(fo_xml: &str, format: cli::OutputFormat) -> Result<Vec<u8>> {
    use fop_render::{PsRenderer, SvgRenderer, TextRenderer};
    use std::io::Cursor;

    let builder = FoTreeBuilder::new();
    let cursor = Cursor::new(fo_xml.as_bytes());
    let arena = builder.parse(cursor).context("Failed to parse XSL-FO")?;

    let engine = LayoutEngine::new();
    let area_tree = engine.layout(&arena).context("Failed to layout")?;

    let bytes = match format {
        cli::OutputFormat::Svg => {
            let renderer = SvgRenderer::new();
            renderer
                .render_to_svg(&area_tree)
                .context("Failed to render SVG")?
                .into_bytes()
        }
        cli::OutputFormat::PostScript => {
            let renderer = PsRenderer::new();
            renderer
                .render_to_ps(&area_tree)
                .context("Failed to render PostScript")?
                .into_bytes()
        }
        cli::OutputFormat::Text => {
            let renderer = TextRenderer::new();
            renderer
                .render_to_text(&area_tree)
                .context("Failed to render text")?
                .into_bytes()
        }
        _ => {
            // Default: PDF
            let renderer = fop_render::PdfRenderer::new();
            let pdf_doc = renderer
                .render_with_fo(&area_tree, &arena)
                .context("Failed to render PDF")?;
            pdf_doc.to_bytes().context("Failed to serialize PDF")?
        }
    };

    Ok(bytes)
}

#[cfg(test)]
mod tests {
    use super::*;

    // ------------------------------------------------------------------
    // Utility function tests
    // ------------------------------------------------------------------

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(2048), "2.00 KB");
        assert_eq!(format_bytes(1_572_864), "1.50 MB");
    }

    #[test]
    fn test_format_bytes_gb() {
        assert_eq!(format_bytes(1_073_741_824), "1.00 GB");
    }

    #[test]
    fn test_format_bytes_zero() {
        assert_eq!(format_bytes(0), "0 B");
    }

    #[test]
    fn test_format_duration() {
        use std::time::Duration;

        assert_eq!(format_duration(Duration::from_millis(500)), "500ms");
        assert_eq!(format_duration(Duration::from_millis(2500)), "2.50s");
        assert_eq!(format_duration(Duration::from_secs(90)), "1m 30s");
    }

    #[test]
    fn test_format_duration_exact_seconds() {
        use std::time::Duration;
        assert_eq!(format_duration(Duration::from_secs(1)), "1.00s");
        assert_eq!(format_duration(Duration::from_millis(999)), "999ms");
    }

    #[test]
    fn test_format_duration_long() {
        use std::time::Duration;
        // 2 minutes 5 seconds
        assert_eq!(format_duration(Duration::from_secs(125)), "2m 5s");
    }

    // ------------------------------------------------------------------
    // process_document: PDF output
    // ------------------------------------------------------------------

    const MINIMAL_FO: &str = r#"<?xml version="1.0"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>Hello, FOP pipeline!</fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"#;

    #[test]
    fn test_process_document_pdf() {
        let result = process_document(MINIMAL_FO, cli::OutputFormat::Pdf);
        assert!(result.is_ok(), "PDF render failed: {:?}", result.err());
        let bytes = result.expect("test: should succeed");
        assert!(!bytes.is_empty(), "PDF output must not be empty");
        assert!(
            bytes.starts_with(b"%PDF-"),
            "Output must start with PDF header"
        );
    }

    #[test]
    fn test_process_document_svg() {
        let result = process_document(MINIMAL_FO, cli::OutputFormat::Svg);
        assert!(result.is_ok(), "SVG render failed: {:?}", result.err());
        let bytes = result.expect("test: should succeed");
        assert!(!bytes.is_empty(), "SVG output must not be empty");
        let text = String::from_utf8_lossy(&bytes);
        assert!(
            text.contains("<svg"),
            "SVG output must contain <svg element"
        );
    }

    #[test]
    fn test_process_document_text() {
        let result = process_document(MINIMAL_FO, cli::OutputFormat::Text);
        assert!(result.is_ok(), "Text render failed: {:?}", result.err());
        let bytes = result.expect("test: should succeed");
        assert!(!bytes.is_empty(), "Text output must not be empty");
        // The text output should contain our content
        let text = String::from_utf8_lossy(&bytes);
        assert!(
            text.contains("Hello"),
            "Text output should contain document text"
        );
    }

    #[test]
    fn test_process_document_postscript() {
        let result = process_document(MINIMAL_FO, cli::OutputFormat::PostScript);
        assert!(
            result.is_ok(),
            "PostScript render failed: {:?}",
            result.err()
        );
        let bytes = result.expect("test: should succeed");
        assert!(!bytes.is_empty(), "PostScript output must not be empty");
        let text = String::from_utf8_lossy(&bytes);
        assert!(
            text.contains("%!PS-Adobe"),
            "PostScript must start with PS header"
        );
    }

    #[test]
    fn test_process_document_invalid_xml() {
        let invalid_fo = "this is not XML at all <<<<";
        let result = process_document(invalid_fo, cli::OutputFormat::Pdf);
        assert!(result.is_err(), "Invalid XML should return an error");
    }

    #[test]
    fn test_process_document_multipage_pdf() {
        let fo = r#"<?xml version="1.0"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4"
      page-width="210mm" page-height="297mm"
      margin-top="20mm" margin-bottom="20mm"
      margin-left="20mm" margin-right="20mm">
      <fo:region-body/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block>Page one content.</fo:block>
      <fo:block break-before="page">Page two content.</fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>"#;
        let result = process_document(fo, cli::OutputFormat::Pdf);
        assert!(result.is_ok(), "Multi-page PDF failed: {:?}", result.err());
        let bytes = result.expect("test: should succeed");
        assert!(bytes.starts_with(b"%PDF-"), "Must be valid PDF");
    }

    // ------------------------------------------------------------------
    // Pipe-mode: stdin → stdout simulation (process_document is the testable core)
    // ------------------------------------------------------------------

    #[test]
    fn test_pipe_mode_pdf_bytes_non_empty() {
        // Simulates: echo FO | fop - - (stdin → stdout)
        // We test this by directly exercising process_document since
        // actual stdin/stdout piping is covered by CLI integration tests.
        let result = process_document(MINIMAL_FO, cli::OutputFormat::Pdf);
        assert!(result.is_ok());
        let bytes = result.expect("test: should succeed");
        // Verify it looks like a real PDF (has xref and trailer)
        let text = String::from_utf8_lossy(&bytes);
        assert!(
            text.contains("xref") || text.contains("%%EOF"),
            "PDF must have xref/EOF marker"
        );
    }
}