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
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
//! Disk-backed orchestration functions: `new_job`, `calc_job`, `retier_job`,
//! config show/set, and add/list/show for printer & filament profiles.
//!
//! Everything here touches the filesystem (the profile library, job files,
//! `config.toml`) — the filesystem-free counterparts (`parse_job`,
//! in-memory calc) live at the `printcalc` root.

use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

use libprintcalc::builtin;
use libprintcalc::formats::slice_summary::SliceSummary;
use libprintcalc::formats::{self, ParseError};
use libprintcalc::model::cost::{self, CalcError, CostBreakdown, CostDefaults};
use libprintcalc::model::filament::MaterialProfile;
use libprintcalc::model::job::{HardwareItem, Job, JobSource, LaborCost, ProfileRef};
use libprintcalc::model::printer::PrinterProfile;

use crate::disk::{self, LibraryError};

/// Why an `ops` call failed — wraps every error type the functions below
/// can hit, so callers only need one error type to handle.
#[derive(Debug)]
#[non_exhaustive]
pub enum OpsError {
    /// A file read/write failed.
    Io(std::io::Error),
    /// Parsing a sliced file failed.
    Parse(ParseError),
    /// Costing a job failed.
    Calc(CalcError),
    /// A profile library or config operation failed.
    Library(LibraryError),
    /// A filesystem-free `printcalc` call failed.
    Lib(libprintcalc::Error),
    /// The request itself doesn't make sense — re-tiering downwards,
    /// removing a built-in preset. Nothing failed; it was never possible.
    Invalid(String),
}

impl fmt::Display for OpsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OpsError::Io(e) => write!(f, "io error: {e}"),
            OpsError::Parse(e) => write!(f, "{e}"),
            OpsError::Calc(e) => write!(f, "{e}"),
            OpsError::Library(e) => write!(f, "{e}"),
            OpsError::Lib(e) => write!(f, "{e}"),
            OpsError::Invalid(e) => write!(f, "{e}"),
        }
    }
}

impl std::error::Error for OpsError {}

impl From<std::io::Error> for OpsError {
    fn from(e: std::io::Error) -> Self {
        OpsError::Io(e)
    }
}
impl From<ParseError> for OpsError {
    fn from(e: ParseError) -> Self {
        OpsError::Parse(e)
    }
}
impl From<CalcError> for OpsError {
    fn from(e: CalcError) -> Self {
        OpsError::Calc(e)
    }
}
impl From<LibraryError> for OpsError {
    fn from(e: LibraryError) -> Self {
        OpsError::Library(e)
    }
}
impl From<libprintcalc::Error> for OpsError {
    fn from(e: libprintcalc::Error) -> Self {
        OpsError::Lib(e)
    }
}

/// Loads the saved config — overrides layered over
/// [`CostDefaults::default`] (see [`disk::load_config`]).
pub fn show_config() -> Result<CostDefaults, OpsError> {
    Ok(disk::load_config()?)
}

/// Persists the config, overwriting any previously saved one.
pub fn set_config(config: &CostDefaults) -> Result<(), OpsError> {
    Ok(disk::save_config(config)?)
}

/// Saves a printer profile to the local library, overwriting any existing
/// one under the same name.
pub fn add_printer(profile: &PrinterProfile) -> Result<(), OpsError> {
    Ok(disk::save_printer(profile)?)
}

/// Every printer profile name — saved ones plus any built-in preset (see
/// [`libprintcalc::builtin`]) not already saved under the same name,
/// sorted.
pub fn list_printers() -> Result<Vec<String>, OpsError> {
    let mut names = disk::list_printers()?;
    for profile in builtin::printers() {
        if !names.contains(&profile.name) {
            names.push(profile.name.clone());
        }
    }
    names.sort();
    Ok(names)
}

/// Loads a printer profile by name — checks the local library first, then
/// falls back to a built-in preset (see [`libprintcalc::builtin`]) under
/// the same name.
pub fn show_printer(name: &str) -> Result<PrinterProfile, OpsError> {
    match disk::load_printer(name) {
        Ok(profile) => Ok(profile),
        Err(LibraryError::NotFound { .. }) => builtin::printer(name).cloned().ok_or_else(|| {
            OpsError::Library(LibraryError::NotFound {
                kind: "printer",
                name: name.to_string(),
            })
        }),
        Err(e) => Err(e.into()),
    }
}

/// Only the printer profiles actually saved in the library — no built-in
/// presets — sorted. Tells `edit` whether it's updating a file or
/// creating one that shadows a preset.
pub fn list_saved_printers() -> Result<Vec<String>, OpsError> {
    Ok(disk::list_printers()?)
}

