tap-msg-derive 0.7.0

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

use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Field, Fields};

/// Procedural derive macro for implementing TapMessage, MessageContext, and optionally TapMessageBody traits.
///
/// # Usage
///
/// ## Basic Usage (TapMessage + MessageContext only)
///
/// ```ignore
/// use tap_msg::TapMessage;
/// use tap_msg::message::{Party, Agent};
/// use tap_caip::AssetId;
///
/// #[derive(TapMessage)]
/// pub struct Transfer {
///     #[tap(participant)]
///     pub originator: Party,
///     
///     #[tap(participant)]
///     pub beneficiary: Option<Party>,
///     
///     #[tap(participant_list)]
///     pub agents: Vec<Agent>,
///     
///     #[tap(transaction_id)]
///     pub transaction_id: String,
///     
///     // regular fields don't need attributes
///     pub amount: String,
///     pub asset: AssetId,
/// }
/// ```
///
/// ## Full Usage (includes TapMessageBody with auto-generated to_didcomm)
///
/// ```ignore
/// use tap_msg::TapMessage;
/// use tap_msg::message::{Party, Agent};
/// use serde::{Serialize, Deserialize};
///
/// #[derive(Debug, Clone, Serialize, Deserialize, TapMessage)]
/// #[tap(message_type = "https://tap.rsvp/schema/1.0#Transfer", initiator, authorizable, transactable)]
/// pub struct Transfer {
///     #[tap(participant)]
///     pub originator: Party,
///     
///     #[tap(participant)]
///     pub beneficiary: Option<Party>,
///     
///     #[tap(participant_list)]
///     pub agents: Vec<Agent>,
///     
///     #[tap(transaction_id)]
///     pub transaction_id: String,
///     
///     pub amount: String,
/// }
///
/// // TapMessageBody is automatically implemented with:
/// // - message_type() returning the specified string
/// // - validate() with basic validation (can be overridden)
/// // - to_didcomm() with automatic participant extraction and message construction
/// // - Authorizable trait (if authorizable attribute is present)
/// // - Transaction trait (if transactable attribute is present)
/// ```
///
/// # Supported Attributes
///
/// ## Struct-level Attributes
/// - `#[tap(message_type = "url")]` - TAP message type URL (enables TapMessageBody generation)
/// - `#[tap(initiator)]` - Marks this as a conversation-initiating message
/// - `#[tap(authorizable)]` - Auto-generates Authorizable trait implementation
/// - `#[tap(transactable)]` - Auto-generates Transaction trait implementation
/// - `#[tap(builder)]` - Auto-generates builder pattern
///
/// ## Field-level Attributes
/// - `#[tap(participant)]` - Single participant field (Party or Agent, required or optional)
/// - `#[tap(participant_list)]` - Vec<Agent> field for lists of agents
/// - `#[tap(transaction_id)]` - Transaction ID field (creates new transaction for initiators)
/// - `#[tap(thread_id)]` - Thread ID field (references existing transaction for replies)
/// - `#[tap(connection_id)]` - Connection ID field (for linking to Connect messages)
#[proc_macro_derive(TapMessage, attributes(tap))]
pub fn derive_tap_message(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let expanded = impl_tap_message(&input);
    TokenStream::from(expanded)
}

#[proc_macro_derive(TapMessageBody, attributes(tap))]
pub fn derive_tap_message_body(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    let expanded = impl_tap_message_body_only(&input);
    TokenStream::from(expanded)
}

