stateset-embedded 0.7.13

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

use rust_decimal_macros::dec;
use stateset_embedded::{
    BackorderPriority,
    BillStatus,
    // Warranty types
    ClaimResolution,
    ClaimStatus,
    Commerce,
    CostLayerSource,
    CostMethod,
    // Backorder types
    CreateBackorder,
    // Accounts Payable types
    CreateBill,
    CreateBillItem,
    // Cost Accounting types
    CreateCostLayer,
    // Credit types
    CreateCreditAccount,
    CreateCustomer,
    // Invoice types
    CreateInvoice,
    CreateInvoiceItem,
    CreateLocation,
    // Lot types
    CreateLot,
    // Product types
    CreateProduct,
    CreateProductVariant,
    // Purchase Order types
    CreatePurchaseOrder,
    CreatePurchaseOrderItem,
    // Receiving types
    CreateReceipt,
    // Serial types
    CreateSerialNumber,
    // Shipment types
    CreateShipment,
    CreateShipmentItem,
    CreateSupplier,
    // Warehouse types
    CreateWarehouse,
    CreateWarranty,
    CreateWarrantyClaim,
    CreateZone,
    InvoiceStatus,
    LocationType,
    PaymentTerms,
    PurchaseOrderStatus,
    ReceiptType,
    RecordInvoicePayment,
    RiskRating,
    SerialStatus,
    SetItemCost,
    ShipmentStatus,
    ShippingCarrier,
    UpdateSerialNumber,
    UpdateWarrantyClaim,
    WarehouseType,
    WarrantyStatus,
    WarrantyType,
};
use uuid::Uuid;

// ============================================================================
// Warehouse Tests
// ============================================================================

#[test]
fn test_warehouse_create_and_list() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let warehouse = commerce
        .warehouse()
        .create_warehouse(CreateWarehouse {
            code: "WH-TEST-001".into(),
            name: "Test Warehouse".into(),
            warehouse_type: WarehouseType::Distribution,
            ..Default::default()
        })
        .expect("Failed to create warehouse");

    assert_eq!(warehouse.code, "WH-TEST-001");
    assert_eq!(warehouse.name, "Test Warehouse");

    let warehouses = commerce
        .warehouse()
        .list_warehouses(Default::default())
        .expect("Failed to list warehouses");
    assert!(warehouses.iter().any(|w| w.id == warehouse.id));
}

#[test]
fn test_location_create_and_get() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let warehouse = commerce
        .warehouse()
        .create_warehouse(CreateWarehouse {
            code: "WH-LOC-001".into(),
            name: "Location Test Warehouse".into(),
            warehouse_type: WarehouseType::Distribution,
            ..Default::default()
        })
        .expect("Failed to create warehouse");

    let location = commerce
        .warehouse()
        .create_location(CreateLocation {
            warehouse_id: warehouse.id,
            location_type: LocationType::Pick,
            zone: Some("A".into()),
            aisle: Some("01".into()),
            rack: Some("02".into()),
            bin: Some("03".into()),
            ..Default::default()
        })
        .expect("Failed to create location");

    assert_eq!(location.warehouse_id, warehouse.id);
    assert!(location.code.contains("A"));

    let fetched = commerce
        .warehouse()
        .get_location(location.id)
        .expect("Failed to get location")
        .expect("Location not found");
    assert_eq!(fetched.id, location.id);
}

#[test]
fn test_zone_create() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let warehouse = commerce
        .warehouse()
        .create_warehouse(CreateWarehouse {
            code: "WH-ZONE-001".into(),
            name: "Zone Test Warehouse".into(),
            warehouse_type: WarehouseType::Distribution,
            ..Default::default()
        })
        .expect("Failed to create warehouse");

    let zone = commerce
        .warehouse()
        .create_zone(CreateZone {
            warehouse_id: warehouse.id,
            code: "ZONE-A".into(),
            name: "Zone A".into(),
            ..Default::default()
        })
        .expect("Failed to create zone");

    assert_eq!(zone.code, "ZONE-A");
    assert_eq!(zone.warehouse_id, warehouse.id);
}

// ============================================================================
// Lot Tests
// ============================================================================

