atlas-cli 0.2.0

Machine Learning Lifecycle & Transparency Manager - Create and verify manifests for ML models and datasets
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
use crate::cc_attestation;
use crate::error::{Error, Result};
use crate::hash;
use crate::in_toto;
use crate::manifest::config::ManifestCreationConfig;
use crate::manifest::utils::{
    determine_dataset_type, determine_format, determine_model_type, determine_software_type,
};
use crate::signing::signable::Signable;
use crate::storage::traits::{ArtifactLocation, StorageBackend};
use atlas_c2pa_lib::assertion::{
    Action, ActionAssertion, Assertion, Author, CreativeWorkAssertion, CustomAssertion,
};
use atlas_c2pa_lib::asset_type::AssetType;
use atlas_c2pa_lib::claim::ClaimV2;
use atlas_c2pa_lib::cose::HashAlgorithm;
use atlas_c2pa_lib::cross_reference::CrossReference;
use atlas_c2pa_lib::datetime_wrapper::OffsetDateTimeWrapper;
use atlas_c2pa_lib::ingredient::{Ingredient, IngredientData};
use atlas_c2pa_lib::manifest::Manifest;
use serde_json::{to_string, to_string_pretty};
use std::path::{Path, PathBuf};
use tdx_workload_attestation::get_platform_name;
use time::OffsetDateTime;
use uuid::Uuid;

const CLAIM_GENERATOR: &str = "atlas-cli:0.2.0";

/// Asset type enum to distinguish between models, datasets, software, and evaluations
pub enum AssetKind {
    Model,
    Dataset,
    Software,
    Evaluation,
}

/// Generates C2PA Assertions based on the asset kind and configuration.
///
/// This function creates standardized C2PA assertions including creative work and action assertions
/// that are tailored to the specific type of asset being attested (model, dataset, software, or evaluation).
/// It also optionally includes confidential computing (CC) attestations when enabled.
fn generate_c2pa_assertions(
    config: &ManifestCreationConfig,
    asset_kind: AssetKind,
) -> Result<Vec<Assertion>> {
    // Determine asset-specific values
    let (creative_type, digital_source_type) = match asset_kind {
        AssetKind::Model => (
            "Model".to_string(),
            "http://cv.iptc.org/newscodes/digitalsourcetype/algorithmicMedia".to_string(),
        ),
        AssetKind::Dataset => (
            "Dataset".to_string(),
            "http://cv.iptc.org/newscodes/digitalsourcetype/dataset".to_string(),
        ),
        AssetKind::Software => (
            "Software".to_string(),
            "http://cv.iptc.org/newscodes/digitalsourcetype/software".to_string(),
        ),
        AssetKind::Evaluation => (
            "EvaluationResult".to_string(),
            "http://cv.iptc.org/newscodes/digitalsourcetype/evaluationResult".to_string(),
        ),
    };

    // Create assertions
    let mut assertions = vec![
        Assertion::CreativeWork(CreativeWorkAssertion {
            context: "http://schema.org/".to_string(),
            creative_type,
            author: vec![
                Author {
                    author_type: "Organization".to_string(),
                    name: config
                        .author_org
                        .clone()
                        .unwrap_or_else(|| "Organization".to_string()),
                },
                Author {
                    author_type: "Person".to_string(),
                    name: config
                        .author_name
                        .clone()
                        .unwrap_or_else(|| "Unknown".to_string()),
                },
            ],
        }),
        Assertion::Action(ActionAssertion {
            actions: vec![Action {
                action: match asset_kind {
                    AssetKind::Evaluation => "c2pa.evaluation".to_string(),
                    _ => "c2pa.created".to_string(),
                },
                software_agent: Some(CLAIM_GENERATOR.to_string()),
                parameters: Some(match asset_kind {
                    AssetKind::Evaluation => {
                        // Merge evaluation parameters with standard parameters
                        let mut params = serde_json::json!({
                            "name": config.name,
                            "description": config.description,
                            "author": {
                                "organization": config.author_org,
                                "name": config.author_name
                            }
                        });

                        // Add evaluation-specific parameters if present
                        if let Some(config_params) = &config.custom_fields {
                            if let Some(eval_params) = config_params.get("evaluation") {
                                if let Some(obj) = params.as_object_mut() {
                                    obj.insert(
                                        "model_id".to_string(),
                                        eval_params
                                            .get("model_id")
                                            .cloned()
                                            .unwrap_or(serde_json::Value::Null),
                                    );
                                    obj.insert(
                                        "dataset_id".to_string(),
                                        eval_params
                                            .get("dataset_id")
                                            .cloned()
                                            .unwrap_or(serde_json::Value::Null),
                                    );
                                    obj.insert(
                                        "metrics".to_string(),
                                        eval_params
                                            .get("metrics")
                                            .cloned()
                                            .unwrap_or(serde_json::Value::Null),
                                    );
                                }
                            }
                        }
                        params
                    }
                    AssetKind::Software => {
                        let mut params = serde_json::json!({
                            "name": config.name,
                            "description": config.description,
                            "author": {
                                "organization": config.author_org,
                                "name": config.author_name
                            }
                        });

                        if let Some(software_type) = &config.software_type {
                            params.as_object_mut().unwrap().insert(
                                "software_type".to_string(),
                                serde_json::Value::String(software_type.clone()),
                            );
                        }
                        if let Some(version) = &config.version {
                            params.as_object_mut().unwrap().insert(
                                "version".to_string(),
                                serde_json::Value::String(version.clone()),
                            );
                        }
                        params
                    }
                    // don't need to repeat info for created action assertions that's
                    // already in the CreativeWork assertion
                    _ => serde_json::json!({}),
                }),
                digital_source_type: Some(digital_source_type),
                instance_id: None,
            }],
        }),
    ];

    // if we're creating the manifest in a CC environment, create
    // an assertion for the CC attestation
    if config.with_cc {
        // the assertion contents will depend on the detected platform
        let cc_assertion = get_cc_attestation_assertion().unwrap();

        assertions.push(Assertion::CustomAssertion(cc_assertion));
    }

    Ok(assertions)
}