fn impl_tap_message(input: &DeriveInput) -> TokenStream2 {
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    let fields = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => &fields.named,
            _ => panic!("TapMessage can only be derived for structs with named fields"),
        },
        _ => panic!("TapMessage can only be derived for structs"),
    };

    let field_info = analyze_fields(fields, &input.attrs);

    // Check if we're inside the tap-msg crate or external
    let is_internal = std::env::var("CARGO_CRATE_NAME").unwrap_or_default() == "tap_msg";

    let tap_message_body_impl = if field_info.message_type.is_some() {
        impl_tap_message_body_trait(
            name,
            &field_info,
            &impl_generics,
            &ty_generics,
            where_clause,
            is_internal,
        )
    } else {
        quote! {}
    };

    let tap_message_impl = impl_tap_message_trait(
        name,
        &field_info,
        &impl_generics,
        &ty_generics,
        where_clause,
        is_internal,
    );

    let message_context_impl = impl_message_context_trait(
        name,
        &field_info,
        &impl_generics,
        &ty_generics,
        where_clause,
        is_internal,
    );

    let authorizable_impl = if field_info.is_authorizable {
        impl_authorizable_trait(
            name,
            &field_info,
            &impl_generics,
            &ty_generics,
            where_clause,
            is_internal,
        )
    } else {
        quote! {}
    };

    let transaction_impl = if field_info.is_transactable {
        impl_transaction_trait(
            name,
            &field_info,
            &impl_generics,
            &ty_generics,
            where_clause,
            is_internal,
        )
    } else {
        quote! {}
    };

    let connectable_impl = if field_info.connection_id_field.is_some() || field_info.is_initiator {
        impl_connectable_trait(
            name,
            &field_info,
            &impl_generics,
            &ty_generics,
            where_clause,
            is_internal,
        )
    } else {
        quote! {}
    };

    quote! {
        #tap_message_impl
        #message_context_impl
        #tap_message_body_impl
        #authorizable_impl
        #transaction_impl
        #connectable_impl
    }
}

fn impl_connectable_trait(
    name: &syn::Ident,
    field_info: &FieldInfo,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: Option<&syn::WhereClause>,
    is_internal: bool,
) -> TokenStream2 {
    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    if let Some(ref conn_field) = field_info.connection_id_field {
        // Has explicit connection_id field
        quote! {
            impl #impl_generics #crate_path::message::tap_message_trait::Connectable for #name #ty_generics #where_clause {
                fn with_connection(&mut self, connection_id: &str) -> &mut Self {
                    self.#conn_field = Some(connection_id.to_string());
                    self
                }

                fn has_connection(&self) -> bool {
                    self.#conn_field.is_some()
                }

                fn connection_id(&self) -> Option<&str> {
                    self.#conn_field.as_deref()
                }
            }
        }
    } else {
        // Initiator messages don't have connections
        quote! {
            impl #impl_generics #crate_path::message::tap_message_trait::Connectable for #name #ty_generics #where_clause {
                fn with_connection(&mut self, _connection_id: &str) -> &mut Self {
                    // Initiator messages don't have connection IDs
                    self
                }

                fn has_connection(&self) -> bool {
                    false
                }

                fn connection_id(&self) -> Option<&str> {
                    None
                }
            }
        }
    }
}

#[derive(Debug)]
struct FieldInfo {
    participant_fields: Vec<syn::Ident>,
    optional_participant_fields: Vec<syn::Ident>,
    participant_list_fields: Vec<syn::Ident>,
    transaction_id_field: Option<syn::Ident>,
    optional_transaction_id_field: Option<syn::Ident>,
    thread_id_field: Option<syn::Ident>,
    optional_thread_id_field: Option<syn::Ident>,
    connection_id_field: Option<syn::Ident>,
    has_generated_id: bool,
    message_type: Option<String>,
    is_initiator: bool,
    is_authorizable: bool,
    is_transactable: bool,
    generate_builder: bool,
    custom_validation: bool,
}