/// Deletes a saved printer profile. A built-in preset isn't a file, so
/// removing one is a [`LibraryError::NotFound`] with a hint saying so.
pub fn remove_printer(name: &str) -> Result<(), OpsError> {
    match disk::remove_printer(name) {
        Err(LibraryError::NotFound { .. }) if builtin::printer(name).is_some() => {
            Err(OpsError::Invalid(format!(
                "printer {name:?} is a built-in preset, not a saved profile — nothing to remove"
            )))
        }
        other => Ok(other?),
    }
}

/// Saves a filament profile to the local library, overwriting any existing
/// one under the same name.
pub fn add_filament(profile: &MaterialProfile) -> Result<(), OpsError> {
    Ok(disk::save_filament(profile)?)
}

/// Only the filament profiles actually saved in the library — no built-in
/// presets — sorted.
pub fn list_saved_filaments() -> Result<Vec<String>, OpsError> {
    Ok(disk::list_filaments()?)
}

/// Deletes a saved filament profile. A built-in preset isn't a file, so
/// removing one is a [`LibraryError::NotFound`] with a hint saying so.
pub fn remove_filament(name: &str) -> Result<(), OpsError> {
    match disk::remove_filament(name) {
        Err(LibraryError::NotFound { .. }) if builtin::filament(name).is_some() => {
            Err(OpsError::Invalid(format!(
                "filament {name:?} is a built-in preset, not a saved profile — nothing to remove"
            )))
        }
        other => Ok(other?),
    }
}

/// Every filament profile name — saved ones plus any built-in preset (see
/// [`libprintcalc::builtin`]) not already saved under the same name,
/// sorted.
pub fn list_filaments() -> Result<Vec<String>, OpsError> {
    let mut names = disk::list_filaments()?;
    for profile in builtin::filaments() {
        if !names.contains(&profile.name) {
            names.push(profile.name.clone());
        }
    }
    names.sort();
    Ok(names)
}

/// Loads a filament profile by name — checks the local library first, then
/// falls back to a built-in preset (see [`libprintcalc::builtin`]) under
/// the same name.
pub fn show_filament(name: &str) -> Result<MaterialProfile, OpsError> {
    match disk::load_filament(name) {
        Ok(profile) => Ok(profile),
        Err(LibraryError::NotFound { .. }) => builtin::filament(name).cloned().ok_or_else(|| {
            OpsError::Library(LibraryError::NotFound {
                kind: "filament",
                name: name.to_string(),
            })
        }),
        Err(e) => Err(e.into()),
    }
}

/// Which shape to write printer/filament refs and the source in. Not a
/// field on [`Job`] itself: tier is just which `ProfileRef`/`JobSource`
/// variant gets built, chosen once at creation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JobTier {
    /// Printer/filament linked by name; source kept as a path re-parsed at
    /// calc time.
    Default,
    /// Printer/filament embedded; source still a path.
    Full,
    /// Printer/filament embedded; source is the parsed `SliceSummary`
    /// itself — fully self-contained, no sliced file needed at calc time.
    Bundled,
}

/// Which profile to price something against: a name to look up in the
/// library (or the built-ins), or a profile already in hand.
///
/// The second case is what auto-detection produces — a profile built out
/// of a sliced file's own stated filament price has no library entry to
/// name, so it's carried by value and embedded whatever the job tier.
#[derive(Debug, Clone)]
pub enum ProfileChoice<T> {
    /// Looked up via [`show_printer`]/[`show_filament`].
    Named(String),
    /// Used as-is, no lookup.
    Profile(Box<T>),
}

impl<T> From<String> for ProfileChoice<T> {
    fn from(name: String) -> Self {
        ProfileChoice::Named(name)
    }
}

/// Inputs to [`new_job`].
pub struct NewJobParams {
    /// A free-form label for the job.
    pub name: String,
    /// The sliced file to parse and cost against.
    pub slice_path: PathBuf,
    /// Which printer profile to price wear and electricity against.
    pub printer: ProfileChoice<PrinterProfile>,
    /// slot_id -> filament profile. The common single-material case
    /// is just a one-entry map — building that one entry under the right
    /// slot_id (not always `"1"`, see
    /// [`SliceSummary::distinct_filament_slots`])
    /// is the caller's job, same as `[material]` vs `[materials]` is just
    /// two TOML shapes for the same underlying map.
    pub filaments: HashMap<String, ProfileChoice<MaterialProfile>>,
    /// `None` => every plate; `Some(i)` => just plate `i`.
    pub plate_selection: Option<u32>,
    /// Time spent on labor for this job.
    pub labor_minutes: f64,
    /// `None` => fall back to the config default.
    pub labor_rate_per_hour: Option<f64>,
    /// Filament for any used slot `filaments` doesn't cover. `None` =>
    /// every used slot must be covered explicitly.
    pub default_filament: Option<ProfileChoice<MaterialProfile>>,
    /// How many units the job covers; `1` for a one-off.
    pub quantity: u32,
    /// Flat per-unit extras (inserts, magnets, packaging, ...).
    pub hardware: Vec<HardwareItem>,
    /// `Some` => this job ignores the global electricity cost.
    pub electricity_cost_per_kwh_override: Option<f64>,
    /// `Some` => this job ignores the global margin percentages.
    pub margin_percentages_override: Option<Vec<f64>>,
    /// `Some` => this job ignores every material's own efficiency factor.
    pub material_efficiency_factor_override: Option<f64>,
    /// Which shape to build the job in.
    pub tier: JobTier,
}