/// Generates a C2PA claim with ingredients and assertions.
///
/// This function creates a complete C2PA claim by generating ingredients from the provided file paths,
/// sorting them alphabetically (as required by the OpenSSF Model Signing specification), and combining
/// them with generated assertions. The claim includes metadata such as instance ID, creation timestamp,
/// and claim generator information.
fn generate_c2pa_claim(config: &ManifestCreationConfig, asset_kind: AssetKind) -> Result<ClaimV2> {
    // Create ingredients using the helper function
    let mut ingredients = Vec::new();

    for (path, ingredient_name) in config.paths.iter().zip(config.ingredient_names.iter()) {
        // Determine asset type and format based on asset kind
        let format = determine_format(path)?;
        let asset_type = match asset_kind {
            AssetKind::Model => determine_model_type(path)?,
            AssetKind::Dataset => determine_dataset_type(path)?,
            AssetKind::Software => determine_software_type(path)?,
            AssetKind::Evaluation => AssetType::Dataset, // Use Dataset type for evaluation results
        };

        // Use the helper function to create the ingredient
        let ingredient = create_ingredient_from_path_with_algorithm(
            path,
            ingredient_name,
            asset_type,
            format,
            &config.hash_alg,
        )?;
        ingredients.push(ingredient);
    }

    // Per the OMS spec, ingredients must be hashed in alphabetical order of the
    // artifact name, so always canonicalize the order regardless of format
    // because the manifest must provide references to all artifacts needed to
    // recompute the model hash.
    // See https://github.com/sigstore/model-transparency/blob/de2f935ad437218d577a3f39378c482bf3aafcec/src/model_signing/_signing/signing.py#L188-L192
    ingredients.sort_by_key(|ingredient| ingredient.title.to_lowercase());

    let assertions = generate_c2pa_assertions(config, asset_kind)?;

    // Create claim
    Ok(ClaimV2 {
        instance_id: format!("urn:c2pa:{}", Uuid::new_v4()),
        ingredients: ingredients.clone(),
        created_assertions: assertions,
        claim_generator_info: CLAIM_GENERATOR.to_string(),
        signature: None,
        created_at: OffsetDateTimeWrapper(OffsetDateTime::now_utc()),
    })
}

