ppt-rs 0.2.19

Create, read, and update PowerPoint 2007+ (.pptx) files with rich formatting, bullet styles, themes, and templates.
Documentation
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
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
//! PPTX builder - orchestrates ZIP creation and file writing

use std::io::{Write, Seek, Cursor};
use zip::write::FileOptions;
use zip::ZipWriter;
use crate::exc::Result;
use crate::core::append_usize;
use super::slide_content::SlideContent;
use super::memory_profile::estimate_output_capacity;
use super::package_cache::{self, print_affects_theme_parts};
use super::package_xml::{
    create_rels_xml, create_presentation_rels_xml_full, create_presentation_xml,
    create_content_types_xml_with_notes_and_charts,
    content_types_opening,
    create_pres_props_xml, create_view_props_xml, create_table_styles_xml,
    create_handout_master_rels_xml,
};
use super::slide_xml::{
    create_slide_xml, create_slide_xml_with_content, create_slide_rels_xml,
};
use super::theme_xml::{
    create_slide_master_xml, create_master_rels_xml, create_theme_xml, create_layout_rels_xml,
};
use super::layout_parts::{create_slide_layout_xml, STANDARD_LAYOUT_COUNT};
use super::template::PptxTemplate;
use super::props_xml::{create_core_props_xml, create_app_props_xml};
use super::notes_xml::*;
use crate::generator::presentation_theme::office_theme_xml;
use crate::generator::charts::{
    chart_embedding_filename, create_chart_rels_xml, generate_chart_part_xml,
    reference_workbook_bytes,
};
use crate::generator::slide_content::print_settings::PrintWhat;
use crate::generator::slide_content::presentation_settings::PresentationSettings;
use super::media_registry::MediaRegistry;

fn zip_options() -> FileOptions {
    FileOptions::default()
}

/// First relationship id after layout (rId1) and optional notes slide.
fn slide_content_rel_start(has_notes: bool, image_count: usize) -> usize {
    2 + usize::from(has_notes) + image_count
}

fn build_media_registry(slides: &[SlideContent]) -> MediaRegistry {
    let mut registry = MediaRegistry::default();
    for slide in slides {
        for image in &slide.images {
            if let Some(bytes) = image.get_bytes() {
                registry.image_number(&bytes, &image.extension());
            }
        }
    }
    registry
}

fn build_media_registry_lazy(slides: &dyn LazySlideSource) -> MediaRegistry {
    let mut registry = MediaRegistry::default();
    for i in 0..slides.slide_count() {
        if let Some(slide) = slides.generate_slide(i) {
            for image in &slide.images {
                if let Some(bytes) = image.get_bytes() {
                    registry.image_number(&bytes, &image.extension());
                }
            }
        }
    }
    registry
}

fn slide_image_rel_targets(slide: &SlideContent, registry: &MediaRegistry) -> Vec<(usize, String)> {
    let mut images = Vec::with_capacity(slide.images.len());
    for image in &slide.images {
        if let Some(bytes) = image.get_bytes() {
            if let Some(num) = registry.lookup_number(&bytes, &image.extension()) {
                images.push((num, image.extension()));
            }
        }
    }
    images
}

/// Collect hyperlink relationship XML (`<Relationship .../>`) for every shape on
/// the slide whose hyperlink has an assigned relationship id. The slide XML
/// references these ids via `<a:hlinkClick r:id="..."/>`, so the matching
/// relationship must be present in the slide's `.rels` part.
fn slide_hyperlink_relationships(slide: &SlideContent) -> Vec<String> {
    slide
        .shapes
        .iter()
        .filter_map(|s| s.hyperlink.as_ref())
        .filter_map(|h| {
            h.r_id
                .as_ref()
                .map(|rid| crate::generator::generate_hyperlink_relationship_xml(h, rid))
        })
        .collect()
}

fn set_notes_part_path(path: &mut String, notes_part_num: usize) {
    path.clear();
    path.push_str("ppt/notesSlides/notesSlide");
    append_usize(path, notes_part_num);
    path.push_str(".xml");
}

#[cfg(debug_assertions)]
fn debug_assert_package_valid(bytes: &[u8]) {
    use crate::core::validate_package_bytes;
    let report = validate_package_bytes(bytes);
    debug_assert!(
        report.is_valid(),
        "generated PPTX failed package validation: {:?}",
        report.error_messages()
    );
}

#[cfg(not(debug_assertions))]
fn debug_assert_package_valid(_bytes: &[u8]) {}

/// Create a minimal but valid PPTX file
pub fn create_pptx(title: &str, slides: usize) -> Result<Vec<u8>> {
    let buffer = Vec::with_capacity(slides.saturating_mul(6_000).max(8_192));
    let cursor = Cursor::new(buffer);
    let mut zip = ZipWriter::new(cursor);
    let options = zip_options();

    write_package_files(&mut zip, &options, title, slides, None, None)?;

    let cursor = zip.finish()?;
    let bytes = cursor.into_inner();
    debug_assert_package_valid(&bytes);
    Ok(bytes)
}

/// Create a PPTX file with custom slide content
pub fn create_pptx_with_content(
    title: &str,
    slides: Vec<SlideContent>,
) -> Result<Vec<u8>> {
    create_pptx_with_settings(title, &slides, None)
}

/// Create a PPTX file with custom slide content, settings, and optional template deck.
pub fn create_pptx_with_template(
    title: &str,
    slides: &[SlideContent],
    template_path: &str,
    settings: Option<PresentationSettings>,
) -> Result<Vec<u8>> {
    let mut merged = settings.unwrap_or_default();
    merged.template_path = Some(template_path.to_string());
    create_pptx_with_settings(title, slides, Some(merged))
}

