printcalc 0.2.0

Command-line tool for calculating 3D print job costs (material, print time, printer wear, electricity, labor, hardware) from sliced-file output. The library lives in `libprintcalc`.
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
//! The default command: cost a sliced file or a saved job `.toml`.
//!
//! This is where auto-detection meets explicit flags. A sliced file is
//! parsed, [`libprintcalc::auto_job`] guesses the printer and each
//! filament slot from what the file records, and anything the user named
//! explicitly overrides that guess. Detection then reports only what a
//! flag *didn't* already settle, so the output never suggests a choice
//! was made for the user when it wasn't.
//!
//! Detection notes and warnings go to stderr, keeping stdout clean for
//! `--format json`.

use std::collections::HashMap;
use std::error::Error;
use std::io::IsTerminal;
use std::path::PathBuf;

use clap::ValueEnum;

use libprintcalc::formats::slice_summary::{SliceSummary, SlotUsage};
use libprintcalc::model::cost::CostBreakdown;
use libprintcalc::model::filament::MaterialProfile;
use libprintcalc::model::job::{HardwareItem, Job};
use libprintcalc::model::printer::PrinterProfile;

use crate::interactive;
use crate::ops::{self, JobTier, NewJobParams, ProfileChoice};

#[derive(Clone, Copy, ValueEnum)]
pub enum OutputFormat {
    Table,
    Json,
}

/// Which job tier to build. Named to match `--type`.
#[derive(Clone, Copy, ValueEnum)]
pub enum JobType {
    Default,
    Full,
    Bundled,
}

impl From<JobType> for JobTier {
    fn from(t: JobType) -> Self {
        match t {
            JobType::Default => JobTier::Default,
            JobType::Full => JobTier::Full,
            JobType::Bundled => JobTier::Bundled,
        }
    }
}

pub struct JobInvocation {
    pub file: PathBuf,
    /// Defaults to the output file's stem, or the input file's stem if no
    /// `-o`. Ignored when calc'ing an existing job (the name's already
    /// baked into the file).
    pub name: Option<String>,
    pub printer: Option<String>,
    /// Default filament for every slot; `filament_map` overrides specific
    /// slots on top of it.
    pub filament: Option<String>,
    /// `slot=name,slot=name,...` — one filament per AMS tray / extruder.
    /// Slots it doesn't cover fall back to `filament`, if given.
    pub filament_map: Option<String>,
    /// Stored on the job as its fallback for any slot still unmapped at
    /// calc time, rather than expanded per slot like `filament`.
    pub default_filament: Option<String>,
    pub output: Option<PathBuf>,
    pub job_type: JobType,
    /// "all" (default) or a plate number; ignored with a warning for
    /// single-plate formats.
    pub plate: Option<String>,
    pub labor_minutes: Option<f64>,
    pub labor_rate: Option<f64>,
    /// How many units to price. All costs and prices are for the batch.
    /// `None` means 1 for a new job, or "keep the saved value" for a job
    /// `.toml`.
    pub quantity: Option<u32>,
    /// `--electricity-cost`: per-job override of the global default.
    pub electricity_cost: Option<f64>,
    /// `--margin`, unparsed: per-job override of the global margins.
    pub margin: Option<String>,
    /// `--efficiency-factor`: per-job override of every material's own.
    pub efficiency_factor: Option<f64>,
    /// `--hardware` entries, unparsed: `name:unit price[:units]`.
    pub hardware: Vec<String>,
    /// Skip reading the printer/filament the sliced file names, and ask
    /// (or fail) as if it said nothing.
    pub no_detect: bool,
    pub format: OutputFormat,
    /// `false` for `--no-color`. Even when `true`, color only shows on a
    /// terminal with `NO_COLOR` unset.
    pub color: bool,
    pub interactive: bool,
    /// Re-tier an existing job (`full` or `bundled` only) instead of
    /// calc'ing it — only meaningful when `file` is a `.toml`. Needs `-o`.
    pub retier: Option<JobType>,
}

/// Everything on the command line that adjusts a job after it's built —
/// shared by the new-job path (where these become the job's initial
/// values) and the job-file path (where they override what's saved).
struct Overrides {
    plate: ops::PlateOverride,
    hardware: Vec<HardwareItem>,
    quantity: Option<u32>,
    electricity_cost: Option<f64>,
    margins: Option<Vec<f64>>,
    efficiency_factor: Option<f64>,
}