/// Creates a manifest for a model, dataset, software, or evaluation
pub fn create_manifest(config: ManifestCreationConfig, asset_kind: AssetKind) -> Result<()> {
    let claim = generate_c2pa_claim(&config, asset_kind)?;

    // Create the manifest
    let mut manifest = Manifest {
        claim_generator: CLAIM_GENERATOR.to_string(),
        title: config.name.clone(),
        instance_id: format!("urn:c2pa:{}", Uuid::new_v4()),
        claim: claim.clone(),
        ingredients: vec![],
        created_at: OffsetDateTimeWrapper(OffsetDateTime::now_utc()),
        cross_references: vec![],
        claim_v2: Some(claim),
        is_active: true,
    };

    // Sign if key is provided
    if let Some(key_file) = &config.key_path {
        manifest.sign(key_file.to_path_buf(), config.hash_alg)?;
    }

    if let Some(manifest_ids) = &config.linked_manifests {
        if let Some(storage_backend) = &config.storage {
            for linked_id in manifest_ids {
                match storage_backend.retrieve_manifest(linked_id) {
                    Ok(linked_manifest) => {
                        // Create a JSON representation of the linked manifest
                        let linked_json = serde_json::to_string(&linked_manifest)
                            .map_err(|e| Error::Serialization(e.to_string()))?;

                        // Create a hash of the linked manifest
                        let linked_hash = hash::calculate_hash(linked_json.as_bytes());

                        // Create a cross-reference
                        let cross_ref = CrossReference {
                            manifest_url: linked_id.clone(),
                            manifest_hash: linked_hash,
                            media_type: Some("application/json".to_string()),
                        };

                        // Add the cross-reference to the manifest
                        manifest.cross_references.push(cross_ref);

                        println!("Added link to manifest: {linked_id}");
                    }
                    Err(e) => {
                        println!("Warning: Could not link to manifest {linked_id}: {e}");
                    }
                }
            }
        } else {
            println!("Warning: Cannot link manifests without a storage backend");
        }
    }

    // Output manifest if requested
    if config.print || config.storage.is_none() {
        match config.output_encoding.to_lowercase().as_str() {
            "json" => {
                let manifest_json =
                    to_string_pretty(&manifest).map_err(|e| Error::Serialization(e.to_string()))?;
                println!("{manifest_json}");
            }
            "cbor" => {
                let manifest_cbor = serde_cbor::to_vec(&manifest)
                    .map_err(|e| Error::Serialization(e.to_string()))?;
                println!("{}", hex::encode(&manifest_cbor));
            }
            _ => {
                return Err(Error::Validation(format!(
                    "Invalid output encoding '{}'. Valid options are: json, cbor",
                    config.output_encoding
                )));
            }
        }
    }

    // Store manifest if storage is provided
    if let Some(storage) = &config.storage {
        if !config.print {
            let id = storage.store_manifest(&manifest)?;
            println!("Manifest stored successfully with ID: {id}");
        }
    }

    Ok(())
}