/// Resolve layout part index for a slide (respects template layout count).
fn resolve_layout_number(slide: &SlideContent, template: Option<&PptxTemplate>) -> usize {
    let requested = slide.layout.layout_number();
    template
        .map(|t| t.resolve_layout_number(requested))
        .unwrap_or(requested)
}

fn load_template(settings: Option<&PresentationSettings>) -> Result<Option<PptxTemplate>> {
    if let Some(path) = settings.and_then(|s| s.template_path.as_deref()) {
        Ok(Some(PptxTemplate::load(path)?))
    } else {
        Ok(None)
    }
}

/// Create a PPTX file with custom slide content and presentation-level settings
pub fn create_pptx_with_settings(
    title: &str,
    slides: &[SlideContent],
    settings: Option<PresentationSettings>,
) -> Result<Vec<u8>> {
    let buffer = Vec::with_capacity(estimate_output_capacity(slides.len(), Some(slides)));
    let cursor = Cursor::new(buffer);
    let mut zip = ZipWriter::new(cursor);
    let options = zip_options();

    write_package_files(&mut zip, &options, title, slides.len(), Some(slides), settings.as_ref())?;

    let cursor = zip.finish()?;
    let bytes = cursor.into_inner();
    debug_assert_package_valid(&bytes);
    Ok(bytes)
}

/// Create a PPTX file and write it directly to a writer (streaming API).
/// This is more memory-efficient for large presentations as it avoids
/// buffering the entire ZIP file in memory.
///
/// # Example
/// ```rust,no_run
/// use std::fs::File;
/// use ppt_rs::create_pptx_to_writer;
///
/// let file = File::create("output.pptx")?;
/// create_pptx_to_writer(file, "My Presentation", 10)?;
/// # Ok::<(), ppt_rs::PptxError>(())
/// ```
pub fn create_pptx_to_writer<W: Write + Seek>(
    writer: W,
    title: &str,
    slides: usize,
) -> Result<W> {
    let mut zip = ZipWriter::new(writer);
    let options = FileOptions::default();

    write_package_files(&mut zip, &options, title, slides, None, None)?;

    Ok(zip.finish()?)
}

/// Create a PPTX file with custom content and write it directly to a writer (streaming API).
/// This is more memory-efficient for large presentations.
///
/// # Example
/// ```rust,no_run
/// use std::fs::File;
/// use ppt_rs::{create_pptx_with_content_to_writer, SlideContent};
///
/// let file = File::create("output.pptx")?;
/// let slides = vec![
///     SlideContent::new("Title").add_bullet("Point 1"),
///     SlideContent::new("Slide 2").add_bullet("Point 2"),
/// ];
/// create_pptx_with_content_to_writer(file, "My Presentation", &slides, None)?;
/// # Ok::<(), ppt_rs::PptxError>(())
/// ```
pub fn create_pptx_with_content_to_writer<W: Write + Seek>(
    writer: W,
    title: &str,
    slides: &[SlideContent],
    settings: Option<PresentationSettings>,
) -> Result<W> {
    let mut zip = ZipWriter::new(writer);
    let options = FileOptions::default();

    write_package_files(&mut zip, &options, title, slides.len(), Some(slides), settings.as_ref())?;

    Ok(zip.finish()?)
}

/// Lazy slide source - allows generating slides on-demand instead of all at once.
/// This is useful for:
/// - Very large presentations that don't fit in memory
/// - Dynamically generated slide content
/// - Streaming data sources
///
/// # Example
/// ```rust,no_run
/// use std::fs::File;
/// use ppt_rs::{create_pptx_lazy_to_writer, LazySlideSource, SlideContent};
///
/// struct MySlideGenerator {
///     count: usize,
/// }
///
/// impl LazySlideSource for MySlideGenerator {
///     fn slide_count(&self) -> usize {
///         self.count
///     }
///
///     fn generate_slide(&self, index: usize) -> Option<SlideContent> {
///         if index < self.count {
///             Some(SlideContent::new(&format!("Slide {}", index + 1))
///                 .add_bullet(&format!("Content {}", index + 1)))
///         } else {
///             None
///         }
///     }
/// }
///
/// let file = File::create("output.pptx")?;
/// create_pptx_lazy_to_writer(file, "My Presentation", Box::new(MySlideGenerator { count: 100 }), None)?;
/// # Ok::<(), ppt_rs::PptxError>(())
/// ```
pub trait LazySlideSource {
    /// Return the total number of slides
    fn slide_count(&self) -> usize;

    /// Generate a slide by index (0-based). Return None if index is out of bounds.
    fn generate_slide(&self, index: usize) -> Option<SlideContent>;

    /// Notes and chart count from a single slide generation when both are needed.
    fn slide_features(&self, index: usize) -> Option<(bool, usize)> {
        self.generate_slide(index)
            .map(|s| (s.notes.is_some(), s.charts.len()))
    }

    /// Check if a slide has notes (default implementation checks the generated slide)
    fn slide_has_notes(&self, index: usize) -> bool {
        self.slide_features(index)
            .map(|(notes, _)| notes)
            .unwrap_or(false)
    }

    /// Get the number of charts in a slide (default implementation checks the generated slide)
    fn slide_chart_count(&self, index: usize) -> usize {
        self.slide_features(index)
            .map(|(_, charts)| charts)
            .unwrap_or(0)
    }
}