impl Overrides {
    fn parse(inv: &JobInvocation) -> Result<Self, Box<dyn Error>> {
        Ok(Overrides {
            plate: parse_plate(inv.plate.as_deref())?,
            hardware: parse_hardware(&inv.hardware)?,
            quantity: inv.quantity,
            electricity_cost: inv.electricity_cost,
            margins: inv
                .margin
                .as_deref()
                .map(super::parse_margins)
                .transpose()?,
            efficiency_factor: inv.efficiency_factor,
        })
    }

    /// Applies to a loaded job. Hardware is added by name — a saved entry
    /// with the same name is replaced, anything else is appended — and
    /// every other field replaces the saved value only when its flag was
    /// given.
    fn apply(self, job: &mut Job) {
        self.plate.apply(job);
        for item in self.hardware {
            job.add_hardware(item);
        }
        if let Some(q) = self.quantity {
            job.quantity = q;
        }
        if self.electricity_cost.is_some() {
            job.electricity_cost_per_kwh_override = self.electricity_cost;
        }
        if self.margins.is_some() {
            job.margin_percentages_override = self.margins;
        }
        if self.efficiency_factor.is_some() {
            job.material_efficiency_factor_override = self.efficiency_factor;
        }
    }
}

/// The configured currency symbol, or `$` if the config can't be read —
/// a display detail shouldn't stop a calc that otherwise works.
fn currency_symbol() -> String {
    ops::show_config()
        .map(|c| c.currency_symbol)
        .unwrap_or_else(|_| "$".to_string())
}

pub fn run(inv: JobInvocation) -> Result<(), Box<dyn Error>> {
    if inv.file.extension().and_then(|e| e.to_str()) == Some("toml") {
        if let Some(job_type) = inv.retier {
            let output = inv
                .output
                .ok_or("--retier needs -o/--output to save the re-tiered job to")?;
            let retiered = ops::retier_job(&inv.file, job_type.into())?;
            ops::save_job(&retiered, &output)?;
            eprintln!("wrote {}", output.display());
            return Ok(());
        }
        // --plate here overrides the job's own stored plate_selection —
        // persisted if -o saves a copy below, calc-only otherwise (e.g.
        // pricing just one plate out of a Bundled job saved with "all",
        // without needing the original sliced file).
        let overrides = Overrides::parse(&inv)?;
        let mut job = ops::load_job(&inv.file)?;
        overrides.apply(&mut job);
        if let Some(output) = &inv.output {
            ops::save_job(&job, output)?;
            eprintln!("wrote {}", output.display());
        }
        let breakdown = ops::calc_job_at(&job, &inv.file)?;
        print_result(&breakdown, inv.format, inv.color, &currency_symbol());
        return Ok(());
    }
    new_job_flow(inv)
}