/// Creates an OpenSSF Model Signing (OMS) compliant C2PA manifest for a model.
///
/// This function generates a manifest that conforms to the OpenSSF Model Signing specification,
/// creating an in-toto format Statement with a DSSE (Dead Simple Signing Envelope). The manifest
/// is specifically designed for model artifacts and includes proper subject hash calculation
/// according to OMS requirements.
///
/// # Arguments
///
/// * `config` - The manifest creation configuration, must include a signing key for OMS format
///
/// # Returns
///
/// `Ok(())` on successful manifest creation, or an error if creation fails.
///
/// # Errors
///
/// Returns an error if:
/// - No signing key is provided (OMS format requires signing)
/// - Subject hash calculation fails
/// - Manifest serialization fails
/// - Storage operations fail
///
/// # Examples
///
/// ```no_run
/// use atlas_cli::manifest::config::ManifestCreationConfig;
/// use atlas_cli::manifest::common::create_oms_manifest;
/// use atlas_c2pa_lib::cose::HashAlgorithm;
/// use std::path::PathBuf;
///
/// let config = ManifestCreationConfig {
///     name: "test-model".to_string(),
///     description: Some("A test model".to_string()),
///     author_name: Some("Test Author".to_string()),
///     author_org: Some("Test Org".to_string()),
///     paths: vec![PathBuf::from("model.onnx")],
///     ingredient_names: vec!["model".to_string()],
///     hash_alg: HashAlgorithm::Sha384,
///     key_path: Some(PathBuf::from("private_key.pem")),
///     output_encoding: "json".to_string(),
///     print: true,
///     storage: None,
///     with_cc: false,
///     linked_manifests: None,
///     custom_fields: None,
///     software_type: None,
///     version: None,
/// };
///
/// create_oms_manifest(config).unwrap();
/// ```
pub fn create_oms_manifest(config: ManifestCreationConfig) -> Result<()> {
    let claim = generate_c2pa_claim(&config, AssetKind::Model)?;

    // Create the manifest
    let mut manifest = Manifest {
        claim_generator: "".to_string(),
        title: "".to_string(),
        instance_id: format!("urn:c2pa:{}", Uuid::new_v4()),
        claim: claim.clone(),
        ingredients: vec![],
        created_at: OffsetDateTimeWrapper(OffsetDateTime::now_utc()),
        cross_references: vec![],
        claim_v2: None,
        is_active: true,
    };

    if let Some(manifest_ids) = &config.linked_manifests {
        if let Some(storage_backend) = &config.storage {
            for linked_id in manifest_ids {
                match storage_backend.retrieve_manifest(linked_id) {
                    Ok(linked_manifest) => {
                        // Create a JSON representation of the linked manifest
                        let linked_json = serde_json::to_string(&linked_manifest)
                            .map_err(|e| Error::Serialization(e.to_string()))?;

                        // Create a hash of the linked manifest
                        let linked_hash = hash::calculate_hash(linked_json.as_bytes());

                        // Create a cross-reference
                        let cross_ref = CrossReference {
                            manifest_url: linked_id.clone(),
                            manifest_hash: linked_hash,
                            media_type: Some("application/json".to_string()),
                        };

                        // Add the cross-reference to the manifest
                        manifest.cross_references.push(cross_ref);

                        println!("Added link to manifest: {linked_id}");
                    }
                    Err(e) => {
                        println!("Warning: Could not link to manifest {linked_id}: {e}");
                    }
                }
            }
        } else {
            println!("Warning: Cannot link manifests without a storage backend");
        }
    }

    // Generate the in-toto format Statement and sign the DSSE

    // we need to convert this into a string to serialize into the Struct proto expected by in-toto
    let manifest_json = to_string(&manifest).map_err(|e| Error::Serialization(e.to_string()))?;
    let manifest_proto = in_toto::json_to_struct_proto(&manifest_json)?;

    let subject_hash = generate_oms_subject_hash(&manifest, &config.hash_alg)?;

    let subject = in_toto::make_minimal_resource_descriptor(
        &config.name,
        hash::algorithm_to_string(&config.hash_alg),
        &subject_hash,
    );

    let key_path = config
        .key_path
        .ok_or_else(|| Error::Validation("OMS format requires a signing key".to_string()))?;

    let envelope = in_toto::generate_signed_statement_v1(
        &[subject],
        "https://spec.c2pa.org/specifications/specifications/2.2",
        &manifest_proto,
        key_path.to_path_buf(),
        config.hash_alg,
    )?;

    // Output manifest if requested
    if config.print || config.storage.is_none() {
        match config.output_encoding.to_lowercase().as_str() {
            "json" => {
                let envelope_json =
                    to_string_pretty(&envelope).map_err(|e| Error::Serialization(e.to_string()))?;
                println!("{envelope_json}");
            }
            "cbor" => {
                let envelope_cbor = serde_cbor::to_vec(&envelope)
                    .map_err(|e| Error::Serialization(e.to_string()))?;
                println!("{}", hex::encode(&envelope_cbor));
            }
            _ => {
                return Err(Error::Validation(format!(
                    "Invalid output encoding '{}'. Valid options are: json, cbor",
                    config.output_encoding
                )));
            }
        }
    }

    // Store manifest if storage is provided
    if let Some(storage) = &config.storage {
        if !config.print {
            let id = storage.store_manifest(&manifest)?;
            println!("Manifest stored successfully with ID: {id}");
        }
    }

    Ok(())
}

/// Lists manifests from storage, optionally filtered by asset type.
///
/// This function retrieves all manifests from the provided storage backend and optionally
/// filters them by asset kind (Model, Dataset, Software, or Evaluation). The filtered
/// manifests are then displayed with their metadata including ID, name, type, and creation time.
///
/// # Arguments
///
/// * `storage` - The storage backend to retrieve manifests from
/// * `asset_kind` - Optional filter for asset type; if None, all manifests are listed
///
/// # Returns
///
/// `Ok(())` on successful listing, or an error if storage retrieval fails.
///
/// # Examples
///
/// ```no_run
/// use atlas_cli::manifest::common::{AssetKind, list_manifests};
/// use atlas_cli::storage::traits::StorageBackend;
/// use atlas_cli::storage::filesystem::FilesystemStorage;
///
/// // Create or obtain a storage backend instance
/// let storage_backend: FilesystemStorage = FilesystemStorage::new("/path/to/storage").unwrap();
///
/// // List all manifests
/// list_manifests(&storage_backend, None).unwrap();
///
/// // List only model manifests
/// list_manifests(&storage_backend, Some(AssetKind::Model)).unwrap();
/// ```
pub fn list_manifests(storage: &dyn StorageBackend, asset_kind: Option<AssetKind>) -> Result<()> {
    let manifests = storage.list_manifests()?;

    // Filter manifests by type if asset_kind is specified
    let filtered_manifests = if let Some(kind) = asset_kind {
        manifests
            .into_iter()
            .filter(|m| match kind {
                AssetKind::Model => {
                    matches!(m.manifest_type, crate::storage::traits::ManifestType::Model)
                }
                AssetKind::Dataset => matches!(
                    m.manifest_type,
                    crate::storage::traits::ManifestType::Dataset
                ),
                AssetKind::Software => matches!(
                    m.manifest_type,
                    crate::storage::traits::ManifestType::Software
                ),
                AssetKind::Evaluation => {
                    // Check if manifest title or name contains "Evaluation"
                    m.name.contains("Evaluation") || m.name.contains("evaluation")
                }
            })
            .collect::<Vec<_>>()
    } else {
        manifests
    };

    // Display the manifests
    for metadata in filtered_manifests {
        println!(
            "Manifest: {} (ID: {}, Type: {:?}, Created: {})",
            metadata.name, metadata.id, metadata.manifest_type, metadata.created_at
        );
    }

    Ok(())
}