/// Create a PPTX file using lazy slide generation and write it directly to a writer.
/// This is the most memory-efficient API for large presentations.
///
/// # Example
/// ```rust,no_run
/// use std::fs::File;
/// use ppt_rs::{create_pptx_lazy_to_writer, LazySlideSource, SlideContent};
///
/// struct MySlideGenerator {
///     count: usize,
/// }
///
/// impl LazySlideSource for MySlideGenerator {
///     fn slide_count(&self) -> usize {
///         self.count
///     }
///
///     fn generate_slide(&self, index: usize) -> Option<SlideContent> {
///         if index < self.count {
///             Some(SlideContent::new(&format!("Slide {}", index + 1))
///                 .add_bullet(&format!("Content {}", index + 1)))
///         } else {
///             None
///         }
///     }
/// }
///
/// let file = File::create("output.pptx")?;
/// let generator = Box::new(MySlideGenerator { count: 1000 });
/// create_pptx_lazy_to_writer(file, "Large Presentation", generator, None)?;
/// # Ok::<(), ppt_rs::PptxError>(())
/// ```
pub fn create_pptx_lazy_to_writer<W: Write + Seek>(
    writer: W,
    title: &str,
    slides: Box<dyn LazySlideSource>,
    settings: Option<PresentationSettings>,
) -> Result<W> {
    let mut zip = ZipWriter::new(writer);
    let options = FileOptions::default();

    write_package_files_lazy(&mut zip, &options, title, slides.as_ref(), settings.as_ref())?;

    Ok(zip.finish()?)
}

/// Chart metadata for slides
struct ChartInfo {
    total_charts: usize,
    slide_start_indices: Vec<usize>,
}

fn set_slide_xml_path(path: &mut String, slide_num: usize) {
    path.clear();
    path.push_str("ppt/slides/slide");
    append_usize(path, slide_num);
    path.push_str(".xml");
}

fn set_slide_rels_path(path: &mut String, slide_num: usize) {
    path.clear();
    path.push_str("ppt/slides/_rels/slide");
    append_usize(path, slide_num);
    path.push_str(".xml.rels");
}

fn push_chart_rid(rids: &mut Vec<String>, rel_num: usize) {
    let mut rid = String::with_capacity(8);
    rid.push_str("rId");
    append_usize(&mut rid, rel_num);
    rids.push(rid);
}

/// Collect chart metadata from slides (eager version)
fn collect_chart_info(slides: Option<&[SlideContent]>) -> ChartInfo {
    let mut total_charts = 0;
    let mut slide_start_indices = Vec::new();

    if let Some(slides) = slides {
        slide_start_indices.reserve(slides.len());
        for slide in slides {
            slide_start_indices.push(total_charts + 1);
            total_charts += slide.charts.len();
        }
    }

    ChartInfo {
        total_charts,
        slide_start_indices,
    }
}

/// Collect chart metadata from lazy slide source
fn collect_chart_info_lazy(slides: &dyn LazySlideSource) -> ChartInfo {
    let mut total_charts = 0;
    let mut slide_start_indices = Vec::with_capacity(slides.slide_count());

    for i in 0..slides.slide_count() {
        slide_start_indices.push(total_charts + 1);
        total_charts += slides
            .slide_features(i)
            .map(|(_, chart_count)| chart_count)
            .unwrap_or(0);
    }

    ChartInfo {
        total_charts,
        slide_start_indices,
    }
}

/// Whether print settings request handout master packaging.
fn uses_handouts(settings: Option<&PresentationSettings>) -> bool {
    settings
        .and_then(|s| s.print.as_ref())
        .map(|p| p.print_what == PrintWhat::Handouts)
        .unwrap_or(false)
}

/// Collect slide titles for `docProps/app.xml` (eager version).
///
/// Returns one title per slide, falling back to "Slide N" placeholders when no
/// custom slide content is available or a slide has an empty title.
fn collect_slide_titles(custom_slides: Option<&[SlideContent]>, slide_count: usize) -> Vec<String> {
    match custom_slides {
        Some(slides) => slides
            .iter()
            .enumerate()
            .map(|(i, s)| {
                if s.title.trim().is_empty() {
                    format!("Slide {}", i + 1)
                } else {
                    s.title.clone()
                }
            })
            .collect(),
        None => (0..slide_count)
            .map(|i| format!("Slide {}", i + 1))
            .collect(),
    }
}

/// Collect slide titles for `docProps/app.xml` (lazy version).
fn collect_slide_titles_lazy(slides: &dyn LazySlideSource, slide_count: usize) -> Vec<String> {
    let mut titles = Vec::with_capacity(slide_count);
    for i in 0..slide_count {
        let title = slides
            .generate_slide(i)
            .map(|s| {
                if s.title.trim().is_empty() {
                    format!("Slide {}", i + 1)
                } else {
                    s.title
                }
            })
            .unwrap_or_else(|| format!("Slide {}", i + 1));
        titles.push(title);
    }
    titles
}

/// Write content types XML
fn write_content_types<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    slide_count: usize,
    custom_slides: Option<&[SlideContent]>,
    chart_info: &ChartInfo,
    has_handout: bool,
) -> Result<()> {
    let media_exts = custom_slides
        .map(build_media_registry)
        .map(|registry| registry.extensions())
        .unwrap_or_default();
    let content_types = create_content_types_xml_with_notes_and_charts(
        slide_count,
        custom_slides,
        chart_info.total_charts,
        has_handout,
        &media_exts,
    );

    zip.start_file("[Content_Types].xml", *options)?;
    zip.write_all(content_types.as_bytes())?;
    Ok(())
}