fn analyze_fields(
    fields: &syn::punctuated::Punctuated<Field, syn::Token![,]>,
    struct_attrs: &[syn::Attribute],
) -> FieldInfo {
    let mut field_info = FieldInfo {
        participant_fields: Vec::new(),
        optional_participant_fields: Vec::new(),
        participant_list_fields: Vec::new(),
        transaction_id_field: None,
        optional_transaction_id_field: None,
        thread_id_field: None,
        optional_thread_id_field: None,
        connection_id_field: None,
        has_generated_id: false,
        message_type: None,
        is_initiator: false,
        is_authorizable: false,
        is_transactable: false,
        generate_builder: false,
        custom_validation: false,
    };

    // First check struct-level attributes
    for attr in struct_attrs {
        if attr.path().is_ident("tap") {
            let _ = attr.parse_nested_meta(|meta| {
                if meta.path.is_ident("generated_id") {
                    field_info.has_generated_id = true;
                } else if meta.path.is_ident("message_type") {
                    if let Ok(lit) = meta.value() {
                        if let Ok(lit_str) = lit.parse::<syn::LitStr>() {
                            field_info.message_type = Some(lit_str.value());
                        }
                    }
                } else if meta.path.is_ident("initiator") {
                    field_info.is_initiator = true;
                } else if meta.path.is_ident("authorizable") {
                    field_info.is_authorizable = true;
                } else if meta.path.is_ident("transactable") {
                    field_info.is_transactable = true;
                } else if meta.path.is_ident("builder") {
                    field_info.generate_builder = true;
                } else if meta.path.is_ident("custom_validation") {
                    field_info.custom_validation = true;
                }
                Ok(())
            });
        }
    }

    for field in fields {
        let field_name = field.ident.as_ref().expect("Field must have a name");

        for attr in &field.attrs {
            if attr.path().is_ident("tap") {
                let _ = attr.parse_nested_meta(|meta| {
                    if meta.path.is_ident("participant") {
                        // Check if the field type is Option<Participant>
                        if is_optional_type(&field.ty) {
                            field_info
                                .optional_participant_fields
                                .push(field_name.clone());
                        } else {
                            field_info.participant_fields.push(field_name.clone());
                        }
                    } else if meta.path.is_ident("participant_list") {
                        field_info.participant_list_fields.push(field_name.clone());
                    } else if meta.path.is_ident("transaction_id") {
                        // Check if the field type is Option<String>
                        if is_optional_type(&field.ty) {
                            field_info.optional_transaction_id_field = Some(field_name.clone());
                        } else {
                            field_info.transaction_id_field = Some(field_name.clone());
                        }
                    } else if meta.path.is_ident("optional_transaction_id") {
                        field_info.optional_transaction_id_field = Some(field_name.clone());
                    } else if meta.path.is_ident("thread_id") {
                        // Check if the field type is Option<String>
                        if is_optional_type(&field.ty) {
                            field_info.optional_thread_id_field = Some(field_name.clone());
                        } else {
                            field_info.thread_id_field = Some(field_name.clone());
                        }
                    } else if meta.path.is_ident("connection_id") {
                        field_info.connection_id_field = Some(field_name.clone());
                    } else if meta.path.is_ident("generated_id") {
                        field_info.has_generated_id = true;
                    }
                    Ok(())
                });
            }
        }
    }

    field_info
}

fn is_optional_type(ty: &syn::Type) -> bool {
    if let syn::Type::Path(type_path) = ty {
        if let Some(segment) = type_path.path.segments.last() {
            return segment.ident == "Option";
        }
    }
    false
}