/// Performs comprehensive verification of a manifest.
///
/// This function verifies a manifest by performing multiple validation steps:
/// 1. Validates the manifest structure against C2PA specifications
/// 2. Verifies hash integrity of all ingredients (file-based and URL-based)
/// 3. Validates cross-references to linked manifests
/// 4. Checks asset-specific requirements based on manifest type
///
/// The verification process ensures that the manifest is structurally valid and that
/// all referenced artifacts maintain their integrity since the manifest was created.
///
/// # Arguments
///
/// * `id` - The unique identifier of the manifest to verify
/// * `storage` - The storage backend to retrieve the manifest and linked manifests
///
/// # Returns
///
/// `Ok(())` if verification succeeds, or an error describing the verification failure.
///
/// # Errors
///
/// Returns an error if:
/// - Manifest cannot be retrieved from storage
/// - Manifest structure is invalid
/// - Any ingredient hash verification fails
/// - Cross-reference verification fails
/// - Asset-specific requirements are not met
///
/// # Examples
///
/// ```no_run
/// use atlas_cli::manifest::common::verify_manifest;
/// use atlas_cli::storage::traits::StorageBackend;
/// use atlas_cli::storage::filesystem::FilesystemStorage;
///
/// // Create or obtain a storage backend instance
/// let storage_backend: FilesystemStorage = FilesystemStorage::new("/path/to/storage").unwrap();
///
/// let manifest_id = "manifest-123";
/// verify_manifest(manifest_id, &storage_backend).unwrap();
/// println!("✓ Manifest verification successful");
/// ```
pub fn verify_manifest(id: &str, storage: &dyn StorageBackend) -> Result<()> {
    let manifest = storage.retrieve_manifest(id)?;

    // Step 1: Verify the manifest structure
    atlas_c2pa_lib::manifest::validate_manifest(&manifest)
        .map_err(|e| crate::error::Error::Validation(e.to_string()))?;

    println!("Verifying manifest with ID: {id}");

    // Step 2: Verify each ingredient's hash
    for ingredient in &manifest.ingredients {
        println!("Verifying ingredient: {}", ingredient.title);

        if ingredient.data.url.starts_with("file://") {
            let path = PathBuf::from(ingredient.data.url.trim_start_matches("file://"));

            // Create ArtifactLocation for verification
            let location = ArtifactLocation {
                url: ingredient.data.url.clone(),
                file_path: Some(path),
                hash: ingredient.data.hash.clone(),
            };

            // Verify the hash and handle the result
            match location.verify() {
                Ok(true) => {
                    println!(
                        "✓ Successfully verified hash for component: {}",
                        ingredient.title
                    );
                }
                Ok(false) => {
                    return Err(Error::Validation(format!(
                        "Hash verification failed for component: {}. The file may have been modified.",
                        ingredient.title
                    )));
                }
                Err(e) => {
                    return Err(Error::Validation(format!(
                        "Error verifying component {}: {}. The file may be missing or inaccessible.",
                        ingredient.title, e
                    )));
                }
            }
        } else {
            // For non-file URLs, try direct hash verification
            match hash::calculate_file_hash(PathBuf::from(&ingredient.data.url)) {
                Ok(calculated_hash) => {
                    if calculated_hash != ingredient.data.hash {
                        return Err(Error::Validation(format!(
                            "Hash mismatch for ingredient: {}",
                            ingredient.title
                        )));
                    }
                    println!(
                        "✓ Successfully verified hash for component: {}",
                        ingredient.title
                    );
                }
                Err(_) => {
                    println!(
                        "âš  Warning: Component {} does not use file:// URL scheme and could not be verified directly",
                        ingredient.title
                    );
                }
            }
        }
    }

    // Step 3: Verify cross-references if present
    if !manifest.cross_references.is_empty() {
        println!("Verifying cross-references...");

        for cross_ref in &manifest.cross_references {
            let linked_manifest = storage.retrieve_manifest(&cross_ref.manifest_url)?;
            let manifest_json = serde_json::to_string(&linked_manifest)
                .map_err(|e| Error::Serialization(e.to_string()))?;
            let algorithm = hash::detect_hash_algorithm(&cross_ref.manifest_hash);
            let calculated_hash =
                hash::calculate_hash_with_algorithm(manifest_json.as_bytes(), &algorithm);

            if calculated_hash != cross_ref.manifest_hash {
                return Err(Error::Validation(format!(
                    "Cross-reference verification failed for linked manifest: {}. Hash mismatch: stored={}, calculated={}",
                    cross_ref.manifest_url, cross_ref.manifest_hash, calculated_hash
                )));
            }
            println!(
                "✓ Verified cross-reference to manifest: {}",
                cross_ref.manifest_url
            );
        }
    }

    // Step 4: Verify asset-specific requirements
    verify_asset_specific_requirements(&manifest)?;

    println!("✓ Manifest verification successful");
    Ok(())
}