/// Write presentation relationships in PowerPoint order.
fn write_presentation_relationships<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    slide_count: usize,
    has_notes: bool,
    has_handout: bool,
) -> Result<()> {
    let pres_rels = create_presentation_rels_xml_full(slide_count, has_notes, has_handout);

    zip.start_file("ppt/_rels/presentation.xml.rels", *options)?;
    zip.write_all(pres_rels.as_bytes())?;
    Ok(())
}

/// Write presProps, viewProps, and tableStyles (always emitted).
fn write_standard_package_parts<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    settings: Option<&PresentationSettings>,
) -> Result<()> {
    let pres_props = create_pres_props_xml(settings);
    zip.start_file("ppt/presProps.xml", *options)?;
    zip.write_all(pres_props.as_bytes())?;

    let view_props = create_view_props_xml();
    zip.start_file("ppt/viewProps.xml", *options)?;
    zip.write_all(view_props.as_bytes())?;

    let table_styles = create_table_styles_xml();
    zip.start_file("ppt/tableStyles.xml", *options)?;
    zip.write_all(table_styles.as_bytes())?;
    Ok(())
}

/// Write handout master when print settings use handouts.
fn write_handout_master<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    settings: Option<&PresentationSettings>,
) -> Result<()> {
    use crate::generator::slide_content::print_settings::PrintSettings;

    let handout_xml = settings
        .and_then(|s| s.print.as_ref())
        .map(|p| p.to_handout_master_xml())
        .unwrap_or_else(|| PrintSettings::default().to_handout_master_xml());

    zip.start_file("ppt/handoutMasters/handoutMaster1.xml", *options)?;
    zip.write_all(handout_xml.as_bytes())?;

    zip.start_file("ppt/theme/theme3.xml", *options)?;
    zip.write_all(office_theme_xml().as_bytes())?;

    let rels = create_handout_master_rels_xml();
    zip.start_file("ppt/handoutMasters/_rels/handoutMaster1.xml.rels", *options)?;
    zip.write_all(rels.as_bytes())?;
    Ok(())
}

/// Write notes master files if needed
fn write_notes_master<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
) -> Result<()> {
    let notes_master = create_notes_master_xml();
    zip.start_file("ppt/notesMasters/notesMaster1.xml", *options)?;
    zip.write_all(notes_master.as_bytes())?;

    zip.start_file("ppt/theme/theme2.xml", *options)?;
    zip.write_all(office_theme_xml().as_bytes())?;

    let notes_master_rels = create_notes_master_rels_xml();
    zip.start_file("ppt/notesMasters/_rels/notesMaster1.xml.rels", *options)?;
    zip.write_all(notes_master_rels.as_bytes())?;
    Ok(())
}

/// Write theme and layout files
fn write_theme_and_layouts<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    settings: Option<&PresentationSettings>,
    template: Option<&PptxTemplate>,
) -> Result<()> {
    let print = settings.and_then(|s| s.print.as_ref());

    if let Some(tmpl) = template {
        for (path, data) in tmpl.parts() {
            zip.start_file(path, *options)?;
            zip.write_all(data)?;
        }
        if let Some(theme) = settings.and_then(|s| s.theme.as_ref()) {
            let theme_xml = create_theme_xml(Some(theme));
            zip.start_file("ppt/theme/theme1.xml", *options)?;
            zip.write_all(theme_xml.as_bytes())?;
        }
        return Ok(());
    }

    let use_cached_layouts = !print_affects_theme_parts(print);

    for n in 1..=STANDARD_LAYOUT_COUNT {
        zip.start_file(format!("ppt/slideLayouts/slideLayout{n}.xml"), *options)?;
        if use_cached_layouts {
            zip.write_all(package_cache::default_layout_xml(n).as_bytes())?;
        } else {
            let layout_xml = create_slide_layout_xml(n, print);
            zip.write_all(layout_xml.as_bytes())?;
        }

        zip.start_file(format!("ppt/slideLayouts/_rels/slideLayout{n}.xml.rels"), *options)?;
        zip.write_all(create_layout_rels_xml().as_bytes())?;
    }

    zip.start_file("ppt/slideMasters/slideMaster1.xml", *options)?;
    if use_cached_layouts {
        zip.write_all(package_cache::default_slide_master_xml().as_bytes())?;
    } else {
        let slide_master = create_slide_master_xml(print);
        zip.write_all(slide_master.as_bytes())?;
    }

    zip.start_file("ppt/slideMasters/_rels/slideMaster1.xml.rels", *options)?;
    if use_cached_layouts {
        zip.write_all(package_cache::master_rels_xml().as_bytes())?;
    } else {
        let master_rels = create_master_rels_xml();
        zip.write_all(master_rels.as_bytes())?;
    }

    if let Some(theme) = settings.and_then(|s| s.theme.as_ref()) {
        let theme_xml = create_theme_xml(Some(theme));
        zip.start_file("ppt/theme/theme1.xml", *options)?;
        zip.write_all(theme_xml.as_bytes())?;
    } else {
        zip.start_file("ppt/theme/theme1.xml", *options)?;
        zip.write_all(office_theme_xml().as_bytes())?;
    }

    Ok(())
}

/// Write document properties
fn write_document_properties<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    title: &str,
    slide_count: usize,
    notes_count: usize,
    slide_titles: &[String],
) -> Result<()> {
    // Core properties
    let core_props = create_core_props_xml(title);
    zip.start_file("docProps/core.xml", *options)?;
    zip.write_all(core_props.as_bytes())?;

    // App properties
    let app_props = create_app_props_xml(slide_count, notes_count, slide_titles);
    zip.start_file("docProps/app.xml", *options)?;
    zip.write_all(app_props.as_bytes())?;

    Ok(())
}