/// `file` isn't a `.toml` — parse it as a sliced file and build a job. With
/// `-o`, save it first. Either way the cost is then resolved and shown,
/// so saving a quote and seeing it are one step. The "wrote" note goes to
/// stderr so `--format json` stays clean on stdout.
fn new_job_flow(inv: JobInvocation) -> Result<(), Box<dyn Error>> {
    let slice = ops::parse_slice(&inv.file)?;
    let overrides = Overrides::parse(&inv)?;
    let currency = currency_symbol();

    // Parsed up front so detection can tell which slots a flag already
    // covers and stay quiet about those.
    let mapped: HashMap<String, ProfileChoice<MaterialProfile>> = match &inv.filament_map {
        Some(s) => parse_filament_map(s)?,
        None => HashMap::new(),
    };

    // What the sliced file itself says about the setup. Explicit flags
    // still win; this only fills in what wasn't passed, and only reports
    // on what it might actually be used for.
    let detected = (!inv.no_detect).then(|| ops::detect_profiles(&slice));
    if let Some(detected) = &detected {
        let covered = |slot: &str| inv.filament.is_some() || mapped.contains_key(slot);
        print_detected(
            &inv.file,
            &slice,
            detected,
            inv.printer.is_none(),
            &covered,
            &currency,
        );
    }

    let printer = resolve_printer_choice(inv.printer, detected.as_ref(), inv.interactive)?;
    let filaments = resolve_filaments(
        &slice,
        detected.as_ref(),
        inv.filament,
        mapped,
        inv.default_filament.is_some(),
        inv.interactive,
    )?;
    // No prompt: unlike printer/filament, omitting this just means no
    // labor cost rather than something that needs asking about.
    let labor_minutes = inv.labor_minutes.unwrap_or(0.0);
    let labor_rate = interactive::optional_f64(
        inv.labor_rate,
        "Labor rate per hour (blank = use config default)",
        inv.interactive,
    )?;

    let name = inv.name.clone().unwrap_or_else(|| {
        inv.output
            .as_ref()
            .and_then(|p| p.file_stem())
            .or_else(|| inv.file.file_stem())
            .and_then(|s| s.to_str())
            .unwrap_or("job")
            .to_string()
    });

    let outcome = ops::new_job(
        NewJobParams {
            name,
            slice_path: inv.file.clone(),
            printer,
            filaments,
            default_filament: inv.default_filament.map(ProfileChoice::Named),
            quantity: overrides.quantity.unwrap_or(1),
            hardware: overrides.hardware,
            plate_selection: overrides.plate.selection(),
            labor_minutes,
            labor_rate_per_hour: labor_rate,
            electricity_cost_per_kwh_override: overrides.electricity_cost,
            margin_percentages_override: overrides.margins,
            material_efficiency_factor_override: overrides.efficiency_factor,
            tier: inv.job_type.into(),
        },
        slice,
    )?;
    if outcome.plate_selection_ignored {
        eprintln!("warning: --plate is ignored for single-plate sliced files");
    }

    if let Some(path) = &inv.output {
        ops::save_job(&outcome.job, path)?;
        eprintln!("wrote {}", path.display());
    }
    let breakdown = ops::calc_job_data(&outcome.job, &outcome.slice)?;
    print_result(&breakdown, inv.format, inv.color, &currency);
    Ok(())
}

/// Everything the sliced file identified, on stderr so `--format json`
/// keeps stdout clean. Nothing here is authoritative — it's shown so a
/// wrong guess is visible before it silently prices the job.
///
/// Only what might actually get used is reported: `want_printer` and
/// `want_slot` are false for anything an explicit flag already covers,
/// since printing a guess that's about to be overridden is just
/// misleading.
fn print_detected(
    path: &std::path::Path,
    slice: &SliceSummary,
    detected: &ops::Detected,
    want_printer: bool,
    want_slot: &dyn Fn(&str) -> bool,
    currency: &str,
) {
    let mut lines: Vec<String> = Vec::new();

    if let Some(slicer) = &slice.meta.slicer {
        lines.push(format!("  slicer:   {slicer}"));
    }
    if want_printer {
        let model = slice
            .meta
            .printer_profile
            .as_deref()
            .or_else(|| slice.printer_model());
        match (&detected.printer, model) {
            (Some(m), _) => lines.push(format!(
                "  printer:  {} -> {} ({})",
                m.matched_on,
                m.profile.name,
                confidence_label(m.confidence)
            )),
            (None, Some(model)) => {
                lines.push(format!("  printer:  {model} -> no matching profile"))
            }
            (None, None) => {}
        }
    }
    for slot in &detected.slots {
        if want_slot(&slot.usage.slot_id) {
            continue;
        }
        let described = slot.usage.label();
        lines.push(match (&slot.matched, &slot.from_file) {
            (Some(m), _) => format!(
                "  {described} -> {} ({})",
                m.profile.name,
                confidence_label(m.confidence)
            ),
            (None, Some(profile)) => format!(
                "  {described} -> priced from the file at {currency}{:.2}/{}",
                profile.price,
                if slot.usage.ml > 0.0 { "L" } else { "kg" }
            ),
            (None, None) => format!("  {described} -> no matching profile"),
        });
    }

    if lines.is_empty() {
        return;
    }
    let name = path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("sliced file");
    eprintln!("detected from {name}");
    for line in lines {
        eprintln!("{line}");
    }
}

/// "exact" is reserved for a name that matched outright; everything else
/// says "guess" so a near-miss is never mistaken for a certainty.
fn confidence_label(confidence: f64) -> &'static str {
    if confidence >= 0.999 {
        "exact"
    } else {
        "guess"
    }
}