// Verify asset-specific requirements based on the manifest content
fn verify_asset_specific_requirements(manifest: &Manifest) -> Result<()> {
    // Determines the asset type from the manifest contents
    let is_dataset = is_dataset_manifest(manifest);
    let is_model = is_model_manifest(manifest);
    let is_software = is_software_manifest(manifest);
    let is_evaluation = is_evaluation_manifest(manifest);

    // Verify that at least one ingredient exists (except for evaluations)
    if !is_evaluation && manifest.ingredients.is_empty() {
        return Err(Error::Validation(
            "Manifest must contain at least one ingredient".to_string(),
        ));
    }

    // Check for dataset, model, software, or evaluation assertion
    if let Some(claim) = &manifest.claim_v2 {
        if is_dataset {
            let has_dataset_assertion = claim.created_assertions.iter().any(|assertion| {
                matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Dataset")
            });

            let has_dataset_assertion_in_claim = if !has_dataset_assertion {
                manifest.claim.created_assertions.iter().any(|assertion| {
                    matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Dataset")
                })
            } else {
                false
            };

            if !has_dataset_assertion && !has_dataset_assertion_in_claim {
                println!(
                    "WARNING: Dataset manifest doesn't contain a Dataset creative work assertion"
                );

                return Err(Error::Validation(
                    "Dataset manifest must contain a Dataset creative work assertion".to_string(),
                ));
            }
        }

        if is_model {
            let has_model_assertion = claim.created_assertions.iter().any(|assertion| {
                matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Model")
            });

            let has_model_assertion_in_claim = if !has_model_assertion {
                manifest.claim.created_assertions.iter().any(|assertion| {
                    matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Model")
                })
            } else {
                false
            };

            if !has_model_assertion && !has_model_assertion_in_claim {
                println!("WARNING: Model manifest doesn't contain a Model creative work assertion");

                return Err(Error::Validation(
                    "Model manifest must contain a Model creative work assertion".to_string(),
                ));
            }
        }

        if is_software {
            let has_software_assertion = claim.created_assertions.iter().any(|assertion| {
                matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Software")
            });

            let has_software_parameters = claim.created_assertions.iter().any(|assertion| {
                if let Assertion::Action(action_assertion) = assertion {
                    action_assertion.actions.iter().any(|action| {
                        if let Some(params) = &action.parameters {
                            params.get("software_type").is_some()
                        } else {
                            false
                        }
                    })
                } else {
                    false
                }
            });

            if !has_software_assertion && !has_software_parameters {
                println!(
                    "WARNING: Software manifest doesn't contain a Software creative work assertion or software_type parameter"
                );

                return Err(Error::Validation(
                    "Software manifest must contain a Software creative work assertion or software_type parameter".to_string(),
                ));
            }
        }

        if is_evaluation {
            let has_evaluation_assertion = claim.created_assertions.iter().any(|assertion| {
                matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "EvaluationResult")
            });

            if !has_evaluation_assertion {
                println!(
                    "WARNING: Evaluation manifest doesn't contain an EvaluationResult creative work assertion"
                );

                return Err(Error::Validation(
                    "Evaluation manifest must contain an EvaluationResult creative work assertion"
                        .to_string(),
                ));
            }
        }
    }

    Ok(())
}