#[test]
fn test_lot_create_and_get() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let lot = commerce
        .lots()
        .create(CreateLot {
            lot_number: Some("LOT-TEST-001".into()),
            sku: "LOT-SKU-001".into(),
            quantity: dec!(1000),
            ..Default::default()
        })
        .expect("Failed to create lot");

    assert_eq!(lot.lot_number, "LOT-TEST-001");
    assert_eq!(lot.quantity_remaining, dec!(1000));

    // Get the lot
    let fetched = commerce.lots().get(lot.id).expect("Failed to get lot").expect("Lot not found");
    assert_eq!(fetched.id, lot.id);
}

// ============================================================================
// Serial Number Tests
// ============================================================================

#[test]
fn test_serial_create() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let serial = commerce
        .serials()
        .create(CreateSerialNumber {
            serial: Some("SN-TEST-001".into()),
            sku: "SERIAL-SKU-001".into(),
            ..Default::default()
        })
        .expect("Failed to create serial");

    assert_eq!(serial.serial, "SN-TEST-001");
    assert_eq!(serial.status, SerialStatus::Available);
}

#[test]
fn test_serial_lookup_with_related_data() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    commerce
        .products()
        .create(CreateProduct {
            name: "Lookup Widget".into(),
            variants: Some(vec![CreateProductVariant {
                sku: "SERIAL-LOOKUP-SKU".into(),
                name: Some("Lookup Variant".into()),
                price: dec!(99.99),
                ..Default::default()
            }]),
            ..Default::default()
        })
        .expect("Failed to create product");

    let lot = commerce
        .lots()
        .create(CreateLot {
            lot_number: Some("LOT-LOOKUP-001".into()),
            sku: "SERIAL-LOOKUP-SKU".into(),
            quantity: dec!(10),
            ..Default::default()
        })
        .expect("Failed to create lot");

    let serial = commerce
        .serials()
        .create(CreateSerialNumber {
            serial: Some("SN-LOOKUP-001".into()),
            sku: "SERIAL-LOOKUP-SKU".into(),
            lot_id: Some(lot.id),
            ..Default::default()
        })
        .expect("Failed to create serial");

    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "serial-lookup@example.com".into(),
            first_name: "Serial".into(),
            last_name: "Lookup".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let warranty = commerce
        .warranties()
        .create(CreateWarranty {
            customer_id: customer.id,
            sku: Some("SERIAL-LOOKUP-SKU".into()),
            serial_number: Some(serial.serial.clone()),
            warranty_type: Some(WarrantyType::Standard),
            duration_months: Some(12),
            ..Default::default()
        })
        .expect("Failed to create warranty");

    commerce
        .serials()
        .update(
            serial.id,
            UpdateSerialNumber { warranty_id: Some(warranty.id.into()), ..Default::default() },
        )
        .expect("Failed to update serial");

    let lookup = commerce
        .serials()
        .lookup("SN-LOOKUP-001")
        .expect("Failed to lookup serial")
        .expect("Serial not found");

    assert_eq!(lookup.product_name.as_deref(), Some("Lookup Widget"));
    let lookup_lot = lookup.lot.expect("Lot not loaded");
    assert_eq!(lookup_lot.id, lot.id);
    let warranty_status = lookup.warranty_status.expect("Warranty status not loaded");
    assert_eq!(warranty_status.warranty_id, warranty.id.into_uuid());
    assert!(warranty_status.is_active);
    assert_eq!(warranty_status.coverage_type.as_deref(), Some("standard"));
}

// ============================================================================
// Receiving Tests
// ============================================================================

#[test]
fn test_receipt_create() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    // Create a warehouse first (receipts require a warehouse_id)
    let warehouse = commerce
        .warehouse()
        .create_warehouse(CreateWarehouse {
            code: "WH-RECV-001".into(),
            name: "Receiving Warehouse".into(),
            warehouse_type: WarehouseType::Distribution,
            ..Default::default()
        })
        .expect("Failed to create warehouse");

    let receipt = commerce
        .receiving()
        .create_receipt(CreateReceipt {
            receipt_type: ReceiptType::PurchaseOrder,
            warehouse_id: warehouse.id,
            ..Default::default()
        })
        .expect("Failed to create receipt");

    assert_eq!(receipt.receipt_type, ReceiptType::PurchaseOrder);

    // Get the receipt
    let fetched = commerce
        .receiving()
        .get_receipt(receipt.id)
        .expect("Failed to get receipt")
        .expect("Receipt not found");
    assert_eq!(fetched.id, receipt.id);
}

// ============================================================================
// Accounts Payable Tests
// ============================================================================