/// Result of [`new_job`].
pub struct NewJobOutcome {
    /// The built job.
    pub job: Job,
    /// The parsed sliced file — handed back so a caller previewing the
    /// cost without saving (the bare `printcalc <slice>` form) doesn't
    /// need to re-read and re-parse it via [`calc_job_data`].
    pub slice: SliceSummary,
    /// `--plate` was given but the sliced file only has one plate to begin
    /// with, so it's ignored — the CLI is responsible for the warning
    /// text, this just tells it whether to print one.
    pub plate_selection_ignored: bool,
}

/// What a sliced file identified about itself, matched against this
/// machine's profile library and the built-in presets. Returned by
/// [`detect_profiles`].
pub struct Detected {
    /// The printer the file names, if a profile matched it.
    pub printer: Option<libprintcalc::Match<PrinterProfile>>,
    /// One entry per used filament slot, in slot order.
    pub slots: Vec<libprintcalc::AutoSlot>,
}

/// Matches the printer and each filament slot a sliced file names against
/// the saved profile library first, then the built-in presets, falling
/// back per slot to a profile built from the file's own stated filament
/// price.
///
/// Purely advisory — nothing is written and nothing is decided here. The
/// CLI shows the result and lets `--printer`/`--filament`/`--filament-map`
/// override any of it.
pub fn detect_profiles(slice: &SliceSummary) -> Detected {
    // The library is a nicety here: if it can't be read, the built-in
    // presets alone still give a useful answer.
    let printers: Vec<PrinterProfile> = disk::list_printers()
        .unwrap_or_default()
        .iter()
        .filter_map(|name| disk::load_printer(name).ok())
        .collect();
    let filaments: Vec<MaterialProfile> = disk::list_filaments()
        .unwrap_or_default()
        .iter()
        .filter_map(|name| disk::load_filament(name).ok())
        .collect();
    let efficiency_factor = default_efficiency_factor(&filaments);

    let auto = libprintcalc::auto_job(
        "detect",
        slice.clone(),
        slice.clone(),
        printers.iter(),
        filaments.iter(),
        efficiency_factor,
    );
    Detected {
        printer: auto.printer,
        slots: auto.slots,
    }
}

/// Waste allowance for a profile built from a sliced file's own price.
/// No slicer records one, so this borrows the median of whatever the
/// user's saved filaments use, falling back to the documented default
/// when they have none saved.
fn default_efficiency_factor(saved: &[MaterialProfile]) -> f64 {
    let mut factors: Vec<f64> = saved
        .iter()
        .map(|m| m.efficiency_factor)
        .filter(|f| f.is_finite() && *f > 0.0)
        .collect();
    if factors.is_empty() {
        return libprintcalc::model::filament::DEFAULT_EFFICIENCY_FACTOR;
    }
    factors.sort_by(|a, b| a.partial_cmp(b).expect("filtered to finite values"));
    factors
        .get(factors.len() / 2)
        .copied()
        .unwrap_or(libprintcalc::model::filament::DEFAULT_EFFICIENCY_FACTOR)
}

/// Reads and parses `path` as a sliced file. Exposed on its own so a
/// caller (the CLI's interactive job-creation flow) can validate the file
/// before doing anything else with it — e.g. prompting for printer/
/// filament/labor — instead of discovering it's unparseable only after
/// asking the user several questions. Its result is then handed to
/// [`new_job`], so the file is only ever parsed once.
pub fn parse_slice(path: &Path) -> Result<SliceSummary, OpsError> {
    let bytes = fs::read(path)?;
    let filename = path.file_name().and_then(|f| f.to_str()).unwrap_or("");
    Ok(formats::parse_slice_bytes(&bytes, filename)?)
}