/// Write all package files to the ZIP archive (eager version with Vec<SlideContent>)
fn write_package_files<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    title: &str,
    slide_count: usize,
    custom_slides: Option<&[SlideContent]>,
    settings: Option<&PresentationSettings>,
) -> Result<()> {
    let has_notes = custom_slides
        .map(|slides| slides.iter().any(|s| s.notes.is_some()))
        .unwrap_or(false);
    let has_handout = uses_handouts(settings);
    let template = load_template(settings)?;

    let chart_info = collect_chart_info(custom_slides);

    // 1. Content types
    write_content_types(zip, options, slide_count, custom_slides, &chart_info, has_handout)?;

    // 2. Package relationships
    let rels = create_rels_xml();
    zip.start_file("_rels/.rels", *options)?;
    zip.write_all(rels.as_bytes())?;

    // 3. Presentation relationships
    write_presentation_relationships(zip, options, slide_count, has_notes, has_handout)?;

    // 4. Presentation document
    let presentation = create_presentation_xml(title, slide_count, has_notes, has_handout);
    zip.start_file("ppt/presentation.xml", *options)?;
    zip.write_all(presentation.as_bytes())?;

    // 5. Standard package parts (presProps, viewProps, tableStyles)
    write_standard_package_parts(zip, options, settings)?;

    // 6. Handout master (when printing handouts)
    if has_handout {
        write_handout_master(zip, options, settings)?;
    }

    // 7. Slides
    write_slides(zip, options, slide_count, custom_slides)?;

    // 8. Slide relationships
    write_slide_relationships_extended(
        zip,
        options,
        custom_slides,
        &chart_info.slide_start_indices,
        slide_count,
        template.as_ref(),
    )?;

    // 9. Notes relationships and master
    if has_notes {
        write_notes_relationships(zip, options, custom_slides)?;
        write_notes_master(zip, options)?;
    }

    // 10. Theme and layouts
    write_theme_and_layouts(zip, options, settings, template.as_ref())?;

    // 11. Document properties
    let notes_count = custom_slides
        .map(|slides| slides.iter().filter(|s| s.notes.is_some()).count())
        .unwrap_or(0);
    let slide_titles = collect_slide_titles(custom_slides, slide_count);
    write_document_properties(zip, options, title, slide_count, notes_count, &slide_titles)?;

    // 12. Charts (with embedded workbooks)
    if chart_info.total_charts > 0 {
        write_charts(zip, options, custom_slides, &chart_info.slide_start_indices)?;
    }

    // 13. Images
    write_images(zip, options, custom_slides)?;

    Ok(())
}

/// Write all package files to the ZIP archive (lazy version with LazySlideSource)
fn write_package_files_lazy<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    title: &str,
    slides: &dyn LazySlideSource,
    settings: Option<&PresentationSettings>,
) -> Result<()> {
    let slide_count = slides.slide_count();
    let has_notes = (0..slide_count).any(|i| {
        slides
            .slide_features(i)
            .map(|(notes, _)| notes)
            .unwrap_or(false)
    });
    let has_handout = uses_handouts(settings);
    let template = load_template(settings)?;

    let chart_info = collect_chart_info_lazy(slides);

    // 1. Content types (lazy version)
    write_content_types_lazy(zip, options, slide_count, slides, &chart_info, has_handout)?;

    // 2. Package relationships
    let rels = create_rels_xml();
    zip.start_file("_rels/.rels", *options)?;
    zip.write_all(rels.as_bytes())?;

    // 3. Presentation relationships
    write_presentation_relationships(zip, options, slide_count, has_notes, has_handout)?;

    // 4. Presentation document
    let presentation = create_presentation_xml(title, slide_count, has_notes, has_handout);
    zip.start_file("ppt/presentation.xml", *options)?;
    zip.write_all(presentation.as_bytes())?;

    // 5. Standard package parts
    write_standard_package_parts(zip, options, settings)?;

    // 6. Handout master
    if has_handout {
        write_handout_master(zip, options, settings)?;
    }

    // 7–8–12. Slides, relationships, and charts (single pass per slide)
    write_slide_packages_lazy(
        zip,
        options,
        slides,
        &chart_info.slide_start_indices,
        template.as_ref(),
    )?;

    // 9. Notes relationships and master (lazy version)
    if has_notes {
        write_notes_relationships_lazy(zip, options, slides)?;
        write_notes_master(zip, options)?;
    }

    // 10. Theme and layouts
    write_theme_and_layouts(zip, options, settings, template.as_ref())?;

    // 11. Document properties
    let notes_count = (0..slide_count)
        .filter(|i| {
            slides
                .slide_features(*i)
                .map(|(notes, _)| notes)
                .unwrap_or(false)
        })
        .count();
    let slide_titles = collect_slide_titles_lazy(slides, slide_count);
    write_document_properties(zip, options, title, slide_count, notes_count, &slide_titles)?;

    // 12. Images
    write_images_lazy(zip, options, slides)?;

    Ok(())
}