/// `--printer` if given, else the detected match, else a prompt (or, under
/// `--no-input`, an error naming the flag).
fn resolve_printer_choice(
    flag: Option<String>,
    detected: Option<&ops::Detected>,
    interactive_mode: bool,
) -> Result<ProfileChoice<PrinterProfile>, Box<dyn Error>> {
    if let Some(name) = flag {
        return Ok(ProfileChoice::Named(name));
    }
    if let Some(matched) = detected.and_then(|d| d.printer.as_ref()) {
        return Ok(ProfileChoice::Named(matched.profile.name.clone()));
    }
    let name = interactive::require_choice(
        None,
        "printer",
        "Printer profile name",
        interactive_mode,
        &ops::list_printers().unwrap_or_default(),
    )?;
    Ok(ProfileChoice::Named(name))
}

/// Builds the slot_id -> filament map for a new job, most specific source
/// first: `map` (already parsed from `--filament-map`), then `--filament`
/// as a default for the rest, then whatever the sliced file identified,
/// then a prompt per remaining slot (or, under `--no-input`, an error
/// naming what's still unpriced).
///
/// `has_default` is `--default-filament`: it's stored on the job rather
/// than expanded here, but it does mean an unmapped slot isn't a problem
/// worth asking about.
fn resolve_filaments(
    slice: &SliceSummary,
    detected: Option<&ops::Detected>,
    filament: Option<String>,
    mut map: HashMap<String, ProfileChoice<MaterialProfile>>,
    has_default: bool,
    interactive_mode: bool,
) -> Result<HashMap<String, ProfileChoice<MaterialProfile>>, Box<dyn Error>> {
    let slots = slice.slots();

    // A slice with no usage at all still needs somewhere to put the one
    // filament the user names.
    let slot_ids: Vec<String> = if slots.is_empty() {
        vec!["1".to_string()]
    } else {
        slots.iter().map(|s| s.slot_id.clone()).collect()
    };

    if let Some(default_name) = &filament {
        for slot in &slot_ids {
            map.entry(slot.clone())
                .or_insert_with(|| ProfileChoice::Named(default_name.clone()));
        }
    }

    for slot in detected.map(|d| d.slots.as_slice()).unwrap_or_default() {
        if let Some(profile) = slot.profile() {
            map.entry(slot.usage.slot_id.clone()).or_insert_with(|| {
                // A matched library/built-in profile keeps its name so the
                // job can link it; one built from the file's own numbers
                // has no name to link and travels by value.
                match &slot.matched {
                    Some(m) => ProfileChoice::Named(m.profile.name.clone()),
                    None => ProfileChoice::Profile(Box::new(profile.clone())),
                }
            });
        }
    }

    let missing: Vec<&SlotUsage> = slots
        .iter()
        .filter(|slot| !map.contains_key(&slot.slot_id))
        .collect();
    if missing.is_empty() || has_default {
        // Nothing named and nothing detected: the single-slot case still
        // deserves the plain, un-slot-labeled prompt.
        if map.is_empty() && !has_default {
            let name = interactive::require_choice(
                None,
                "filament",
                "Filament profile name",
                interactive_mode,
                &ops::list_filaments().unwrap_or_default(),
            )?;
            let first_slot = slot_ids
                .into_iter()
                .next()
                .unwrap_or_else(|| "1".to_string());
            return Ok(HashMap::from([(first_slot, ProfileChoice::Named(name))]));
        }
        return Ok(map);
    }

    if !interactive_mode {
        let listed: Vec<String> = missing.iter().map(|s| s.label()).collect();
        return Err(format!(
            "no filament for {}; pass --filament as a default, or --filament-map slot=name,... per slot",
            listed.join(", ")
        )
        .into());
    }

    let choices = ops::list_filaments().unwrap_or_default();
    for slot in missing {
        // One slot and nothing else in play reads better without the
        // slot label, same as it always has.
        let label = if slots.len() == 1 {
            "Filament profile name".to_string()
        } else {
            format!("Filament profile for {}", slot.label())
        };
        let name = interactive::require_choice(None, "filament", &label, true, &choices)?;
        map.insert(slot.slot_id.clone(), ProfileChoice::Named(name));
    }
    Ok(map)
}

/// `--plate`: absent, `"all"`, or a plate number.
fn parse_plate(plate: Option<&str>) -> Result<ops::PlateOverride, Box<dyn Error>> {
    match plate {
        None => Ok(ops::PlateOverride::Keep),
        Some("all") => Ok(ops::PlateOverride::All),
        Some(s) => s
            .parse::<u32>()
            .map(ops::PlateOverride::Only)
            .map_err(|_parse_err| format!("invalid --plate value: {s:?}").into()),
    }
}