/// Builds a [`Job`] at the requested [`JobTier`] from a sliced file
/// already read via [`parse_slice`].
///
/// `Full`/`Bundled` load the named printer/filament profiles (library or
/// built-in, see [`show_printer`]/[`show_filament`]) to embed them,
/// erroring if a name is neither saved nor built-in. `Default` just links
/// by name, so no lookup happens here.
pub fn new_job(params: NewJobParams, summary: SliceSummary) -> Result<NewJobOutcome, OpsError> {
    let plate_selection_ignored = params.plate_selection.is_some() && summary.plates.len() <= 1;
    let plate_selection = if plate_selection_ignored {
        None
    } else {
        params.plate_selection
    };

    let printer = resolve_choice(params.printer, params.tier, show_printer)?;
    let material: HashMap<String, ProfileRef<MaterialProfile>> = params
        .filaments
        .into_iter()
        .map(|(slot, choice)| Ok((slot, resolve_choice(choice, params.tier, show_filament)?)))
        .collect::<Result<_, OpsError>>()?;
    let default_material = params
        .default_filament
        .map(|choice| resolve_choice(choice, params.tier, show_filament))
        .transpose()?;
    let source = match params.tier {
        JobTier::Bundled => JobSource::Embedded(summary.clone()),
        JobTier::Default | JobTier::Full => JobSource::Path(params.slice_path),
    };

    let mut labor = LaborCost::new(params.labor_minutes);
    labor.rate_per_hour = params.labor_rate_per_hour;
    let job = Job::new(params.name, source, printer)
        .with_plate_selection(plate_selection)
        .with_materials(material)
        .with_default_material(default_material)
        .with_quantity(params.quantity)
        .with_labor(labor)
        .with_hardware(params.hardware)
        .with_electricity_cost_per_kwh_override(params.electricity_cost_per_kwh_override)
        .with_margin_percentages_override(params.margin_percentages_override)
        .with_material_efficiency_factor_override(params.material_efficiency_factor_override);
    Ok(NewJobOutcome {
        job,
        slice: summary,
        plate_selection_ignored,
    })
}

/// Writes `job` as TOML to `path`, overwriting any existing file there.
/// Serialized at the lib's [`libprintcalc::JobTier::Slim`] tier (as-is);
/// re-tiering is [`retier_job`]'s job, done against the local library.
pub fn save_job(job: &Job, path: &Path) -> Result<(), OpsError> {
    let text = job.to_toml(libprintcalc::JobTier::Slim)?;
    fs::write(path, text)?;
    Ok(())
}

/// Reads and parses a job TOML file from disk, warning about any key in
/// it that no field claimed (see [`disk::warn_unknown_keys`]).
pub fn load_job(path: &Path) -> Result<Job, OpsError> {
    let text = fs::read_to_string(path)?;
    let (job, unknown) = libprintcalc::parse_toml(&text)?;
    disk::warn_unknown_keys(path, &unknown);
    Ok(job)
}

/// What a `--plate` flag asks for, including the case where it wasn't
/// given at all.
///
/// The distinction between [`PlateOverride::Keep`] and
/// [`PlateOverride::All`] is the whole reason this isn't just an
/// `Option<u32>`: a job saved with `plate = "all"` and one with no
/// `--plate` on the command line agree on the outcome, but only the
/// latter should leave the job's own stored selection alone.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlateOverride {
    /// No `--plate` given — keep whatever the job file says.
    Keep,
    /// `--plate all` — cost every plate, whatever the job file says.
    All,
    /// `--plate N` — cost just plate `N`.
    Only(u32),
}

impl PlateOverride {
    /// Applies this override to `job`, leaving [`PlateOverride::Keep`]
    /// alone. The job in memory only — nothing is written to disk.
    pub fn apply(self, job: &mut Job) {
        match self {
            PlateOverride::Keep => {}
            PlateOverride::All => job.plate_selection = None,
            PlateOverride::Only(index) => job.plate_selection = Some(index),
        }
    }

    /// The [`Job::plate_selection`] to give a job being built from
    /// scratch, where there is no stored selection to keep.
    pub fn selection(self) -> Option<u32> {
        match self {
            PlateOverride::Keep | PlateOverride::All => None,
            PlateOverride::Only(index) => Some(index),
        }
    }
}