/// Write content types for lazy slides
fn write_content_types_lazy<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    slide_count: usize,
    slides: &dyn LazySlideSource,
    chart_info: &ChartInfo,
    has_handout: bool,
) -> Result<()> {
    let notes_count = (0..slide_count)
        .filter(|i| {
            slides
                .slide_features(*i)
                .map(|(notes, _)| notes)
                .unwrap_or(false)
        })
        .count();

    let media_registry = build_media_registry_lazy(slides);
    let media_exts = media_registry.extensions();

    let mut content_types = content_types_opening(&media_exts, chart_info.total_charts);

    for i in 1..=slide_count {
        content_types.push_str(&format!(
            "\n<Override PartName=\"/ppt/slides/slide{i}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.slide+xml\"/>"
        ));
    }

    if notes_count > 0 {
        let mut notes_index = 0usize;
        for i in 0..slide_count {
            if slides
                .slide_features(i)
                .map(|(notes, _)| notes)
                .unwrap_or(false)
            {
                notes_index += 1;
                content_types.push_str(&format!(
                    "\n<Override PartName=\"/ppt/notesSlides/notesSlide{notes_index}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml\"/>"
                ));
            }
        }
        content_types.push_str("\n<Override PartName=\"/ppt/notesMasters/notesMaster1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.notesMaster+xml\"/>");
        content_types.push_str("\n<Override PartName=\"/ppt/theme/theme2.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.theme+xml\"/>");
    }

    if has_handout {
        content_types.push_str("\n<Override PartName=\"/ppt/handoutMasters/handoutMaster1.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.presentationml.handoutMaster+xml\"/>");
        content_types.push_str("\n<Override PartName=\"/ppt/theme/theme3.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.theme+xml\"/>");
    }

    for i in 1..=chart_info.total_charts {
        content_types.push_str(&format!(
            "\n<Override PartName=\"/ppt/charts/chart{i}.xml\" ContentType=\"application/vnd.openxmlformats-officedocument.drawingml.chart+xml\"/>"
        ));
        content_types.push_str(&format!(
            "\n<Override PartName=\"/ppt/embeddings/{}\" ContentType=\"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\"/>",
            chart_embedding_filename(i)
        ));
    }

    content_types.push_str(
        r#"
<Override PartName="/ppt/slideMasters/slideMaster1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml"/>
<Override PartName="/ppt/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
<Override PartName="/ppt/tableStyles.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.tableStyles+xml"/>
<Override PartName="/ppt/viewProps.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.viewProps+xml"/>
<Override PartName="/ppt/presProps.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presProps+xml"/>
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>"#,
    );
    super::layout_parts::append_layout_content_type_overrides(&mut content_types, STANDARD_LAYOUT_COUNT);
    content_types.push_str("\n</Types>");

    zip.start_file("[Content_Types].xml", *options)?;
    zip.write_all(content_types.as_bytes())?;
    Ok(())
}

/// Write a chart part with rels and embedded Excel workbook.
fn write_chart_package<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    chart_idx: usize,
    chart: &crate::generator::charts::Chart,
) -> Result<()> {
    let chart_xml = generate_chart_part_xml(chart);
    zip.start_file(format!("ppt/charts/chart{chart_idx}.xml"), *options)?;
    zip.write_all(chart_xml.as_bytes())?;

    let embedding_name = chart_embedding_filename(chart_idx);
    let rels_xml = create_chart_rels_xml(&embedding_name);
    zip.start_file(format!("ppt/charts/_rels/chart{chart_idx}.xml.rels"), *options)?;
    zip.write_all(rels_xml.as_bytes())?;

    zip.start_file(format!("ppt/embeddings/{embedding_name}"), *options)?;
    zip.write_all(reference_workbook_bytes())?;
    Ok(())
}

/// Write slide XML, relationships, and chart parts in one pass (lazy version).
fn write_slide_packages_lazy<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    slides: &dyn LazySlideSource,
    slide_chart_start_indices: &[usize],
    template: Option<&PptxTemplate>,
) -> Result<()> {
    let media_registry = build_media_registry_lazy(slides);
    let mut slide_path = String::with_capacity(48);
    let mut rels_path = String::with_capacity(56);
    let mut notes_part_num = 0usize;

    for i in 0..slides.slide_count() {
        let Some(slide) = slides.generate_slide(i) else {
            continue;
        };
        let slide_num = i + 1;
        let layout_number = resolve_layout_number(&slide, template);
        let images = slide_image_rel_targets(&slide, &media_registry);
        let image_count = images.len();

        let start_rid = slide_content_rel_start(slide.notes.is_some(), image_count);
        let mut chart_rids = Vec::with_capacity(slide.charts.len());
        for j in 0..slide.charts.len() {
            push_chart_rid(&mut chart_rids, start_rid + j);
        }

        let slide_xml = create_slide_xml_with_content(slide_num, &slide, &chart_rids);
        set_slide_xml_path(&mut slide_path, slide_num);
        zip.start_file(&slide_path, *options)?;
        zip.write_all(slide_xml.as_bytes())?;

        let notes_part = if slide.notes.is_some() {
            notes_part_num += 1;
            Some(notes_part_num)
        } else {
            None
        };

        if let Some(ref notes) = slide.notes {
            let notes_xml = create_notes_xml(slide_num, notes);
            set_notes_part_path(&mut slide_path, notes_part_num);
            zip.start_file(&slide_path, *options)?;
            zip.write_all(notes_xml.as_bytes())?;
        }

        let mut chart_rels = Vec::with_capacity(slide.charts.len());
        let start_chart_idx = slide_chart_start_indices[i];
        for j in 0..slide.charts.len() {
            let mut rid = String::with_capacity(8);
            rid.push_str("rId");
            append_usize(&mut rid, start_rid + j);
            let mut target = String::with_capacity(24);
            target.push_str("../charts/chart");
            append_usize(&mut target, start_chart_idx + j);
            target.push_str(".xml");
            chart_rels.push((rid, target));
        }

        let slide_rels = super::package_xml::create_slide_rels_xml_with_images(
            layout_number,
            slide.notes.is_some(),
            notes_part.unwrap_or(1),
            &chart_rels,
            &images,
            &slide_hyperlink_relationships(&slide),
        );
        set_slide_rels_path(&mut rels_path, slide_num);
        zip.start_file(&rels_path, *options)?;
        zip.write_all(slide_rels.as_bytes())?;

        for (j, chart) in slide.charts.iter().enumerate() {
            let chart_idx = start_chart_idx + j;
            write_chart_package(zip, options, chart_idx, chart)?;
        }
    }

    Ok(())
}