fn parse_filament_map(
    s: &str,
) -> Result<HashMap<String, ProfileChoice<MaterialProfile>>, Box<dyn Error>> {
    s.split(',')
        .map(|pair| {
            let (slot, name) = pair.split_once('=').ok_or_else(|| {
                format!("invalid --filament-map entry {pair:?}, expected slot=name")
            })?;
            Ok((
                slot.trim().to_string(),
                ProfileChoice::Named(name.trim().to_string()),
            ))
        })
        .collect()
}

/// `--hardware` entries: `name:unit price[:units]`, one [`HardwareItem`]
/// each. Split from the right so a name may itself contain colons
/// (`M3x5:brass:0.08:4` is a name of `M3x5:brass`). Units default to 1,
/// matching the job file's default.
fn parse_hardware(entries: &[String]) -> Result<Vec<HardwareItem>, Box<dyn Error>> {
    entries.iter().map(|e| parse_hardware_entry(e)).collect()
}

fn parse_hardware_entry(entry: &str) -> Result<HardwareItem, Box<dyn Error>> {
    let invalid = || -> Box<dyn Error> {
        format!("invalid --hardware entry {entry:?}, expected name:unit-price[:units]").into()
    };
    // rsplitn yields right-to-left: [units?, price, name].
    let mut parts: Vec<&str> = entry.rsplitn(3, ':').map(str::trim).collect();
    parts.reverse();
    let (name, price, units) = match parts.as_slice() {
        [name, price] => (*name, *price, "1"),
        [name, price, units] => (*name, *price, *units),
        _ => return Err(invalid()),
    };
    if name.is_empty() {
        return Err(invalid());
    }
    // A pasted "$0.08" or "€0.08" should still work: drop anything
    // before the number itself.
    let cost: f64 = price
        .trim_start_matches(|c: char| !c.is_ascii_digit() && c != '.' && c != '-')
        .parse()
        .map_err(|_parse_err| format!("invalid --hardware unit price {price:?} in {entry:?}"))?;
    if !cost.is_finite() || cost < 0.0 {
        return Err(format!("invalid --hardware unit price {price:?} in {entry:?}").into());
    }
    let quantity: u32 = units.parse().ok().filter(|q| *q > 0).ok_or_else(|| {
        format!("invalid --hardware units {units:?} in {entry:?} (whole number, at least 1)")
    })?;
    Ok(HardwareItem::new(name, cost).with_quantity(quantity))
}

fn print_result(breakdown: &CostBreakdown, format: OutputFormat, color: bool, currency: &str) {
    match format {
        OutputFormat::Table => print_receipt(breakdown, color && color_allowed(), currency),
        OutputFormat::Json => println!(
            "{}",
            serde_json::to_string_pretty(breakdown).expect("CostBreakdown always serializes")
        ),
    }
}

/// Color is for eyes, not pipes: off when stdout isn't a terminal, and
/// off when `NO_COLOR` is set to anything (<https://no-color.org>).
fn color_allowed() -> bool {
    std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none_or(|v| v.is_empty())
}

const CYAN: &str = "\x1b[36m";
const AMBER: &str = "\x1b[33m";
const ROSE: &str = "\x1b[31m";
const BLUE: &str = "\x1b[34m";
const BOLD_GREEN: &str = "\x1b[1;32m";
const RESET: &str = "\x1b[0m";

/// The per-plate table, shown only for a multi-plate file — a single
/// plate would just restate the summary.
fn print_plate_section(b: &CostBreakdown, width: usize) {
    if b.plate_breakdown.len() <= 1 {
        return;
    }
    println!("plates");
    for p in &b.plate_breakdown {
        let label = format!("Plate {}", p.plate_index);
        let detail = format!(
            "{} · {}",
            format_duration(p.print_time_seconds),
            format_amount(p.grams, p.ml)
        );
        print_dotted(&label, &detail, width);
    }
    let combined_seconds: f64 = b.plate_breakdown.iter().map(|p| p.print_time_seconds).sum();
    let combined_grams: f64 = b.plate_breakdown.iter().map(|p| p.grams).sum();
    let combined_ml: f64 = b.plate_breakdown.iter().map(|p| p.ml).sum();
    println!("  {}", "─".repeat(width + 3));
    let detail = format!(
        "{} · {}",
        format_duration(combined_seconds),
        format_amount(combined_grams, combined_ml)
    );
    print_dotted("Combined", &detail, width);
    println!();
}