/// Loads the job at `job_path`, resolves its printer/filament refs (against
/// the local library, for `Linked` — embedded profiles need no lookup) and
/// its slice data (re-parsed from `source.path`, or read straight off
/// `source.summary` for a Bundled job), then runs the cost calc.
///
/// `plate` replaces the job's own `plate_selection` for this calc only —
/// the file on disk is untouched. That lets a job saved with
/// `plate = "all"` (a Bundled job whose original sliced file is long gone,
/// say) still be priced one plate at a time on demand.
///
/// `hardware` is appended to whatever the job file already lists, so
/// `--hardware` on the command line adds to a saved job rather than
/// replacing its entries. The job on disk is untouched.
#[cfg(test)]
pub fn calc_job(
    job_path: &Path,
    plate: PlateOverride,
    hardware: Vec<HardwareItem>,
) -> Result<CostBreakdown, OpsError> {
    let mut job = load_job(job_path)?;
    plate.apply(&mut job);
    for item in hardware {
        job.add_hardware(item);
    }
    calc_job_at(&job, job_path)
}

/// Costs a job already loaded (and possibly modified in
/// memory) from `job_path` — the path is still needed to resolve a
/// relative `source.path` against the job file's directory.
pub fn calc_job_at(job: &Job, job_path: &Path) -> Result<CostBreakdown, OpsError> {
    let slice = resolve_slice_summary(job, job_path)?;
    calc_job_data(job, &slice)
}

/// Same as [`calc_job_at`], but for a `job`/`slice` already in hand — used to
/// preview a job's cost without writing it to disk first (the bare
/// `printcalc <slice>` form, no `-o`). Loads the saved config for the
/// fallback defaults.
pub fn calc_job_data(job: &Job, slice: &SliceSummary) -> Result<CostBreakdown, OpsError> {
    let printer = resolve_printer(&job.printer)?;
    let material_profiles = resolve_materials(&job.material)?;
    let default_material = job
        .default_material
        .as_ref()
        .map(resolve_material)
        .transpose()?;
    let defaults = disk::load_config()?;
    Ok(cost::calculate_cost(
        job,
        slice,
        &printer,
        &material_profiles,
        default_material.as_ref(),
        &defaults,
    )?)
}

/// Re-tiers an already-saved job up to `tier` (`Full` or `Bundled` only —
/// there's no way back down to `Default` once refs/source are embedded,
/// since the original library names and file path aren't tracked once
/// they're resolved away). Resolves any `Linked` printer/filament refs
/// against the library to embed them; for `Bundled`, also parses
/// `source.path` (if it isn't already `Embedded`) to embed the
/// `SliceSummary` too — the point being the result no longer needs the
/// library or the original sliced file at calc time.
pub fn retier_job(job_path: &Path, tier: JobTier) -> Result<Job, OpsError> {
    if tier == JobTier::Default {
        return Err(OpsError::Invalid(
            "can't re-tier to Default — a Full/Bundled job's linked names and original file path \
             aren't tracked anymore once embedded"
                .to_string(),
        ));
    }
    let job = load_job(job_path)?;

    let printer = ProfileRef::Embedded {
        profile: Box::new(resolve_printer(&job.printer)?),
    };
    let material = resolve_materials(&job.material)?
        .into_iter()
        .map(|(slot, profile)| {
            (
                slot,
                ProfileRef::Embedded {
                    profile: Box::new(profile),
                },
            )
        })
        .collect();
    let mut job = job;
    if tier == JobTier::Bundled {
        job.source = JobSource::Embedded(resolve_slice_summary(&job, job_path)?);
    }
    job.printer = printer;
    job.material = material;
    Ok(job)
}

fn resolve_slice_summary(job: &Job, job_path: &Path) -> Result<SliceSummary, OpsError> {
    match &job.source {
        JobSource::Embedded(summary) => Ok(summary.clone()),
        JobSource::Path(path) => {
            let resolved = if path.is_absolute() {
                path.clone()
            } else {
                job_path
                    .parent()
                    .unwrap_or_else(|| Path::new("."))
                    .join(path)
            };
            let bytes = fs::read(&resolved)?;
            let filename = resolved.file_name().and_then(|f| f.to_str()).unwrap_or("");
            Ok(formats::parse_slice_bytes(&bytes, filename)?)
        }
    }
}

fn resolve_printer(printer_ref: &ProfileRef<PrinterProfile>) -> Result<PrinterProfile, OpsError> {
    match printer_ref {
        ProfileRef::Embedded { profile } => Ok((**profile).clone()),
        ProfileRef::Linked { name } => show_printer(name),
    }
}