// Helper function to determine if a manifest is for a dataset
fn is_dataset_manifest(manifest: &Manifest) -> bool {
    // Check if it's an evaluation manifest - if so, it's NOT a dataset
    if is_evaluation_manifest(manifest) {
        return false;
    }

    // Now proceed with the regular dataset checking
    let has_dataset_ingredients = manifest.ingredients.iter().any(|ingredient| {
        ingredient.data.data_types.iter().any(|t| {
            matches!(
                t,
                AssetType::Dataset
                    | AssetType::DatasetOnnx
                    | AssetType::DatasetTensorFlow
                    | AssetType::DatasetPytorch
            )
        })
    });

    let has_dataset_assertion = if let Some(claim) = &manifest.claim_v2 {
        claim.created_assertions.iter().any(|assertion| {
            matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Dataset")
        })
    } else {
        false
    };

    has_dataset_ingredients || has_dataset_assertion
}

// Helper function to determine if a manifest is for a model
fn is_model_manifest(manifest: &Manifest) -> bool {
    // Check if any ingredients have model type
    let has_model_ingredients = manifest.ingredients.iter().any(|ingredient| {
        ingredient.data.data_types.iter().any(|t| {
            matches!(
                t,
                AssetType::Model
                    | AssetType::ModelOnnx
                    | AssetType::ModelTensorFlow
                    | AssetType::ModelPytorch
                    | AssetType::ModelOpenVino
            )
        })
    });

    // Check for model assertion
    let has_model_assertion = if let Some(claim) = &manifest.claim_v2 {
        claim.created_assertions.iter().any(|assertion| {
            matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Model")
        })
    } else if let Some(Assertion::CreativeWork(creative_work)) = manifest
        .claim
        .created_assertions
        .iter()
        .find(|a| matches!(a, Assertion::CreativeWork(_)))
    {
        // Check in the old claim field as a fallback
        creative_work.creative_type == "Model"
    } else {
        false
    };

    // Returns true if either condition is met
    has_model_ingredients || has_model_assertion
}

// Helper function to check if a manifest is a software manifest
fn is_software_manifest(manifest: &Manifest) -> bool {
    // Check if any ingredients have software type
    let has_software_ingredients = manifest.ingredients.iter().any(|ingredient| {
        ingredient
            .data
            .data_types
            .iter()
            .any(|t| matches!(t, AssetType::Generator))
    });

    // Check for software assertion
    let has_software_assertion = if let Some(claim) = &manifest.claim_v2 {
        claim.created_assertions.iter().any(|assertion| {
            matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "Software")
        })
    } else {
        false
    };

    // Check for software parameters in actions
    let has_software_parameters = if let Some(claim) = &manifest.claim_v2 {
        claim.created_assertions.iter().any(|assertion| {
            if let Assertion::Action(action_assertion) = assertion {
                action_assertion.actions.iter().any(|action| {
                    if let Some(params) = &action.parameters {
                        params.get("software_type").is_some()
                    } else {
                        false
                    }
                })
            } else {
                false
            }
        })
    } else {
        false
    };

    has_software_ingredients || has_software_assertion || has_software_parameters
}

// Helper function to check if a manifest is an evaluation manifest
fn is_evaluation_manifest(manifest: &Manifest) -> bool {
    if let Some(claim) = &manifest.claim_v2 {
        claim.created_assertions.iter().any(|assertion| {
            matches!(assertion, Assertion::CreativeWork(creative_work) if creative_work.creative_type == "EvaluationResult")
        })
    } else {
        false
    }
}

/// Create a C2PA Ingredient from a path
pub fn create_ingredient_from_path(
    path: &Path,
    name: &str,
    asset_type: AssetType,
    format: String,
) -> Result<Ingredient> {
    create_ingredient_from_path_with_algorithm(
        path,
        name,
        asset_type,
        format,
        &HashAlgorithm::Sha384,
    )
}

/// Create a C2PA Ingredient from a path with a specified hash algorithm
pub fn create_ingredient_from_path_with_algorithm(
    path: &Path,
    name: &str,
    asset_type: AssetType,
    format: String,
    algorithm: &HashAlgorithm,
) -> Result<Ingredient> {
    let ingredient_data = IngredientData {
        url: format!("file://{}", path.to_string_lossy()),
        alg: algorithm.as_str().to_string(),
        hash: hash::calculate_file_hash_with_algorithm(path, algorithm)?,
        data_types: vec![asset_type],
        linked_ingredient_url: None,
        linked_ingredient_hash: None,
    };

    Ok(Ingredient {
        title: name.to_string(),
        format,
        relationship: "componentOf".to_string(),
        document_id: format!("uuid:{}", Uuid::new_v4()),
        instance_id: format!("uuid:{}", Uuid::new_v4()),
        data: ingredient_data,
        linked_ingredient: None,
        public_key: None,
    })
}