#[test]
fn test_bill_create_and_list() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let supplier_id = Uuid::new_v4();

    let bill = commerce
        .accounts_payable()
        .create_bill(CreateBill {
            supplier_id,
            items: vec![CreateBillItem {
                description: "Widget supplies".into(),
                quantity: dec!(100),
                unit_price: dec!(10.00),
                ..Default::default()
            }],
            ..Default::default()
        })
        .expect("Failed to create bill");

    assert_eq!(bill.status, BillStatus::Draft);
    assert_eq!(bill.total_amount, dec!(1000.00));

    let bills =
        commerce.accounts_payable().list_bills(Default::default()).expect("Failed to list bills");
    assert!(bills.iter().any(|b| b.id == bill.id));
}

// ============================================================================
// Cost Accounting Tests
// ============================================================================

#[test]
fn test_item_cost_set_and_get() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    // Set item cost
    let item_cost = commerce
        .cost_accounting()
        .set_item_cost(SetItemCost {
            sku: "COST-SKU-001".into(),
            cost_method: Some(CostMethod::Standard),
            standard_cost: Some(dec!(25.00)),
            ..Default::default()
        })
        .expect("Failed to set item cost");

    assert_eq!(item_cost.sku, "COST-SKU-001");
    assert_eq!(item_cost.cost_method, CostMethod::Standard);
    assert_eq!(item_cost.standard_cost, dec!(25.00));

    // Get item cost
    let fetched = commerce
        .cost_accounting()
        .get_item_cost("COST-SKU-001")
        .expect("Failed to get item cost")
        .expect("Item cost not found");
    assert_eq!(fetched.sku, "COST-SKU-001");
}

#[test]
fn test_cost_layer_create() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let layer = commerce
        .cost_accounting()
        .create_cost_layer(CreateCostLayer {
            sku: "LAYER-SKU-001".into(),
            quantity: dec!(100),
            unit_cost: dec!(15.50),
            source_type: CostLayerSource::Purchase,
            source_id: None,
            lot_id: None,
            location_id: None,
        })
        .expect("Failed to create cost layer");

    assert_eq!(layer.sku, "LAYER-SKU-001");
    assert_eq!(layer.quantity, dec!(100));
    assert_eq!(layer.unit_cost, dec!(15.50));
}

// ============================================================================
// Credit Tests
// ============================================================================

#[test]
fn test_credit_account_create_and_get() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    // Create a customer first
    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "credit@example.com".into(),
            first_name: "Credit".into(),
            last_name: "Test".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let account = commerce
        .credit()
        .create_credit_account(CreateCreditAccount {
            customer_id: customer.id,
            credit_limit: dec!(5000.00),
            risk_rating: Some(RiskRating::Low),
            ..Default::default()
        })
        .expect("Failed to create credit account");

    assert_eq!(account.customer_id, customer.id);
    assert_eq!(account.credit_limit, dec!(5000.00));

    // Get account
    let fetched = commerce
        .credit()
        .get_credit_account(account.id)
        .expect("Failed to get account")
        .expect("Account not found");
    assert_eq!(fetched.id, account.id);
}

// ============================================================================
// Backorder Tests
// ============================================================================

#[test]
fn test_backorder_create_and_list() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    // Create a customer
    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "backorder@example.com".into(),
            first_name: "Backorder".into(),
            last_name: "Test".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    // Create an order for the backorder
    let order = commerce
        .orders()
        .create(stateset_embedded::CreateOrder {
            customer_id: customer.id,
            items: vec![stateset_embedded::CreateOrderItem {
                product_id: Uuid::new_v4().into(),
                sku: "BO-SKU-001".into(),
                quantity: 10,
                unit_price: dec!(25.00),
                name: "Backorder Item".into(),
                ..Default::default()
            }],
            ..Default::default()
        })
        .expect("Failed to create order");

    let backorder = commerce
        .backorder()
        .create_backorder(CreateBackorder {
            order_id: order.id.into(),
            customer_id: customer.id.into(),
            sku: "BO-SKU-001".into(),
            quantity: dec!(50),
            priority: Some(BackorderPriority::High),
            order_line_id: None,
            expected_date: None,
            promised_date: None,
            source_location_id: None,
            notes: None,
        })
        .expect("Failed to create backorder");

    assert_eq!(backorder.sku, "BO-SKU-001");
    assert_eq!(backorder.priority, BackorderPriority::High);

    let backorders = commerce
        .backorder()
        .list_backorders(Default::default())
        .expect("Failed to list backorders");
    assert!(backorders.iter().any(|b| b.id == backorder.id));
}