/// Write notes relationship files (lazy version)
fn write_notes_relationships_lazy<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    slides: &dyn LazySlideSource,
) -> Result<()> {
    let mut notes_part_num = 0usize;
    for i in 0..slides.slide_count() {
        if slides
            .slide_features(i)
            .map(|(notes, _)| notes)
            .unwrap_or(false)
        {
            notes_part_num += 1;
            let slide_num = i + 1;
            let notes_rels = create_notes_rels_xml(slide_num);
            zip.start_file(format!("ppt/notesSlides/_rels/notesSlide{notes_part_num}.xml.rels"), *options)?;
            zip.write_all(notes_rels.as_bytes())?;
        }
    }
    Ok(())
}

/// Write slide XML files (eager version)
fn write_slides<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    slide_count: usize,
    custom_slides: Option<&[SlideContent]>,
) -> Result<()> {
    let mut zip_path = String::with_capacity(48);

    match custom_slides {
        Some(slides) => {
            let mut notes_part_num = 0usize;
            for (i, slide) in slides.iter().enumerate() {
                let slide_num = i + 1;

                let mut chart_rids = Vec::with_capacity(slide.charts.len());
                let start_rid = slide_content_rel_start(slide.notes.is_some(), slide.images.len());
                for j in 0..slide.charts.len() {
                    push_chart_rid(&mut chart_rids, start_rid + j);
                }

                let slide_xml = create_slide_xml_with_content(slide_num, slide, &chart_rids);
                set_slide_xml_path(&mut zip_path, slide_num);
                zip.start_file(&zip_path, *options)?;
                zip.write_all(slide_xml.as_bytes())?;

                if let Some(notes) = &slide.notes {
                    notes_part_num += 1;
                    let notes_xml = create_notes_xml(slide_num, notes);
                    set_notes_part_path(&mut zip_path, notes_part_num);
                    zip.start_file(&zip_path, *options)?;
                    zip.write_all(notes_xml.as_bytes())?;
                }
            }
        }
        None => {
            for i in 1..=slide_count {
                let slide_xml = create_slide_xml(i, "Presentation");
                set_slide_xml_path(&mut zip_path, i);
                zip.start_file(&zip_path, *options)?;
                zip.write_all(slide_xml.as_bytes())?;
            }
        }
    }
    Ok(())
}

/// Write slide relationship files with notes and charts (eager version)
fn write_slide_relationships_extended<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    custom_slides: Option<&[SlideContent]>,
    slide_chart_start_indices: &[usize],
    slide_count: usize,
    template: Option<&PptxTemplate>,
) -> Result<()> {
    let media_registry = custom_slides
        .map(build_media_registry)
        .unwrap_or_default();
    let mut notes_part_num = 0usize;

    match custom_slides {
        Some(slides) => {
            let mut zip_path = String::with_capacity(56);
            for (i, slide) in slides.iter().enumerate() {
                let slide_num = i + 1;
                let layout_number = resolve_layout_number(slide, template);
                let images = slide_image_rel_targets(slide, &media_registry);
                let image_count = images.len();
                let notes_part = if slide.notes.is_some() {
                    notes_part_num += 1;
                    Some(notes_part_num)
                } else {
                    None
                };

                let mut chart_rels = Vec::with_capacity(slide.charts.len());
                let start_chart_idx = slide_chart_start_indices[i];
                let start_rid = slide_content_rel_start(slide.notes.is_some(), image_count);

                for j in 0..slide.charts.len() {
                    let mut rid = String::with_capacity(8);
                    rid.push_str("rId");
                    append_usize(&mut rid, start_rid + j);
                    let mut target = String::with_capacity(24);
                    target.push_str("../charts/chart");
                    append_usize(&mut target, start_chart_idx + j);
                    target.push_str(".xml");
                    chart_rels.push((rid, target));
                }

                let slide_rels = super::package_xml::create_slide_rels_xml_with_images(
                    layout_number,
                    slide.notes.is_some(),
                    notes_part.unwrap_or(1),
                    &chart_rels,
                    &images,
                    &slide_hyperlink_relationships(slide),
                );
                set_slide_rels_path(&mut zip_path, slide_num);
                zip.start_file(&zip_path, *options)?;
                zip.write_all(slide_rels.as_bytes())?;
            }
        }
        None => {
            let mut zip_path = String::with_capacity(56);
            for i in 1..=slide_count {
                let slide_rels = create_slide_rels_xml();
                set_slide_rels_path(&mut zip_path, i);
                zip.start_file(&zip_path, *options)?;
                zip.write_all(slide_rels.as_bytes())?;
            }
        }
    }
    Ok(())
}

/// Write chart files (eager version)
fn write_charts<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    custom_slides: Option<&[SlideContent]>,
    slide_chart_start_indices: &[usize],
) -> Result<()> {
    if let Some(slides) = custom_slides {
        for (i, slide) in slides.iter().enumerate() {
            let start_chart_idx = slide_chart_start_indices[i];
            for (j, chart) in slide.charts.iter().enumerate() {
                let chart_idx = start_chart_idx + j;
                write_chart_package(zip, options, chart_idx, chart)?;
            }
        }
    }
    Ok(())
}