/// One line per contributing filament profile. Slots sharing a profile
/// are already collapsed into one line by the calculation.
fn print_material_section(b: &CostBreakdown, width: usize, color: bool, cur: &str) {
    if b.material_breakdown.is_empty() {
        return;
    }
    println!("material breakdown");
    for line in &b.material_breakdown {
        let price_unit = if line.unit == "g" { "kg" } else { "L" };
        let detail = format!(
            "{:.2}{} @ {cur}{:.2}/{}",
            line.amount, line.unit, line.unit_price, price_unit
        );
        print_item_line(
            &line.filament_name,
            &detail,
            line.cost,
            width,
            CYAN,
            color,
            cur,
        );
    }
    println!();
}

/// One line per hardware entry, never aggregated — each is already its
/// own line item. For a batch the per-unit count and the multiplier are
/// both shown, since `quantity` scaling is easy to forget.
fn print_hardware_section(b: &CostBreakdown, width: usize, color: bool, cur: &str) {
    if b.hardware_breakdown.is_empty() {
        return;
    }
    println!("hardware breakdown");
    for line in &b.hardware_breakdown {
        let each = if b.quantity > 1 {
            format!(
                "x{} @ {cur}{:.2} × {}",
                line.quantity, line.unit_cost, b.quantity
            )
        } else {
            format!("x{} @ {cur}{:.2}", line.quantity, line.unit_cost)
        };
        print_item_line(&line.name, &each, line.cost, width, BLUE, color, cur);
    }
    println!();
}

/// Receipt-style, dotted-leader table format, split into sections.
///
/// "plates" only shows when more than one plate is actually in play — a
/// single selected plate (whether because the job only has one, or
/// because `--plate N` picked one out) is the "omit entirely" case, same
/// rule `material breakdown`/`hardware breakdown` follow. The per-unit
/// column follows the same rule against `quantity`.
///
/// Every money figure is for the whole batch; the trailing summary line
/// spells out the per-unit split when there's more than one unit.
fn print_receipt(b: &CostBreakdown, color: bool, cur: &str) {
    let batch = b.quantity > 1;

    let cost_labels = [
        "Material",
        "Printer wear",
        "Electricity",
        "Labor",
        "Hardware",
        "TOTAL",
    ];
    let plate_labels = ["Combined".to_string()];
    let width = b
        .plate_breakdown
        .iter()
        .map(|p| format!("Plate {}", p.plate_index).len())
        .chain(plate_labels.iter().map(|s| s.len()))
        .chain(b.material_breakdown.iter().map(|l| l.filament_name.len()))
        .chain(b.hardware_breakdown.iter().map(|l| l.name.len()))
        .chain(cost_labels.iter().map(|s| s.len()))
        .max()
        .unwrap_or(0)
        + 14;

    println!();
    print_plate_section(b, width);
    print_material_section(b, width, color, cur);
    print_hardware_section(b, width, color, cur);

    if batch {
        println!("summary  ({} units)", b.quantity);
    } else {
        println!("summary");
    }
    print_cost_line("Material", b.material_cost, width, CYAN, color, cur);
    print_cost_line("Printer wear", b.wear_cost, width, AMBER, color, cur);
    print_cost_line("Electricity", b.electricity_cost, width, AMBER, color, cur);
    print_cost_line("Labor", b.labor_cost, width, ROSE, color, cur);
    print_cost_line("Hardware", b.hardware_cost, width, BLUE, color, cur);
    println!("  {}", "─".repeat(width + 3));
    print_cost_line("TOTAL", b.total_cost, width, BOLD_GREEN, color, cur);
    if batch {
        print_cost_line("  per unit", b.unit_cost, width, BOLD_GREEN, color, cur);
    }
    println!();

    // Margins, not markups: price = cost / (1 - margin). The header says
    // so, because the difference between the two is large and silent.
    let margins: Vec<String> = b
        .suggested_prices
        .iter()
        .map(|p| format!("{:.0}%", p.margin_percent))
        .collect();
    let prices: Vec<String> = b
        .suggested_prices
        .iter()
        .map(|p| format!("{cur}{:.2}", p.price))
        .collect();
    let header = format!("  sale price @ {} margin:", margins.join(" / "));
    println!(
        "{}",
        paint(
            &format!("{header}  {}", prices.join("  ")),
            BOLD_GREEN,
            color
        )
    );
    if batch {
        let per_unit: Vec<String> = b
            .suggested_prices
            .iter()
            .map(|p| format!("{cur}{:.2}", p.unit_price))
            .collect();
        // Padded to the header so the two rows of prices line up.
        let label = format!("{:<width$}", "  per unit:", width = header.len());
        println!(
            "{}",
            paint(
                &format!("{label}  {}", per_unit.join("  ")),
                BOLD_GREEN,
                color
            )
        );
    }
}