// ============================================================================
// Quality Tests
// ============================================================================

#[test]
fn test_quality_inspection_create() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    // Use a dummy reference ID for the inspection
    let reference_id = Uuid::new_v4();

    let inspection = commerce
        .quality()
        .create_inspection(stateset_embedded::CreateInspection {
            inspection_type: stateset_embedded::InspectionType::Incoming,
            reference_type: "receipt".into(),
            reference_id,
            inspector_id: Some("QC-001".into()),
            scheduled_at: None,
            notes: Some("Incoming quality check".into()),
            items: vec![stateset_embedded::CreateInspectionItem {
                sku: "QC-SKU-001".into(),
                lot_number: None,
                serial_number: None,
                quantity_to_inspect: dec!(100),
            }],
        })
        .expect("Failed to create inspection");

    assert_eq!(inspection.reference_type, "receipt");
    assert_eq!(inspection.reference_id, reference_id);

    // List inspections
    let inspections = commerce
        .quality()
        .list_inspections(Default::default())
        .expect("Failed to list inspections");
    assert!(inspections.iter().any(|i| i.id == inspection.id));
}

// ============================================================================
// General Ledger Tests
// ============================================================================

#[test]
fn test_gl_account_create_and_list() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let account = commerce
        .general_ledger()
        .create_account(stateset_embedded::CreateGlAccount {
            account_number: "1000".into(),
            name: "Cash".into(),
            account_type: stateset_embedded::AccountType::Asset,
            description: Some("Main cash account".into()),
            account_sub_type: None,
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        })
        .expect("Failed to create GL account");

    assert_eq!(account.account_number, "1000");
    assert_eq!(account.name, "Cash");

    let accounts = commerce
        .general_ledger()
        .list_accounts(Default::default())
        .expect("Failed to list accounts");
    assert!(accounts.iter().any(|a| a.id == account.id));
}

// ============================================================================
// Accounts Receivable Tests
// ============================================================================

#[test]
fn test_ar_aging_summary() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    // AR aging should work even with no invoices
    let summary =
        commerce.accounts_receivable().get_aging_summary().expect("Failed to get aging summary");

    assert_eq!(summary.total, dec!(0));
    assert_eq!(summary.current, dec!(0));
}

// ============================================================================
// Fulfillment Tests
// ============================================================================

#[test]
fn test_fulfillment_wave_create() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    // Create a warehouse first
    let warehouse = commerce
        .warehouse()
        .create_warehouse(CreateWarehouse {
            code: "WH-FULFILL-001".into(),
            name: "Fulfillment Warehouse".into(),
            warehouse_type: WarehouseType::Distribution,
            ..Default::default()
        })
        .expect("Failed to create warehouse");

    let wave = commerce
        .fulfillment()
        .create_wave(stateset_embedded::CreateWave {
            warehouse_id: warehouse.id,
            order_ids: vec![],
            priority: Some(1),
            notes: Some("Test Wave".into()),
            created_by: None,
        })
        .expect("Failed to create wave");

    assert_eq!(wave.warehouse_id, warehouse.id);

    // List waves
    let waves =
        commerce.fulfillment().list_waves(Default::default()).expect("Failed to list waves");
    assert!(waves.iter().any(|w| w.id == wave.id));
}

// ============================================================================
// Shipments Tests
// ============================================================================