fn impl_tap_message_trait(
    name: &syn::Ident,
    field_info: &FieldInfo,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: Option<&syn::WhereClause>,
    is_internal: bool,
) -> TokenStream2 {
    let thread_id_impl = generate_thread_id_impl(field_info);
    let message_id_impl = generate_message_id_impl(field_info);
    let get_all_participants_impl = generate_get_all_participants_impl(field_info);

    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    // message_type is no longer part of TapMessage trait

    quote! {
        impl #impl_generics #crate_path::message::tap_message_trait::TapMessage for #name #ty_generics #where_clause {
            fn validate(&self) -> #crate_path::error::Result<()> {
                <Self as #crate_path::message::tap_message_trait::TapMessageBody>::validate(self)
            }

            fn is_tap_message(&self) -> bool {
                <Self as #crate_path::message::tap_message_trait::TapMessageBody>::message_type()
                    .starts_with("https://tap.rsvp/schema/1.0#")
            }

            fn get_tap_type(&self) -> Option<String> {
                Some(
                    <Self as #crate_path::message::tap_message_trait::TapMessageBody>::message_type()
                        .to_string(),
                )
            }

            fn body_as<T: #crate_path::message::tap_message_trait::TapMessageBody>(
                &self,
            ) -> #crate_path::error::Result<T> {
                unimplemented!()
            }

            fn get_all_participants(&self) -> Vec<String> {
                // Import TapParticipant trait for .id() method access
                use #crate_path::message::agent::TapParticipant;
                #get_all_participants_impl
            }

            fn create_reply<T: #crate_path::message::tap_message_trait::TapMessageBody>(
                &self,
                body: &T,
                creator_did: &str,
            ) -> #crate_path::error::Result<#crate_path::didcomm::PlainMessage> {
                // Create the base message with creator as sender
                let mut message = body.to_didcomm(creator_did)?;

                // message is already PlainMessage<Value> from to_didcomm

                // Set the thread ID to maintain the conversation thread
                if let Some(thread_id) = self.thread_id() {
                    message.thid = Some(thread_id.to_string());
                } else {
                    // If no thread ID exists, use the original message ID as the thread ID
                    message.thid = Some(self.message_id().to_string());
                }

                // Set the parent thread ID if this thread is part of a larger transaction
                if let Some(parent_thread_id) = self.parent_thread_id() {
                    message.pthid = Some(parent_thread_id.to_string());
                }

                Ok(message)
            }


            fn thread_id(&self) -> Option<&str> {
                #thread_id_impl
            }

            fn parent_thread_id(&self) -> Option<&str> {
                None
            }

            fn message_id(&self) -> &str {
                #message_id_impl
            }
        }
    }
}

fn impl_message_context_trait(
    name: &syn::Ident,
    field_info: &FieldInfo,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: Option<&syn::WhereClause>,
    is_internal: bool,
) -> TokenStream2 {
    let participant_dids_impl = generate_participant_dids_impl(field_info);
    let transaction_context_impl = generate_transaction_context_impl(field_info, is_internal);

    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    quote! {
        impl #impl_generics #crate_path::message::MessageContext for #name #ty_generics #where_clause {
            fn participant_dids(&self) -> Vec<String> {
                // Import TapParticipant trait for .id() method access
                use #crate_path::message::agent::TapParticipant;
                #participant_dids_impl
            }

            fn transaction_context(&self) -> Option<#crate_path::message::TransactionContext> {
                #transaction_context_impl
            }
        }
    }
}

fn generate_participant_dids_impl(field_info: &FieldInfo) -> TokenStream2 {
    let mut did_extracts = Vec::new();

    // Add required participants (Agent, Party, or Participant types)
    for field in &field_info.participant_fields {
        did_extracts.push(quote! {
            // Use the TapParticipant trait to get ID - works with Agent, Party, or Participant
            dids.push(self.#field.id().to_string());
        });
    }

    // Add optional participants
    for field in &field_info.optional_participant_fields {
        did_extracts.push(quote! {
            if let Some(ref participant) = self.#field {
                dids.push(participant.id().to_string());
            }
        });
    }

    // Add participant lists (typically Vec<Agent> or Vec<Participant>)
    for field in &field_info.participant_list_fields {
        did_extracts.push(quote! {
            for participant in &self.#field {
                dids.push(participant.id().to_string());
            }
        });
    }

    quote! {
        let mut dids = Vec::new();
        #(#did_extracts)*
        dids
    }
}