/// Helper function to generate a CC attestation assertion
fn get_cc_attestation_assertion() -> Result<CustomAssertion> {
    let report = match cc_attestation::get_report(false) {
        Ok(r) => r,
        Err(e) => {
            return Err(Error::CCAttestationError(format!(
                "Failed to get attestation: {e}"
            )));
        }
    };

    // detect the underlying CC platform
    let platform = match get_platform_name() {
        Ok(p) => p,
        Err(e) => {
            return Err(Error::CCAttestationError(format!(
                "Error detecting attestation platform: {e}"
            )));
        }
    };

    let cc_assertion = CustomAssertion {
        label: platform,
        data: serde_json::Value::String(report),
    };

    Ok(cc_assertion)
}

// Compute the OMS subject hash as specified in https://github.com/sigstore/model-transparency/blob/de2f935ad437218d577a3f39378c482bf3aafcec/src/model_signing/_signing/signing.py#L181-L186
fn generate_oms_subject_hash(manifest: &Manifest, hash_alg: &HashAlgorithm) -> Result<String> {
    // generate the hash over all ingredient hashes for the model
    if manifest.claim.ingredients.is_empty() {
        return Err(Error::Validation(
            "OMS requires at least one ingredient".to_string(),
        ));
    }

    // Per the OMS spec, the ingredients must be hashed in a canonical order
    // (alphabetical order of artifact name)
    // Since we cannot assume that the ingredients in the manifest are sorted
    // as expected (e.g., during verification), we sort every time we hash.
    let mut ingredients_to_hash = manifest.claim.ingredients.clone();
    ingredients_to_hash.sort_by_key(|ingredient| ingredient.title.to_lowercase());

    let mut ingredient_hashes: Vec<u8> = Vec::new();
    for ingredient in &ingredients_to_hash {
        let raw_bytes = hex::decode(&ingredient.data.hash).map_err(|e| {
            Error::Validation(format!(
                "Invalid hash for ingredient {}: {}",
                ingredient.title, e
            ))
        })?;
        ingredient_hashes.extend_from_slice(&raw_bytes);
    }

    Ok(hash::calculate_hash_with_algorithm(
        &ingredient_hashes,
        hash_alg,
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::signing::test_utils::generate_temp_key;

    fn make_test_manifest_config() -> ManifestCreationConfig {
        let (_secure_key, tmp_dir) = generate_temp_key().unwrap();

        ManifestCreationConfig {
            name: "test-model".to_string(),
            description: Some("A test model".to_string()),
            author_name: Some("Test Author".to_string()),
            author_org: Some("Test Org".to_string()),
            paths: vec![],
            ingredient_names: vec![],
            hash_alg: HashAlgorithm::Sha384,
            key_path: Some(tmp_dir.path().join("test_key.pem")),
            output_encoding: "json".to_string(),
            print: false,
            storage: None,
            with_cc: false,
            linked_manifests: None,
            custom_fields: None,
            software_type: None,
            version: None,
        }
    }

    #[test]
    fn test_generate_c2pa_assertions() {
        let config = make_test_manifest_config();

        let assertions = generate_c2pa_assertions(&config, AssetKind::Model).unwrap();
        assert!(!assertions.is_empty()); // Should have at least the CreativeWork assertion
    }

    #[test]
    fn test_generate_c2pa_claim() {
        let config = make_test_manifest_config();
        let claim = generate_c2pa_claim(&config, AssetKind::Model).unwrap();
        assert!(claim.instance_id.starts_with("urn:c2pa:"));
        assert_eq!(claim.claim_generator_info, "atlas-cli:0.1.1");
    }

    // #[test]
    // fn test_create_manifest() -> Result<()>{
    //     let config = make_test_manifest_config();
    //     let result = create_manifest(config, AssetKind::Model);
    //     assert!(result.is_ok()); // Should succeed even with no ingredients

    //     Ok(())
    // }

    // #[test]
    // fn test_create_oms_manifest() -> Result<()> {
    //     let config = make_test_manifest_config();
    //     let result = create_oms_manifest(config);
    //     assert!(result.is_ok()); // Should succeed with the provided key

    //     Ok(())
    // }

    #[test]
    fn test_create_oms_manifest_no_key() {
        let mut config = make_test_manifest_config();
        config.key_path = None; // Remove the key path to simulate missing key
        let result = create_oms_manifest(config);
        assert!(result.is_err()); // Should fail because OMS requires a signing key
    }
}