#[test]
fn test_shipment_tracking_flow() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "ship@example.com".into(),
            first_name: "Ship".into(),
            last_name: "Test".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let order = commerce
        .orders()
        .create(stateset_embedded::CreateOrder {
            customer_id: customer.id,
            items: vec![stateset_embedded::CreateOrderItem {
                product_id: Uuid::new_v4().into(),
                sku: "SHIP-SKU-001".into(),
                name: "Shipment Item".into(),
                quantity: 1,
                unit_price: dec!(19.99),
                ..Default::default()
            }],
            ..Default::default()
        })
        .expect("Failed to create order");

    let tracking_number = "1Z999AA10123456784".to_string();

    let shipment = commerce
        .shipments()
        .create(CreateShipment {
            order_id: order.id,
            carrier: Some(ShippingCarrier::Ups),
            recipient_name: "Alice Smith".into(),
            shipping_address: "123 Main St, City, ST 12345".into(),
            items: Some(vec![CreateShipmentItem {
                sku: "SHIP-SKU-001".into(),
                name: "Shipment Item".into(),
                quantity: 1,
                ..Default::default()
            }]),
            ..Default::default()
        })
        .expect("Failed to create shipment");

    assert_eq!(shipment.status, ShipmentStatus::Pending);

    let shipped = commerce
        .shipments()
        .ship(shipment.id, Some(tracking_number.clone()))
        .expect("Failed to ship");
    assert_eq!(shipped.status, ShipmentStatus::Shipped);
    assert!(shipped.tracking_url.as_ref().is_some_and(|url| url.contains("ups.com")));

    let delivered =
        commerce.shipments().mark_delivered(shipment.id).expect("Failed to mark delivered");
    assert_eq!(delivered.status, ShipmentStatus::Delivered);

    let by_tracking = commerce
        .shipments()
        .get_by_tracking(&tracking_number)
        .expect("Failed to fetch by tracking")
        .expect("Shipment not found by tracking");
    assert_eq!(by_tracking.id, shipment.id);
}

// ============================================================================
// Warranty Tests
// ============================================================================

#[test]
fn test_warranty_claim_lifecycle() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "warranty@example.com".into(),
            first_name: "Warranty".into(),
            last_name: "Test".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let warranty = commerce
        .warranties()
        .create(CreateWarranty {
            customer_id: customer.id,
            sku: Some("WRN-SKU-001".into()),
            warranty_type: Some(WarrantyType::Standard),
            duration_months: Some(12),
            ..Default::default()
        })
        .expect("Failed to create warranty");

    assert_eq!(warranty.status, WarrantyStatus::Active);

    let fetched = commerce
        .warranties()
        .get_by_number(&warranty.warranty_number)
        .expect("Failed to get warranty by number")
        .expect("Warranty not found");
    assert_eq!(fetched.id, warranty.id);

    let claim = commerce
        .warranties()
        .create_claim(CreateWarrantyClaim {
            warranty_id: warranty.id,
            issue_description: "Stopped working after 3 months".into(),
            contact_email: Some("warranty@example.com".into()),
            ..Default::default()
        })
        .expect("Failed to create warranty claim");

    assert_eq!(claim.status, ClaimStatus::Submitted);

    let approved = commerce.warranties().approve_claim(claim.id).expect("Failed to approve claim");
    assert_eq!(approved.status, ClaimStatus::Approved);

    let completed = commerce
        .warranties()
        .complete_claim(claim.id, ClaimResolution::Replacement)
        .expect("Failed to complete claim");
    assert_eq!(completed.status, ClaimStatus::Completed);
    assert_eq!(completed.resolution, ClaimResolution::Replacement);
}

#[test]
fn test_warranty_claim_invalid_transitions() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "warranty-invalid@example.com".into(),
            first_name: "Warranty".into(),
            last_name: "Invalid".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let warranty = commerce
        .warranties()
        .create(CreateWarranty {
            customer_id: customer.id,
            sku: Some("WRN-SKU-002".into()),
            warranty_type: Some(WarrantyType::Standard),
            duration_months: Some(12),
            ..Default::default()
        })
        .expect("Failed to create warranty");

    let claim = commerce
        .warranties()
        .create_claim(CreateWarrantyClaim {
            warranty_id: warranty.id,
            issue_description: "Battery failure".into(),
            ..Default::default()
        })
        .expect("Failed to create warranty claim");

    let result = commerce.warranties().complete_claim(claim.id, ClaimResolution::Replacement);
    assert!(result.is_err());

    let result = commerce.warranties().deny_claim(claim.id, "");
    assert!(result.is_err());

    let approved = commerce.warranties().approve_claim(claim.id).expect("Failed to approve claim");
    assert_eq!(approved.status, ClaimStatus::Approved);

    let result = commerce.warranties().approve_claim(claim.id);
    assert!(result.is_err());

    let result = commerce.warranties().deny_claim(claim.id, "Late filing");
    assert!(result.is_err());

    let result = commerce.warranties().complete_claim(claim.id, ClaimResolution::None);
    assert!(result.is_err());

    let completed = commerce
        .warranties()
        .complete_claim(claim.id, ClaimResolution::Repair)
        .expect("Failed to complete claim");
    assert_eq!(completed.status, ClaimStatus::Completed);

    let result = commerce.warranties().cancel_claim(claim.id);
    assert!(result.is_err());
}