fn generate_get_all_participants_impl(field_info: &FieldInfo) -> TokenStream2 {
    let mut participant_extracts = Vec::new();

    // Add required participants
    for field in &field_info.participant_fields {
        participant_extracts.push(quote! {
            participants.push(self.#field.id().to_string());
        });
    }

    // Add optional participants
    for field in &field_info.optional_participant_fields {
        participant_extracts.push(quote! {
            if let Some(ref participant) = self.#field {
                participants.push(participant.id().to_string());
            }
        });
    }

    // Add participant lists
    for field in &field_info.participant_list_fields {
        participant_extracts.push(quote! {
            for participant in &self.#field {
                participants.push(participant.id().to_string());
            }
        });
    }

    quote! {
        let mut participants = Vec::new();
        #(#participant_extracts)*
        participants
    }
}

fn generate_thread_id_impl(field_info: &FieldInfo) -> TokenStream2 {
    if let Some(ref thread_field) = field_info.thread_id_field {
        quote! { Some(&self.#thread_field) }
    } else if let Some(ref opt_thread_field) = field_info.optional_thread_id_field {
        quote! { self.#opt_thread_field.as_deref() }
    } else if field_info.is_initiator {
        // Initiators don't have a thread_id - they start the thread
        quote! { None }
    } else if let Some(ref tx_field) = field_info.transaction_id_field {
        quote! { Some(&self.#tx_field) }
    } else if let Some(ref opt_tx_field) = field_info.optional_transaction_id_field {
        quote! { self.#opt_tx_field.as_deref() }
    } else {
        quote! { None }
    }
}

fn generate_message_id_impl(field_info: &FieldInfo) -> TokenStream2 {
    if let Some(tx_field) = &field_info.transaction_id_field {
        quote! { &self.#tx_field }
    } else if let Some(thread_field) = &field_info.thread_id_field {
        quote! { &self.#thread_field }
    } else if let Some(opt_tx_field) = &field_info.optional_transaction_id_field {
        quote! {
            self.#opt_tx_field.as_deref().unwrap_or("")
        }
    } else {
        quote! {
            // For types without an ID field, we'll use a static string
            // This isn't ideal but it satisfies the API contract
            static FALLBACK_ID: &str = "00000000-0000-0000-0000-000000000000";
            FALLBACK_ID
        }
    }
}

fn generate_transaction_context_impl(field_info: &FieldInfo, is_internal: bool) -> TokenStream2 {
    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    if let Some(tx_field) = &field_info.transaction_id_field {
        quote! {
            Some(#crate_path::message::TransactionContext::new(
                self.#tx_field.clone(),
                <Self as #crate_path::message::tap_message_trait::TapMessageBody>::message_type().to_string(),
            ))
        }
    } else if let Some(opt_tx_field) = &field_info.optional_transaction_id_field {
        quote! {
            self.#opt_tx_field.as_ref().map(|tx_id| {
                #crate_path::message::TransactionContext::new(
                    tx_id.clone(),
                    <Self as #crate_path::message::tap_message_trait::TapMessageBody>::message_type().to_string(),
                )
            })
        }
    } else {
        quote! { None }
    }
}

fn impl_tap_message_body_trait(
    name: &syn::Ident,
    field_info: &FieldInfo,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: Option<&syn::WhereClause>,
    is_internal: bool,
) -> TokenStream2 {
    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    let message_type = field_info
        .message_type
        .as_ref()
        .expect("message_type attribute is required for TapMessageBody");

    let validate_impl = if field_info.custom_validation {
        // If custom_validation is specified, delegate to a validate_<struct_name_lowercase> method
        let method_name = syn::Ident::new(
            &format!("validate_{}", name.to_string().to_lowercase()),
            name.span(),
        );
        quote! {
            self.#method_name()
        }
    } else {
        quote! {
            // Basic validation - users can override this by implementing custom validation
            Ok(())
        }
    };

    let to_didcomm_impl = generate_to_didcomm_impl(field_info, is_internal);

    quote! {
        impl #impl_generics #crate_path::message::tap_message_trait::TapMessageBody for #name #ty_generics #where_clause {
            fn message_type() -> &'static str {
                #message_type
            }

            fn validate(&self) -> #crate_path::error::Result<()> {
                #validate_impl
            }

            fn to_didcomm(&self, from_did: &str) -> #crate_path::error::Result<#crate_path::didcomm::PlainMessage> {
                #to_didcomm_impl
            }
        }
    }
}