/// `label` + dotted leader (to `width`) + `trailing`, uncolored — used for
/// the plates section, which isn't part of the cost color scheme.
fn print_dotted(label: &str, trailing: &str, width: usize) {
    let dots = ".".repeat(width.saturating_sub(label.len()));
    println!("  {label}{dots} {trailing}");
}

fn print_cost_line(label: &str, value: f64, width: usize, code: &str, color: bool, cur: &str) {
    let dots = ".".repeat(width.saturating_sub(label.len()));
    println!(
        "{}",
        paint(&format!("  {label}{dots} {cur}{value:.2}"), code, color)
    );
}

/// A breakdown row: `label` + dotted leader + `detail` (the "amount @
/// price" part) + ` = $cost`.
fn print_item_line(
    label: &str,
    detail: &str,
    cost: f64,
    width: usize,
    code: &str,
    color: bool,
    cur: &str,
) {
    let dots = ".".repeat(width.saturating_sub(label.len()));
    println!(
        "{}",
        paint(
            &format!("  {label}{dots} {detail} = {cur}{cost:.2}"),
            code,
            color
        )
    );
}

/// "17m17s" / "1h05m17s" style.
fn format_duration(seconds: f64) -> String {
    let total = seconds.round() as i64;
    let h = total / 3600;
    let m = (total % 3600) / 60;
    let s = total % 60;
    if h > 0 {
        format!("{h}h{m:02}m{s:02}s")
    } else {
        format!("{m}m{s:02}s")
    }
}

/// "6.66g", "2.60ml", or "6.66g + 2.60ml" if a plate somehow has both
/// (grams/ml are 0.0 rather than absent when not applicable).
fn format_amount(grams: f64, ml: f64) -> String {
    match (grams > 0.0, ml > 0.0) {
        (true, true) => format!("{grams:.2}g + {ml:.2}ml"),
        (true, false) => format!("{grams:.2}g"),
        (false, true) => format!("{ml:.2}ml"),
        (false, false) => "0g".to_string(),
    }
}

fn paint(text: &str, code: &str, enabled: bool) -> String {
    if enabled {
        format!("{code}{text}{RESET}")
    } else {
        text.to_string()
    }
}

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

    #[test]
    fn parses_name_price_units() {
        let item = parse_hardware_entry("M3 brass insert:0.08:4").unwrap();
        assert_eq!(item.name, "M3 brass insert");
        assert_eq!(item.cost, 0.08);
        assert_eq!(item.quantity, 4);
    }

    #[test]
    fn units_default_to_one_and_currency_symbol_is_tolerated() {
        let item = parse_hardware_entry("box:$0.60").unwrap();
        assert_eq!(item.name, "box");
        assert_eq!(item.cost, 0.60);
        assert_eq!(item.quantity, 1);
        assert_eq!(parse_hardware_entry("box:€0.60").unwrap().cost, 0.60);
    }

    #[test]
    fn name_may_contain_colons() {
        let item = parse_hardware_entry("magnet 6:3:0.12:2").unwrap();
        assert_eq!(item.name, "magnet 6:3");
        assert_eq!(item.cost, 0.12);
        assert_eq!(item.quantity, 2);
    }

    #[test]
    fn rejects_malformed_entries() {
        for bad in [
            "box",
            ":0.5",
            "box:abc",
            "box:-1",
            "box:0.5:0",
            "box:0.5:1.5",
            "box:0.5:x",
        ] {
            assert!(
                parse_hardware_entry(bad).is_err(),
                "{bad:?} should be rejected"
            );
        }
    }

    #[test]
    fn parses_many() {
        let items = parse_hardware(&["a:1".into(), "b:2:3".into()]).unwrap();
        assert_eq!(items.len(), 2);
        assert_eq!(items[1].quantity, 3);
    }
}