#[test]
fn test_warranty_status_transition_guards() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "warranty-transition@example.com".into(),
            first_name: "Warranty".into(),
            last_name: "Transition".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let new_customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "warranty-new-owner@example.com".into(),
            first_name: "New".into(),
            last_name: "Owner".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let warranty = commerce
        .warranties()
        .create(CreateWarranty {
            customer_id: customer.id,
            sku: Some("WRN-SKU-003".into()),
            warranty_type: Some(WarrantyType::Standard),
            duration_months: Some(12),
            ..Default::default()
        })
        .expect("Failed to create warranty");

    let expired =
        commerce.warranties().expire(warranty.id.into()).expect("Failed to expire warranty");
    assert_eq!(expired.status, WarrantyStatus::Expired);

    let result = commerce.warranties().void(warranty.id.into());
    assert!(result.is_err());

    let result = commerce.warranties().transfer(warranty.id.into(), new_customer.id.into());
    assert!(result.is_err());
}

#[test]
fn test_warranty_update_claim_transitions() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "warranty-update@example.com".into(),
            first_name: "Warranty".into(),
            last_name: "Update".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let warranty = commerce
        .warranties()
        .create(CreateWarranty {
            customer_id: customer.id,
            sku: Some("WRN-SKU-004".into()),
            warranty_type: Some(WarrantyType::Standard),
            duration_months: Some(12),
            ..Default::default()
        })
        .expect("Failed to create warranty");

    let claim = commerce
        .warranties()
        .create_claim(CreateWarrantyClaim {
            warranty_id: warranty.id,
            issue_description: "Screen flicker".into(),
            ..Default::default()
        })
        .expect("Failed to create warranty claim");

    let under_review = commerce
        .warranties()
        .update_claim(
            claim.id,
            UpdateWarrantyClaim { status: Some(ClaimStatus::UnderReview), ..Default::default() },
        )
        .expect("Failed to update claim to under_review");
    assert_eq!(under_review.status, ClaimStatus::UnderReview);

    let result = commerce.warranties().update_claim(
        claim.id,
        UpdateWarrantyClaim {
            status: Some(ClaimStatus::Completed),
            resolution: Some(ClaimResolution::Refund),
            ..Default::default()
        },
    );
    assert!(result.is_err());

    let approved = commerce
        .warranties()
        .update_claim(
            claim.id,
            UpdateWarrantyClaim { status: Some(ClaimStatus::Approved), ..Default::default() },
        )
        .expect("Failed to update claim to approved");
    assert_eq!(approved.status, ClaimStatus::Approved);
    assert!(approved.approved_at.is_some());

    let in_progress = commerce
        .warranties()
        .update_claim(
            claim.id,
            UpdateWarrantyClaim { status: Some(ClaimStatus::InProgress), ..Default::default() },
        )
        .expect("Failed to update claim to in_progress");
    assert_eq!(in_progress.status, ClaimStatus::InProgress);

    let result = commerce.warranties().update_claim(
        claim.id,
        UpdateWarrantyClaim {
            status: Some(ClaimStatus::Completed),
            resolution: Some(ClaimResolution::None),
            ..Default::default()
        },
    );
    assert!(result.is_err());

    let completed = commerce
        .warranties()
        .update_claim(
            claim.id,
            UpdateWarrantyClaim {
                status: Some(ClaimStatus::Completed),
                resolution: Some(ClaimResolution::Refund),
                ..Default::default()
            },
        )
        .expect("Failed to complete claim");
    assert_eq!(completed.status, ClaimStatus::Completed);
    assert_eq!(completed.resolution, ClaimResolution::Refund);
    assert!(completed.resolved_at.is_some());

    let claim2 = commerce
        .warranties()
        .create_claim(CreateWarrantyClaim {
            warranty_id: warranty.id,
            issue_description: "Battery drain".into(),
            ..Default::default()
        })
        .expect("Failed to create second claim");

    let result = commerce.warranties().update_claim(
        claim2.id,
        UpdateWarrantyClaim {
            status: Some(ClaimStatus::UnderReview),
            resolution: Some(ClaimResolution::Denied),
            ..Default::default()
        },
    );
    assert!(result.is_err());

    let result = commerce.warranties().update_claim(
        claim2.id,
        UpdateWarrantyClaim { status: Some(ClaimStatus::Denied), ..Default::default() },
    );
    assert!(result.is_err());

    let denied = commerce
        .warranties()
        .update_claim(
            claim2.id,
            UpdateWarrantyClaim {
                status: Some(ClaimStatus::Denied),
                denial_reason: Some("Out of coverage".into()),
                ..Default::default()
            },
        )
        .expect("Failed to deny claim");
    assert_eq!(denied.status, ClaimStatus::Denied);
    assert_eq!(denied.resolution, ClaimResolution::Denied);
    assert!(denied.resolved_at.is_some());
}