/// Turns a [`ProfileChoice`] into the [`ProfileRef`] a job of this tier
/// should store: `Default` links a name, the other tiers embed the loaded
/// profile. A `Profile` choice has no name to link, so it embeds at every
/// tier.
fn resolve_choice<T>(
    choice: ProfileChoice<T>,
    tier: JobTier,
    load: impl FnOnce(&str) -> Result<T, OpsError>,
) -> Result<ProfileRef<T>, OpsError> {
    Ok(match (choice, tier) {
        (ProfileChoice::Named(name), JobTier::Default) => ProfileRef::Linked { name },
        (ProfileChoice::Named(name), _) => ProfileRef::Embedded {
            profile: Box::new(load(&name)?),
        },
        (ProfileChoice::Profile(profile), _) => ProfileRef::Embedded { profile },
    })
}

fn resolve_material(
    material_ref: &ProfileRef<MaterialProfile>,
) -> Result<MaterialProfile, OpsError> {
    match material_ref {
        ProfileRef::Embedded { profile } => Ok((**profile).clone()),
        ProfileRef::Linked { name } => show_filament(name),
    }
}

fn resolve_materials(
    map: &HashMap<String, ProfileRef<MaterialProfile>>,
) -> Result<HashMap<String, MaterialProfile>, OpsError> {
    map.iter()
        .map(|(slot, r)| Ok((slot.clone(), resolve_material(r)?)))
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use libprintcalc::formats::slice_summary::{FilamentUsage, PlateSummary, SourceFormat};
    use libprintcalc::model::filament::MaterialKind;
    use libprintcalc::model::job::SINGLE_MATERIAL_SLOT;
    use libprintcalc::model::printer::{PrinterKind, WearProfile};

    fn sl1_fixture_path() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR")).join("../Testfiles/Shape-Box.sl1s")
    }

    /// `Testfiles/bundled-job-example.toml` — a real Bundled-tier job
    /// (embedded profiles + embedded slice summary from `Shape-Box.sl1s`),
    /// generated via the CLI itself with `--type bundled`. Calc'ing it
    /// needs no library and no original sliced file — checked here by
    /// simply not setting `PRINTCALC_CONFIG_DIR` at all. Doesn't assert
    /// `electricity_cost`/`total_cost`: those still fall back to whatever
    /// `~/.config/printcalc/config.toml` says (electricity price isn't
    /// baked into a job even at the Bundled tier), so asserting them here
    /// would make the test depend on ambient host config instead of this
    /// fixture.
    #[test]
    fn calc_job_resolves_the_bundled_example_fixture() {
        let path =
            Path::new(env!("CARGO_MANIFEST_DIR")).join("../Testfiles/bundled-job-example.toml");

        // Default (no override): both plates summed.
        // Tolerances here are 1e-4: reported costs are rounded to four
        // decimal places (see `cost::COST_DP`), so they can't match an
        // exact product to more than that.
        let breakdown = calc_job(&path, PlateOverride::Keep, Vec::new()).unwrap();
        assert!((breakdown.material_cost - 0.2654538075).abs() < 1e-4);
        assert_eq!(breakdown.material_breakdown.len(), 1);
        assert_eq!(
            breakdown.material_breakdown[0].filament_name,
            "prusament-resin-orange"
        );
        assert_eq!(breakdown.labor_cost, 3.75); // $15/hr * 15 minutes
        assert_eq!(breakdown.hardware_cost, 0.64); // 4 * $0.12 + 2 * $0.08
        assert_eq!(breakdown.hardware_breakdown.len(), 2);

        // --plate 1 override: just the first plate's material.
        let plate1 = calc_job(&path, PlateOverride::Only(1), Vec::new()).unwrap();
        assert!((plate1.material_cost - 0.136426395).abs() < 1e-4);

        // --plate 3 doesn't exist (only plates 1 and 2 are in this fixture).
        assert!(matches!(
            calc_job(&path, PlateOverride::Only(3), Vec::new()).unwrap_err(),
            OpsError::Calc(CalcError::PlateNotFound(3))
        ));
    }

    #[test]
    fn new_job_parses_slice_and_builds_default_tier_job() {
        let outcome = new_job(
            NewJobParams {
                name: "test-print".into(),
                slice_path: sl1_fixture_path(),
                printer: "sl1-printer".to_string().into(),
                filaments: HashMap::from([(
                    SINGLE_MATERIAL_SLOT.to_string(),
                    "resin-grey".to_string().into(),
                )]),
                plate_selection: None,
                labor_minutes: 10.0,
                labor_rate_per_hour: None,
                default_filament: None,
                quantity: 1,
                hardware: Vec::new(),
                electricity_cost_per_kwh_override: None,
                margin_percentages_override: None,
                material_efficiency_factor_override: None,
                tier: JobTier::Default,
            },
            parse_slice(&sl1_fixture_path()).unwrap(),
        )
        .unwrap();

        assert!(!outcome.plate_selection_ignored);
        assert_eq!(outcome.job.name, "test-print");
        assert_eq!(outcome.job.plate_selection, None);
        assert_eq!(
            outcome.job.printer,
            ProfileRef::Linked {
                name: "sl1-printer".into()
            }
        );
        assert_eq!(
            outcome.job.material[SINGLE_MATERIAL_SLOT],
            ProfileRef::Linked {
                name: "resin-grey".into()
            }
        );
    }

    #[test]
    fn new_job_warns_when_plate_selection_is_moot() {
        // Shape-Box.sl1s has exactly one plate — requesting plate 2 has
        // nothing to select, so it should be reported as ignored.
        let outcome = new_job(
            NewJobParams {
                name: "test-print".into(),
                slice_path: sl1_fixture_path(),
                printer: "sl1-printer".to_string().into(),
                filaments: HashMap::from([(
                    SINGLE_MATERIAL_SLOT.to_string(),
                    "resin-grey".to_string().into(),
                )]),
                plate_selection: Some(2),
                labor_minutes: 0.0,
                labor_rate_per_hour: None,
                default_filament: None,
                quantity: 1,
                hardware: Vec::new(),
                electricity_cost_per_kwh_override: None,
                margin_percentages_override: None,
                material_efficiency_factor_override: None,
                tier: JobTier::Default,
            },
            parse_slice(&sl1_fixture_path()).unwrap(),
        )
        .unwrap();

        assert!(outcome.plate_selection_ignored);
        assert_eq!(outcome.job.plate_selection, None);
    }

    /// The resin printer every test here prices against: a 400 machine
    /// over 5 years at 30% uptime, 20/yr maintenance, 60W, 1.2 buffer.
    fn sl1_wear() -> WearProfile {
        WearProfile::new()
            .with_purchase_price(400.0)
            .with_estimated_life_years(5.0)
            .with_estimated_uptime_percent(0.3)
            .with_annual_maintenance_cost(20.0)
            .with_power_consumption_watts(60.0)
            .with_cost_buffer_factor(1.2)
    }

    /// Test cleanup that stays honest: a missing path is fine, any other
    /// failure is a real problem worth failing the test over.
    fn remove_dir_if_present(path: &Path) {
        match fs::remove_dir_all(path) {
            Ok(()) => {}
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => panic!("couldn't remove {}: {e}", path.display()),
        }
    }

    fn remove_file_if_present(path: &Path) {
        match fs::remove_file(path) {
            Ok(()) => {}
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => panic!("couldn't remove {}: {e}", path.display()),
        }
    }

    fn temp_job_path(name: &str) -> PathBuf {
        std::env::temp_dir().join(format!("printcalc-ops-test-{name}.toml"))
    }

    /// Bundled-tier job (embedded summary + embedded profiles): exercises
    /// save_job/load_job and the Embedded branch of every resolve_* helper
    /// without touching the profile library at all.
    #[test]
    fn calc_job_resolves_bundled_job_from_disk() {
        let slice = SliceSummary::new(
            SourceFormat::PrusaSl1,
            vec![PlateSummary::new(
                1,
                3600.0,
                vec![FilamentUsage::ml(SINGLE_MATERIAL_SLOT, 10.0).with_material_type("Resin")],
            )],
        );
        let printer = PrinterProfile::new("sl1-printer", PrinterKind::Resin, sl1_wear());
        let resin = MaterialProfile::new("resin-grey", MaterialKind::Resin)
            .with_price(40.0)
            .with_efficiency_factor(1.0);
        let mut job = Job::new(
            "bundled",
            JobSource::Embedded(slice),
            ProfileRef::Embedded {
                profile: Box::new(printer),
            },
        )
        .with_labor(LaborCost::new(20.0).with_rate_per_hour(15.0))
        .with_hardware(vec![HardwareItem::new("resin vat film", 2.0)]);
        job.add_material(
            SINGLE_MATERIAL_SLOT,
            ProfileRef::Embedded {
                profile: Box::new(resin),
            },
        );

        let path = temp_job_path("bundled");
        save_job(&job, &path).unwrap();
        let breakdown = calc_job(&path, PlateOverride::Keep, Vec::new()).unwrap();
        remove_file_if_present(&path);

        // 10ml * 1.0 factor * ($40/1000) = $0.40
        assert!((breakdown.material_cost - 0.40).abs() < 1e-9);
        // 20 minutes @ $15/hr
        assert!((breakdown.labor_cost - 5.0).abs() < 1e-9);
        assert_eq!(breakdown.hardware_cost, 2.0);
    }

    /// Default-tier job (path source + linked refs): exercises the
    /// re-parse-at-calc-time path and library-backed profile resolution.
    /// Uses `PRINTCALC_CONFIG_DIR` to keep this off the real user config —
    /// no other test in this crate reads that env var, so it's safe to set
    /// process-wide here.
    #[test]
    fn calc_job_resolves_default_tier_job_against_library() {
        let config_dir = std::env::temp_dir().join("printcalc-ops-test-config-dir");
        remove_dir_if_present(&config_dir);
        // SAFETY: this is the only test in the binary that touches the
        // environment, and the test harness runs it on its own thread
        // without any other thread reading env vars concurrently.
        unsafe {
            std::env::set_var("PRINTCALC_CONFIG_DIR", &config_dir);
        }

        // Nothing saved yet — a built-in preset name still resolves.
        assert_eq!(show_printer("bambu-a1").unwrap().kind, PrinterKind::Fdm);
        assert!(list_printers().unwrap().contains(&"bambu-a1".to_string()));

        add_printer(&PrinterProfile::new(
            "sl1-printer",
            PrinterKind::Resin,
            sl1_wear(),
        ))
        .unwrap();
        add_filament(
            &MaterialProfile::new("resin-grey", MaterialKind::Resin)
                .with_price(40.0)
                .with_efficiency_factor(1.0),
        )
        .unwrap();
        let printers = list_printers().unwrap();
        assert!(printers.contains(&"sl1-printer".to_string()));
        assert!(printers.contains(&"bambu-a1".to_string())); // built-in, merged in
        let filaments = list_filaments().unwrap();
        assert!(filaments.contains(&"resin-grey".to_string()));
        assert!(filaments.contains(&"pla-black".to_string())); // built-in, merged in
        assert_eq!(
            show_printer("sl1-printer").unwrap().kind,
            PrinterKind::Resin
        );

        // A saved profile shadows a built-in preset under the same name.
        add_printer(&PrinterProfile::new(
            "bambu-a1",
            PrinterKind::Fdm,
            builtin::printer("bambu-a1")
                .unwrap()
                .wear
                .with_purchase_price(999.0),
        ))
        .unwrap();
        assert_eq!(show_printer("bambu-a1").unwrap().wear.purchase_price, 999.0);

        let outcome = new_job(
            NewJobParams {
                name: "test-print".into(),
                slice_path: sl1_fixture_path(),
                printer: "sl1-printer".to_string().into(),
                filaments: HashMap::from([(
                    SINGLE_MATERIAL_SLOT.to_string(),
                    "resin-grey".to_string().into(),
                )]),
                plate_selection: None,
                labor_minutes: 6.0,
                labor_rate_per_hour: None,
                default_filament: None,
                quantity: 1,
                hardware: Vec::new(),
                electricity_cost_per_kwh_override: None,
                margin_percentages_override: None,
                material_efficiency_factor_override: None,
                tier: JobTier::Default,
            },
            parse_slice(&sl1_fixture_path()).unwrap(),
        )
        .unwrap();

        let path = temp_job_path("default-tier");
        save_job(&outcome.job, &path).unwrap();
        let breakdown = calc_job(&path, PlateOverride::Keep, Vec::new()).unwrap();

        // Re-tiering: Default -> Bundled embeds both the profiles and the
        // parsed slice summary; the retiered job needs neither the
        // library nor the original sliced file to calc the same result.
        let bundled = retier_job(&path, JobTier::Bundled).unwrap();
        assert!(matches!(bundled.printer, ProfileRef::Embedded { .. }));
        assert!(matches!(
            bundled.material[SINGLE_MATERIAL_SLOT],
            ProfileRef::Embedded { .. }
        ));
        assert!(matches!(bundled.source, JobSource::Embedded(_)));
        let bundled_path = temp_job_path("bundled-retiered");
        save_job(&bundled, &bundled_path).unwrap();
        remove_dir_if_present(&config_dir); // library gone...
        // SAFETY: as above — no concurrent reader of the environment.
        unsafe {
            std::env::remove_var("PRINTCALC_CONFIG_DIR");
        }
        let bundled_breakdown = calc_job(&bundled_path, PlateOverride::Keep, Vec::new()).unwrap(); // ...still calcs fine
        assert_eq!(bundled_breakdown.material_cost, breakdown.material_cost);

        assert!(matches!(
            retier_job(&path, JobTier::Default),
            Err(OpsError::Invalid(_))
        ));

        remove_file_if_present(&path);
        remove_file_if_present(&bundled_path);

        // Shape-Box.sl1s uses 2.598598 ml of resin.
        // Rounded to 4dp on the way out, so not an exact product.
        assert!((breakdown.material_cost - 2.598598 * (40.0 / 1000.0)).abs() < 1e-4);
    }
}