fn generate_to_didcomm_impl(field_info: &FieldInfo, is_internal: bool) -> TokenStream2 {
    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    // Generate participant extraction
    let participant_extraction = if !field_info.participant_fields.is_empty()
        || !field_info.optional_participant_fields.is_empty()
        || !field_info.participant_list_fields.is_empty()
    {
        let mut extracts = Vec::new();

        // Required participants
        for field in &field_info.participant_fields {
            extracts.push(quote! {
                recipient_dids.push(self.#field.id().to_string());
            });
        }

        // Optional participants
        for field in &field_info.optional_participant_fields {
            extracts.push(quote! {
                if let Some(ref participant) = self.#field {
                    recipient_dids.push(participant.id().to_string());
                }
            });
        }

        // Participant lists
        for field in &field_info.participant_list_fields {
            extracts.push(quote! {
                for participant in &self.#field {
                    recipient_dids.push(participant.id().to_string());
                }
            });
        }

        quote! {
            let mut recipient_dids = Vec::new();
            #(#extracts)*

            // Remove duplicates and sender
            recipient_dids.sort();
            recipient_dids.dedup();
            recipient_dids.retain(|did| did != from_did);
        }
    } else {
        quote! {
            let recipient_dids: Vec<String> = Vec::new();
        }
    };

    // Generate thread ID assignment
    let thread_assignment = if field_info.is_initiator {
        // Initiators create a new thread - their transaction_id becomes the thid for replies
        if let Some(tx_field) = &field_info.transaction_id_field {
            quote! {
                thid: Some(self.#tx_field.clone()),
            }
        } else {
            quote! {
                thid: None,
            }
        }
    } else if let Some(thread_field) = &field_info.thread_id_field {
        // Reply messages use thread_id to reference the existing transaction
        quote! {
            thid: Some(self.#thread_field.clone()),
        }
    } else if let Some(opt_thread_field) = &field_info.optional_thread_id_field {
        // Optional thread_id field
        quote! {
            thid: self.#opt_thread_field.clone(),
        }
    } else if let Some(tx_field) = &field_info.transaction_id_field {
        // Fallback for backwards compatibility
        quote! {
            thid: Some(self.#tx_field.clone()),
        }
    } else if let Some(opt_tx_field) = &field_info.optional_transaction_id_field {
        quote! {
            thid: self.#opt_tx_field.clone(),
        }
    } else {
        quote! {
            thid: None,
        }
    };

    // Generate pthid assignment for connection linking
    let pthid_assignment = if let Some(conn_field) = &field_info.connection_id_field {
        quote! {
            pthid: self.#conn_field.clone(),
        }
    } else {
        quote! {
            pthid: None,
        }
    };

    quote! {
        // Import TapParticipant trait for .id() method access
        use #crate_path::message::agent::TapParticipant;

        // Serialize the message body to JSON
        let mut body_json = serde_json::to_value(self)
            .map_err(|e| #crate_path::error::Error::SerializationError(e.to_string()))?;

        // Ensure the @type field is correctly set in the body
        if let Some(body_obj) = body_json.as_object_mut() {
            body_obj.insert(
                "@type".to_string(),
                serde_json::Value::String(Self::message_type().to_string()),
            );
        }

        // Extract recipient DIDs from participants
        #participant_extraction

        let now = chrono::Utc::now().timestamp() as u64;

        // Create the PlainMessage
        Ok(#crate_path::didcomm::PlainMessage {
            id: uuid::Uuid::new_v4().to_string(),
            typ: "application/didcomm-plain+json".to_string(),
            type_: Self::message_type().to_string(),
            body: body_json,
            from: from_did.to_string(),
            to: recipient_dids,
            #thread_assignment
            #pthid_assignment
            created_time: Some(now),
            expires_time: None,
            extra_headers: std::collections::HashMap::new(),
            from_prior: None,
            attachments: None,
        })
    }
}

fn impl_tap_message_body_only(input: &DeriveInput) -> TokenStream2 {
    let name = &input.ident;
    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    let fields = match &input.data {
        Data::Struct(data) => match &data.fields {
            Fields::Named(fields) => &fields.named,
            _ => panic!("TapMessageBody can only be derived for structs with named fields"),
        },
        _ => panic!("TapMessageBody can only be derived for structs"),
    };

    let field_info = analyze_fields(fields, &input.attrs);

    // Check if we're inside the tap-msg crate or external
    let is_internal = std::env::var("CARGO_CRATE_NAME").unwrap_or_default() == "tap_msg";

    // TapMessageBody can only be derived if message_type is specified
    if field_info.message_type.is_none() {
        panic!("TapMessageBody derive macro requires #[tap(message_type = \"...\")] attribute");
    }

    impl_tap_message_body_trait(
        name,
        &field_info,
        &impl_generics,
        &ty_generics,
        where_clause,
        is_internal,
    )
}

fn impl_authorizable_trait(
    name: &syn::Ident,
    field_info: &FieldInfo,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: Option<&syn::WhereClause>,
    is_internal: bool,
) -> TokenStream2 {
    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    // Get the transaction ID field access
    let tx_id_access = if let Some(ref tx_field) = field_info.transaction_id_field {
        quote! { &self.#tx_field }
    } else if let Some(ref opt_tx_field) = field_info.optional_transaction_id_field {
        quote! { self.#opt_tx_field.as_deref().unwrap_or("") }
    } else if let Some(ref thread_field) = field_info.thread_id_field {
        quote! { &self.#thread_field }
    } else {
        quote! { "" }
    };

    quote! {
        impl #impl_generics #crate_path::message::tap_message_trait::Authorizable for #name #ty_generics #where_clause {
            fn authorize(
                &self,
                creator_did: &str,
                settlement_address: Option<&str>,
                expiry: Option<&str>,
            ) -> #crate_path::didcomm::PlainMessage<#crate_path::message::Authorize> {
                let authorize = #crate_path::message::Authorize::with_all(#tx_id_access, settlement_address, expiry);
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&authorize, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, authorize)
            }

            fn cancel(&self, creator_did: &str, by: &str, reason: Option<&str>) -> #crate_path::didcomm::PlainMessage<#crate_path::message::Cancel> {
                let cancel = if let Some(reason) = reason {
                    #crate_path::message::Cancel::with_reason(#tx_id_access, by, reason)
                } else {
                    #crate_path::message::Cancel::new(#tx_id_access, by)
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&cancel, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, cancel)
            }

            fn reject(&self, creator_did: &str, reason: &str) -> #crate_path::didcomm::PlainMessage<#crate_path::message::Reject> {
                let reject = #crate_path::message::Reject {
                    transaction_id: (#tx_id_access).to_string(),
                    reason: Some(reason.to_string()),
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&reject, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, reject)
            }
        }
    }
}