// ============================================================================
// Purchase Order Tests
// ============================================================================

#[test]
fn test_purchase_order_workflow() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let supplier = commerce
        .purchase_orders()
        .create_supplier(CreateSupplier {
            name: "Acme Supplies".into(),
            email: Some("orders@acme.test".into()),
            payment_terms: Some(PaymentTerms::Net30),
            ..Default::default()
        })
        .expect("Failed to create supplier");

    let po = commerce
        .purchase_orders()
        .create(CreatePurchaseOrder {
            supplier_id: supplier.id,
            items: vec![CreatePurchaseOrderItem {
                sku: "PART-001".into(),
                name: "Widget Part".into(),
                quantity: dec!(10),
                unit_cost: dec!(2.50),
                ..Default::default()
            }],
            ..Default::default()
        })
        .expect("Failed to create purchase order");

    assert_eq!(po.status, PurchaseOrderStatus::Draft);
    assert_eq!(po.items.len(), 1);

    let submitted = commerce.purchase_orders().submit(po.id.into()).expect("Failed to submit PO");
    assert_eq!(submitted.status, PurchaseOrderStatus::PendingApproval);

    let approved =
        commerce.purchase_orders().approve(po.id.into(), "tester").expect("Failed to approve PO");
    assert_eq!(approved.status, PurchaseOrderStatus::Approved);

    let sent = commerce.purchase_orders().send(po.id.into()).expect("Failed to send PO");
    assert_eq!(sent.status, PurchaseOrderStatus::Sent);

    let fetched = commerce
        .purchase_orders()
        .get_by_number(&po.po_number)
        .expect("Failed to get PO by number")
        .expect("PO not found");
    assert_eq!(fetched.id, po.id);

    let by_code = commerce
        .purchase_orders()
        .get_supplier_by_code(&supplier.supplier_code)
        .expect("Failed to get supplier by code")
        .expect("Supplier not found");
    assert_eq!(by_code.id, supplier.id);
}

// ============================================================================
// Invoice Tests
// ============================================================================

#[test]
fn test_invoice_send_and_payment() {
    let commerce = Commerce::new(":memory:").expect("Failed to create commerce");

    let customer = commerce
        .customers()
        .create(CreateCustomer {
            email: "invoice@example.com".into(),
            first_name: "Invoice".into(),
            last_name: "Test".into(),
            ..Default::default()
        })
        .expect("Failed to create customer");

    let invoice = commerce
        .invoices()
        .create(CreateInvoice {
            customer_id: customer.id,
            items: vec![CreateInvoiceItem {
                description: "Implementation services".into(),
                quantity: dec!(2),
                unit_price: dec!(100.00),
                ..Default::default()
            }],
            ..Default::default()
        })
        .expect("Failed to create invoice");

    assert_eq!(invoice.status, InvoiceStatus::Draft);
    assert_eq!(invoice.total, dec!(200.00));

    let sent = commerce.invoices().send(invoice.id.into()).expect("Failed to send invoice");
    assert_eq!(sent.status, InvoiceStatus::Sent);

    let paid = commerce
        .invoices()
        .record_payment(
            invoice.id.into(),
            RecordInvoicePayment {
                amount: dec!(200.00),
                payment_method: Some("card".into()),
                reference: Some("PAY-TEST-001".into()),
                ..Default::default()
            },
        )
        .expect("Failed to record payment");

    assert_eq!(paid.status, InvoiceStatus::Paid);
    assert_eq!(paid.balance_due, dec!(0));

    let fetched = commerce
        .invoices()
        .get_by_number(&invoice.invoice_number)
        .expect("Failed to get invoice by number")
        .expect("Invoice not found");
    assert_eq!(fetched.id, invoice.id);
}