/// Write notes relationship files (eager version)
fn write_notes_relationships<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    custom_slides: Option<&[SlideContent]>,
) -> Result<()> {
    if let Some(slides) = custom_slides {
        let mut notes_part_num = 0usize;
        for (i, slide) in slides.iter().enumerate() {
            if slide.notes.is_some() {
                notes_part_num += 1;
                let slide_num = i + 1;
                let notes_rels = create_notes_rels_xml(slide_num);
                zip.start_file(format!("ppt/notesSlides/_rels/notesSlide{notes_part_num}.xml.rels"), *options)?;
                zip.write_all(notes_rels.as_bytes())?;
            }
        }
    }
    Ok(())
}

/// Write image files to ppt/media/
fn write_images<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    custom_slides: Option<&[SlideContent]>,
) -> Result<()> {
    if let Some(slides) = custom_slides {
        let registry = build_media_registry(slides);
        for (i, (bytes, ext)) in registry.files().iter().enumerate() {
            let filename = format!("ppt/media/image{}.{}", i + 1, ext);
            zip.start_file(filename, *options)?;
            zip.write_all(bytes)?;
        }
    }
    Ok(())
}

/// Write image files from a lazy slide source.
fn write_images_lazy<W: Write + Seek>(
    zip: &mut ZipWriter<W>,
    options: &FileOptions,
    slides: &dyn LazySlideSource,
) -> Result<()> {
    let registry = build_media_registry_lazy(slides);
    for (i, (bytes, ext)) in registry.files().iter().enumerate() {
        let filename = format!("ppt/media/image{}.{}", i + 1, ext);
        zip.start_file(filename, *options)?;
        zip.write_all(bytes)?;
    }
    Ok(())
}

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

    /// A simple test slide source that generates numbered slides
    struct TestSlideSource {
        count: usize,
        with_notes: bool,
    }

    impl LazySlideSource for TestSlideSource {
        fn slide_count(&self) -> usize {
            self.count
        }

        fn generate_slide(&self, index: usize) -> Option<SlideContent> {
            if index < self.count {
                let mut slide = SlideContent::new(&format!("Slide {}", index + 1))
                    .add_bullet(&format!("Point {}", index + 1));

                if self.with_notes {
                    slide.notes = Some("Speaker notes here".to_string());
                }

                Some(slide)
            } else {
                None
            }
        }

        fn slide_has_notes(&self, index: usize) -> bool {
            self.with_notes && index < self.count
        }

        fn slide_chart_count(&self, _index: usize) -> usize {
            0
        }
    }

    #[test]
    fn test_create_pptx_to_writer() {
        let buffer = Vec::new();
        let cursor = Cursor::new(buffer);
        let result = create_pptx_to_writer(cursor, "Test Presentation", 3);
        assert!(result.is_ok());
    }

    #[test]
    fn test_create_pptx_with_content_to_writer() {
        let slides = vec![
            SlideContent::new("Title").add_bullet("Point 1"),
            SlideContent::new("Slide 2").add_bullet("Point 2"),
        ];

        let buffer = Vec::new();
        let cursor = Cursor::new(buffer);
        let result = create_pptx_with_content_to_writer(cursor, "Test", &slides, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_create_pptx_lazy_to_writer() {
        let source = TestSlideSource { count: 10, with_notes: false };

        let buffer = Vec::new();
        let cursor = Cursor::new(buffer);
        let result = create_pptx_lazy_to_writer(cursor, "Lazy Test", Box::new(source), None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_create_pptx_lazy_with_notes() {
        let source = TestSlideSource { count: 5, with_notes: true };

        let buffer = Vec::new();
        let cursor = Cursor::new(buffer);
        let result = create_pptx_lazy_to_writer(cursor, "Lazy Test with Notes", Box::new(source), None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_lazy_slide_source() {
        let source = TestSlideSource { count: 3, with_notes: false };

        assert_eq!(source.slide_count(), 3);
        assert!(!source.slide_has_notes(0));
        assert_eq!(source.slide_chart_count(0), 0);

        let slide = source.generate_slide(0);
        assert!(slide.is_some());
        assert_eq!(slide.unwrap().title, "Slide 1");

        let out_of_bounds = source.generate_slide(10);
        assert!(out_of_bounds.is_none());
    }

    #[test]
    fn test_streaming_api_compatibility() {
        // Test that the streaming API produces the same output as the in-memory API
        let slides = vec![
            SlideContent::new("Test").add_bullet("Item 1"),
        ];

        // In-memory version
        let in_memory = create_pptx_with_content("Test", slides.clone()).unwrap();

        // Streaming version
        let buffer = Vec::new();
        let cursor = Cursor::new(buffer);
        let streaming = create_pptx_with_content_to_writer(cursor, "Test", &slides, None).unwrap().into_inner();

        // Both should produce valid ZIP files (non-empty)
        assert!(!in_memory.is_empty());
        assert!(!streaming.is_empty());
    }

    #[test]
    fn test_lazy_vs_eager_compatibility() {
        // Test that lazy and eager APIs produce compatible output
        let eager_slides = vec![
            SlideContent::new("Slide 1").add_bullet("Point 1"),
            SlideContent::new("Slide 2").add_bullet("Point 2"),
        ];

        // Eager version
        let eager = create_pptx_with_content("Test", eager_slides).unwrap();

        // Lazy version (equivalent content)
        let source = TestSlideSource { count: 2, with_notes: false };
        let buffer = Vec::new();
        let cursor = Cursor::new(buffer);
        let lazy = create_pptx_lazy_to_writer(cursor, "Test", Box::new(source), None).unwrap().into_inner();

        // Both should produce valid ZIP files
        assert!(!eager.is_empty());
        assert!(!lazy.is_empty());
    }
}