fn impl_transaction_trait(
    name: &syn::Ident,
    field_info: &FieldInfo,
    impl_generics: &syn::ImplGenerics,
    ty_generics: &syn::TypeGenerics,
    where_clause: Option<&syn::WhereClause>,
    is_internal: bool,
) -> TokenStream2 {
    let crate_path = if is_internal {
        quote! { crate }
    } else {
        quote! { ::tap_msg }
    };

    // Get the transaction ID field access
    let tx_id_access = if let Some(ref tx_field) = field_info.transaction_id_field {
        quote! { &self.#tx_field }
    } else if let Some(ref opt_tx_field) = field_info.optional_transaction_id_field {
        quote! { self.#opt_tx_field.as_deref().unwrap_or("") }
    } else if let Some(ref thread_field) = field_info.thread_id_field {
        quote! { &self.#thread_field }
    } else {
        quote! { "" }
    };

    quote! {
        impl #impl_generics #crate_path::message::tap_message_trait::Transaction for #name #ty_generics #where_clause {
            fn settle(
                &self,
                creator_did: &str,
                settlement_id: &str,
                amount: Option<&str>,
            ) -> #crate_path::didcomm::PlainMessage<#crate_path::message::Settle> {
                let settle = #crate_path::message::Settle {
                    transaction_id: (#tx_id_access).to_string(),
                    settlement_id: Some(settlement_id.to_string()),
                    amount: amount.map(|s| s.to_string()),
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&settle, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, settle)
            }

            fn revert(
                &self,
                creator_did: &str,
                settlement_address: &str,
                reason: &str,
            ) -> #crate_path::didcomm::PlainMessage<#crate_path::message::Revert> {
                let revert = #crate_path::message::Revert {
                    transaction_id: (#tx_id_access).to_string(),
                    settlement_address: settlement_address.to_string(),
                    reason: reason.to_string(),
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&revert, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, revert)
            }

            fn add_agents(&self, creator_did: &str, agents: Vec<#crate_path::message::Agent>) -> #crate_path::didcomm::PlainMessage<#crate_path::message::AddAgents> {
                let add_agents = #crate_path::message::AddAgents {
                    transaction_id: (#tx_id_access).to_string(),
                    agents,
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&add_agents, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, add_agents)
            }

            fn replace_agent(
                &self,
                creator_did: &str,
                original_agent: &str,
                replacement: #crate_path::message::Agent,
            ) -> #crate_path::didcomm::PlainMessage<#crate_path::message::ReplaceAgent> {
                let replace_agent = #crate_path::message::ReplaceAgent {
                    transaction_id: (#tx_id_access).to_string(),
                    original: original_agent.to_string(),
                    replacement,
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&replace_agent, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, replace_agent)
            }

            fn remove_agent(&self, creator_did: &str, agent: &str) -> #crate_path::didcomm::PlainMessage<#crate_path::message::RemoveAgent> {
                let remove_agent = #crate_path::message::RemoveAgent {
                    transaction_id: (#tx_id_access).to_string(),
                    agent: agent.to_string(),
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&remove_agent, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, remove_agent)
            }

            fn update_party(
                &self,
                creator_did: &str,
                party_type: &str,
                party: #crate_path::message::Party,
            ) -> #crate_path::didcomm::PlainMessage<#crate_path::message::UpdateParty> {
                let update_party = #crate_path::message::UpdateParty {
                    transaction_id: (#tx_id_access).to_string(),
                    party_type: party_type.to_string(),
                    party,
                    context: None,
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&update_party, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, update_party)
            }

            fn update_policies(
                &self,
                creator_did: &str,
                policies: Vec<#crate_path::message::Policy>,
            ) -> #crate_path::didcomm::PlainMessage<#crate_path::message::UpdatePolicies> {
                let update_policies = #crate_path::message::UpdatePolicies {
                    transaction_id: (#tx_id_access).to_string(),
                    policies,
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&update_policies, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, update_policies)
            }

            fn confirm_relationship(
                &self,
                creator_did: &str,
                agent_did: &str,
                for_entity: &str,
            ) -> #crate_path::didcomm::PlainMessage<#crate_path::message::ConfirmRelationship> {
                let confirm_relationship = #crate_path::message::ConfirmRelationship {
                    transaction_id: (#tx_id_access).to_string(),
                    agent_id: agent_did.to_string(),
                    for_entity: for_entity.to_string(),
                    role: None,
                };
                let original_message = self
                    .to_didcomm(creator_did)
                    .expect("Failed to create DIDComm message");
                let reply = original_message
                    .create_reply(&confirm_relationship, creator_did)
                    .expect("Failed to create reply");
                #crate_path::message::tap_message_trait::typed_plain_message(reply, confirm_relationship)
            }
        }
    }
}