stateset-core 1.22.0

Core domain models and business logic for StateSet iCommerce
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
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
//! General Ledger domain models
//!
//! Full double-entry accounting system supporting:
//! - Chart of Accounts with hierarchy
//! - Journal entries (balanced debits = credits)
//! - GL periods (open, closed, locked)
//! - Auto-posting from commerce transactions
//! - Trial balance and financial statements

use chrono::{DateTime, NaiveDate, Utc};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use stateset_primitives::CurrencyCode;
use std::fmt;
use std::str::FromStr;
use strum::{Display, EnumString};
use uuid::Uuid;

// ============================================================================
// Account Type Enums
// ============================================================================

/// GL Account type (follows standard accounting)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumString, Serialize, Deserialize)]
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AccountType {
    /// Cash, receivables, inventory, and other economic resources.
    Asset,
    /// Obligations owed to creditors and suppliers.
    Liability,
    /// Owner's residual interest in the business.
    Equity,
    /// Income earned from sales or services.
    Revenue,
    /// Costs incurred in earning revenue.
    Expense,
}

impl AccountType {
    /// Returns the normal balance side for this account type
    #[must_use]
    pub const fn normal_balance(&self) -> BalanceSide {
        match self {
            Self::Asset | Self::Expense => BalanceSide::Debit,
            Self::Liability | Self::Equity | Self::Revenue => BalanceSide::Credit,
        }
    }

    /// Returns true if this account type appears on the Balance Sheet
    #[must_use]
    pub const fn is_balance_sheet(&self) -> bool {
        matches!(self, Self::Asset | Self::Liability | Self::Equity)
    }

    /// Returns true if this account type appears on the Income Statement
    #[must_use]
    pub const fn is_income_statement(&self) -> bool {
        matches!(self, Self::Revenue | Self::Expense)
    }
}

/// Balance side (debit or credit)
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Display, EnumString, Serialize, Deserialize, Default,
)]
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum BalanceSide {
    /// Increases asset and expense accounts; decreases liabilities, equity, and revenue.
    #[default]
    Debit,
    /// Increases liability, equity, and revenue accounts; decreases assets and expenses.
    Credit,
}

/// Account sub-types for more granular classification
#[derive(Debug, Clone, Copy, PartialEq, Eq, Display, EnumString, Serialize, Deserialize)]
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AccountSubType {
    // Assets
    /// Cash and cash equivalents.
    Cash,
    /// Amounts owed by customers for goods or services delivered.
    AccountsReceivable,
    /// Goods held for sale or used in production.
    Inventory,
    /// Expenses paid in advance not yet recognized as cost.
    PrepaidExpense,
    /// Long-lived tangible assets such as equipment and buildings.
    FixedAsset,
    /// Contra-asset reducing the carrying value of fixed assets.
    AccumulatedDepreciation,
    /// Current assets not classified elsewhere.
    OtherCurrentAsset,
    /// Non-current assets not classified elsewhere.
    OtherNonCurrentAsset,
    // Liabilities
    /// Amounts owed to suppliers for goods or services received.
    AccountsPayable,
    /// Expenses incurred but not yet paid.
    AccruedLiabilities,
    /// Customer deposits or payments for goods/services not yet delivered.
    UnearnedRevenue,
    /// Debt due within one year.
    ShortTermDebt,
    /// Debt due beyond one year.
    LongTermDebt,
    /// Current liabilities not classified elsewhere.
    OtherCurrentLiability,
    /// Non-current liabilities not classified elsewhere.
    OtherNonCurrentLiability,
    // Equity
    /// Paid-in capital from shareholders.
    CommonStock,
    /// Cumulative earnings retained in the business.
    RetainedEarnings,
    /// Equity accounts not classified elsewhere.
    OtherEquity,
    // Revenue
    /// Revenue from product sales.
    SalesRevenue,
    /// Revenue from services rendered.
    ServiceRevenue,
    /// Revenue not classified elsewhere.
    OtherRevenue,
    // Expense
    /// Direct cost of goods sold to customers.
    CostOfGoodsSold,
    /// Recurring expenses related to running the business.
    OperatingExpense,
    /// Wages, salaries, and related employee costs.
    Payroll,
    /// Costs for leasing office or warehouse space.
    RentExpense,
    /// Electricity, water, and similar utility costs.
    UtilitiesExpense,
    /// Allocation of fixed asset cost over its useful life.
    DepreciationExpense,
    /// Cost of borrowing funds.
    InterestExpense,
    /// Income tax and other tax charges.
    TaxExpense,
    /// Expenses not classified elsewhere.
    OtherExpense,
}

/// Account status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum AccountStatus {
    /// Account accepts postings and appears in reports.
    #[default]
    Active,
    /// Account is temporarily disabled; no new postings allowed.
    Inactive,
    /// Account is permanently closed and hidden from normal views.
    Archived,
}

impl fmt::Display for AccountStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Active => write!(f, "active"),
            Self::Inactive => write!(f, "inactive"),
            Self::Archived => write!(f, "archived"),
        }
    }
}

impl FromStr for AccountStatus {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "active" => Ok(Self::Active),
            "inactive" => Ok(Self::Inactive),
            "archived" => Ok(Self::Archived),
            _ => Err(format!("Unknown account status: {s}")),
        }
    }
}

// ============================================================================
// Period & Journal Entry Enums
// ============================================================================

/// GL Period status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PeriodStatus {
    /// Period has not yet started; posting is not allowed.
    #[default]
    Future,
    /// Period is active and accepts journal entry postings.
    Open,
    /// Period has ended; no further postings permitted without re-opening.
    Closed,
    /// Period is permanently sealed; cannot be re-opened.
    Locked,
}

impl fmt::Display for PeriodStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Future => write!(f, "future"),
            Self::Open => write!(f, "open"),
            Self::Closed => write!(f, "closed"),
            Self::Locked => write!(f, "locked"),
        }
    }
}

impl FromStr for PeriodStatus {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "future" => Ok(Self::Future),
            "open" => Ok(Self::Open),
            "closed" => Ok(Self::Closed),
            "locked" => Ok(Self::Locked),
            _ => Err(format!("Unknown period status: {s}")),
        }
    }
}

/// Journal entry type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum JournalEntryType {
    /// Routine transaction entry.
    #[default]
    Standard,
    /// End-of-period accrual or deferral entry.
    Adjusting,
    /// Entry to close temporary accounts at period end.
    Closing,
    /// Auto-generated entry that reverses a prior adjusting entry.
    Reversing,
    /// Entry to establish opening balances for a new period or entity.
    Opening,
}

impl fmt::Display for JournalEntryType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Standard => write!(f, "standard"),
            Self::Adjusting => write!(f, "adjusting"),
            Self::Closing => write!(f, "closing"),
            Self::Reversing => write!(f, "reversing"),
            Self::Opening => write!(f, "opening"),
        }
    }
}

impl FromStr for JournalEntryType {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "standard" => Ok(Self::Standard),
            "adjusting" => Ok(Self::Adjusting),
            "closing" => Ok(Self::Closing),
            "reversing" => Ok(Self::Reversing),
            "opening" => Ok(Self::Opening),
            _ => Err(format!("Unknown journal entry type: {s}")),
        }
    }
}

/// Journal entry source
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum JournalEntrySource {
    /// Entry created manually by a user.
    #[default]
    Manual,
    /// Auto-generated when a customer invoice is posted.
    AutoInvoice,
    /// Auto-generated when a customer payment is received.
    AutoPayment,
    /// Auto-generated when a supplier bill is approved.
    AutoBill,
    /// Auto-generated when a supplier bill payment is made.
    AutoBillPayment,
    /// Auto-generated from an inventory transaction.
    AutoInventory,
    /// Auto-generated when an AR balance is written off.
    AutoWriteOff,
    /// Generated automatically during period-close processing.
    SystemClosing,
    /// Imported from an external system or file.
    Import,
}

impl fmt::Display for JournalEntrySource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Manual => write!(f, "manual"),
            Self::AutoInvoice => write!(f, "auto_invoice"),
            Self::AutoPayment => write!(f, "auto_payment"),
            Self::AutoBill => write!(f, "auto_bill"),
            Self::AutoBillPayment => write!(f, "auto_bill_payment"),
            Self::AutoInventory => write!(f, "auto_inventory"),
            Self::AutoWriteOff => write!(f, "auto_write_off"),
            Self::SystemClosing => write!(f, "system_closing"),
            Self::Import => write!(f, "import"),
        }
    }
}

impl FromStr for JournalEntrySource {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "manual" => Ok(Self::Manual),
            "auto_invoice" => Ok(Self::AutoInvoice),
            "auto_payment" => Ok(Self::AutoPayment),
            "auto_bill" => Ok(Self::AutoBill),
            "auto_bill_payment" => Ok(Self::AutoBillPayment),
            "auto_inventory" => Ok(Self::AutoInventory),
            "auto_write_off" => Ok(Self::AutoWriteOff),
            "system_closing" => Ok(Self::SystemClosing),
            "import" => Ok(Self::Import),
            _ => Err(format!("Unknown journal entry source: {s}")),
        }
    }
}

/// Journal entry status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum JournalEntryStatus {
    /// Entry is being prepared; not yet submitted for posting.
    #[default]
    Draft,
    /// Entry is awaiting approval before posting.
    Pending,
    /// Entry has been posted and affects account balances.
    Posted,
    /// Entry has been cancelled; has no effect on balances.
    Voided,
    /// Entry has been offset by a reversing entry.
    Reversed,
}

impl fmt::Display for JournalEntryStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Draft => write!(f, "draft"),
            Self::Pending => write!(f, "pending"),
            Self::Posted => write!(f, "posted"),
            Self::Voided => write!(f, "voided"),
            Self::Reversed => write!(f, "reversed"),
        }
    }
}

impl FromStr for JournalEntryStatus {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "draft" => Ok(Self::Draft),
            "pending" => Ok(Self::Pending),
            "posted" => Ok(Self::Posted),
            "voided" => Ok(Self::Voided),
            "reversed" => Ok(Self::Reversed),
            _ => Err(format!("Unknown journal entry status: {s}")),
        }
    }
}

// ============================================================================
// Core GL Structs
// ============================================================================

/// A GL Account (Chart of Accounts entry)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlAccount {
    /// Unique identifier for this account.
    pub id: Uuid,
    /// Structured account code (e.g. `"1010"`).
    pub account_number: String,
    /// Human-readable account name.
    pub name: String,
    /// Optional description of the account's purpose.
    pub description: Option<String>,
    /// Top-level classification (Asset, Liability, Equity, Revenue, Expense).
    pub account_type: AccountType,
    /// Finer-grained classification within the account type.
    pub account_sub_type: Option<AccountSubType>,
    /// Parent account for hierarchy grouping; `None` if top-level.
    pub parent_account_id: Option<Uuid>,
    /// If `true`, this is a summary header; postings go to child accounts.
    pub is_header: bool,
    /// If `true`, journal entry lines may be posted directly to this account.
    pub is_posting: bool,
    /// Expected side (Debit/Credit) that increases this account.
    pub normal_balance: BalanceSide,
    /// Currency in which this account is maintained.
    pub currency: CurrencyCode,
    /// Lifecycle status of the account.
    pub status: AccountStatus,
    /// Running balance as of the last posting.
    pub current_balance: Decimal,
    /// Timestamp of account creation.
    pub created_at: DateTime<Utc>,
    /// Timestamp of the last update.
    pub updated_at: DateTime<Utc>,
}

impl GlAccount {
    /// Returns true if this account can accept postings
    #[must_use]
    pub fn can_post(&self) -> bool {
        self.is_posting && self.status == AccountStatus::Active
    }

    /// Calculates the balance effect of a debit/credit
    #[must_use]
    pub fn balance_effect(&self, debit: Decimal, credit: Decimal) -> Decimal {
        // Keep behavior consistent with account type, even if persisted normal_balance drifts.
        match self.account_type.normal_balance() {
            BalanceSide::Debit => debit - credit,
            BalanceSide::Credit => credit - debit,
        }
    }
}

/// GL Period (accounting period)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GlPeriod {
    /// Unique identifier for this period.
    pub id: Uuid,
    /// Display name, typically in `YYYY-MM` format.
    pub period_name: String,
    /// Fiscal year this period belongs to.
    pub fiscal_year: i32,
    /// Sequential number within the fiscal year (1–12 for monthly).
    pub period_number: i32,
    /// First date of the period (inclusive).
    pub start_date: NaiveDate,
    /// Last date of the period (inclusive).
    pub end_date: NaiveDate,
    /// Current lifecycle status of the period.
    pub status: PeriodStatus,
    /// Timestamp when the period was closed.
    pub closed_at: Option<DateTime<Utc>>,
    /// User who closed the period.
    pub closed_by: Option<String>,
    /// Timestamp when the period was permanently locked.
    pub locked_at: Option<DateTime<Utc>>,
    /// User who locked the period.
    pub locked_by: Option<String>,
    /// Timestamp of period creation.
    pub created_at: DateTime<Utc>,
    /// Timestamp of the last update.
    pub updated_at: DateTime<Utc>,
}

impl GlPeriod {
    /// Returns true if the period allows posting
    #[must_use]
    pub fn can_post(&self) -> bool {
        self.status == PeriodStatus::Open
    }

    /// Returns true if a date falls within this period
    #[must_use]
    pub fn contains_date(&self, date: NaiveDate) -> bool {
        date >= self.start_date && date <= self.end_date
    }
}

/// Journal Entry header
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JournalEntry {
    /// Unique identifier for this journal entry.
    pub id: Uuid,
    /// Human-readable entry reference number (e.g. `"JE-20240101-ABCD1234"`).
    pub entry_number: String,
    /// Date the transaction occurred or is being recorded.
    pub entry_date: NaiveDate,
    /// Accounting period this entry belongs to.
    pub period_id: Uuid,
    /// Classification of the entry (standard, adjusting, closing, etc.).
    pub entry_type: JournalEntryType,
    /// System or process that created the entry.
    pub source: JournalEntrySource,
    /// Entity type of the originating document (e.g. `"invoice"`).
    pub source_document_type: Option<String>,
    /// Identifier of the originating document.
    pub source_document_id: Option<Uuid>,
    /// Narrative description of the transaction.
    pub description: String,
    /// Sum of all debit line amounts.
    pub total_debits: Decimal,
    /// Sum of all credit line amounts.
    pub total_credits: Decimal,
    /// `true` when `total_debits == total_credits`.
    pub is_balanced: bool,
    /// Current lifecycle status.
    pub status: JournalEntryStatus,
    /// Timestamp when the entry was posted to the ledger.
    pub posted_at: Option<DateTime<Utc>>,
    /// User who posted the entry.
    pub posted_by: Option<String>,
    /// Entry that this one reverses, if applicable.
    pub reversed_entry_id: Option<Uuid>,
    /// Entry created to reverse this one, if applicable.
    pub reversing_entry_id: Option<Uuid>,
    /// Individual debit/credit lines that make up this entry.
    pub lines: Vec<JournalEntryLine>,
    /// Timestamp of entry creation.
    pub created_at: DateTime<Utc>,
    /// Timestamp of the last update.
    pub updated_at: DateTime<Utc>,
}

impl JournalEntry {
    /// Returns true if debits equal credits
    #[must_use]
    pub fn is_balanced(&self) -> bool {
        self.total_debits == self.total_credits
    }

    /// Recalculates totals from lines
    pub fn recalculate_totals(&mut self) {
        self.total_debits = self.lines.iter().map(|l| l.debit_amount).sum();
        self.total_credits = self.lines.iter().map(|l| l.credit_amount).sum();
        self.is_balanced = self.total_debits == self.total_credits;
    }

    /// Returns true if entry can be posted
    pub fn can_post(&self) -> bool {
        if self.status != JournalEntryStatus::Draft || self.lines.is_empty() {
            return false;
        }

        if !self.lines.iter().all(JournalEntryLine::is_valid) {
            return false;
        }

        let calculated_debits: Decimal = self.lines.iter().map(|l| l.debit_amount).sum();
        let calculated_credits: Decimal = self.lines.iter().map(|l| l.credit_amount).sum();
        self.total_debits == calculated_debits
            && self.total_credits == calculated_credits
            && calculated_debits == calculated_credits
    }

    /// Returns true if entry can be voided
    #[must_use]
    pub fn can_void(&self) -> bool {
        self.status == JournalEntryStatus::Posted
    }
}

/// Journal Entry line (detail)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JournalEntryLine {
    /// Unique identifier for this line.
    pub id: Uuid,
    /// Parent journal entry.
    pub journal_entry_id: Uuid,
    /// Sequence number within the entry (1-based).
    pub line_number: i32,
    /// GL account being debited or credited.
    pub account_id: Uuid,
    /// Denormalized account number for reporting convenience.
    pub account_number: Option<String>,
    /// Denormalized account name for reporting convenience.
    pub account_name: Option<String>,
    /// Optional line-level narrative.
    pub description: Option<String>,
    /// Debit amount; exactly one of `debit_amount` or `credit_amount` must be non-zero.
    pub debit_amount: Decimal,
    /// Credit amount; exactly one of `debit_amount` or `credit_amount` must be non-zero.
    pub credit_amount: Decimal,
    /// Currency of the amounts on this line.
    pub currency: CurrencyCode,
    /// Entity type of a related sub-ledger record (e.g. `"invoice_line"`).
    pub reference_type: Option<String>,
    /// Identifier of the related sub-ledger record.
    pub reference_id: Option<Uuid>,
    /// Timestamp of line creation.
    pub created_at: DateTime<Utc>,
}

impl JournalEntryLine {
    /// Returns true if line has only debit or only credit
    #[must_use]
    pub fn is_valid(&self) -> bool {
        (self.debit_amount > Decimal::ZERO && self.credit_amount == Decimal::ZERO)
            || (self.debit_amount == Decimal::ZERO && self.credit_amount > Decimal::ZERO)
    }

    /// Returns the net amount (positive for debit, negative for credit)
    #[must_use]
    pub fn net_amount(&self) -> Decimal {
        self.debit_amount - self.credit_amount
    }
}

/// Auto-posting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoPostingConfig {
    pub id: Uuid,
    pub config_name: String,
    pub cash_account_id: Uuid,
    pub accounts_receivable_account_id: Uuid,
    pub inventory_account_id: Uuid,
    pub accounts_payable_account_id: Uuid,
    pub unearned_revenue_account_id: Option<Uuid>,
    pub sales_revenue_account_id: Uuid,
    pub shipping_revenue_account_id: Option<Uuid>,
    pub cogs_account_id: Uuid,
    pub bad_debt_expense_account_id: Option<Uuid>,
    /// Account receiving unrealized FX gains/losses posted by period-end revaluation.
    #[serde(default)]
    pub fx_gain_loss_account_id: Option<Uuid>,
    /// Auto-post a journal entry when fixed-asset depreciation is posted.
    #[serde(default)]
    pub auto_post_depreciation: bool,
    /// Auto-post a journal entry when deferred revenue is recognized.
    #[serde(default)]
    pub auto_post_revenue_recognition: bool,
    pub is_active: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

/// Account balance for a period
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountBalance {
    pub id: Uuid,
    pub account_id: Uuid,
    pub period_id: Uuid,
    pub opening_balance: Decimal,
    pub total_debits: Decimal,
    pub total_credits: Decimal,
    pub closing_balance: Decimal,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

// ============================================================================
// Financial Reports
// ============================================================================

/// Trial Balance line
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrialBalanceLine {
    pub account_id: Uuid,
    pub account_number: String,
    pub account_name: String,
    pub account_type: AccountType,
    pub debit_balance: Decimal,
    pub credit_balance: Decimal,
}

/// Trial Balance report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrialBalance {
    pub as_of_date: NaiveDate,
    pub period_id: Option<Uuid>,
    pub total_debits: Decimal,
    pub total_credits: Decimal,
    pub is_balanced: bool,
    pub lines: Vec<TrialBalanceLine>,
}

impl TrialBalance {
    /// Returns true if debits equal credits
    #[must_use]
    pub fn is_balanced(&self) -> bool {
        self.total_debits == self.total_credits
    }
}

/// Balance Sheet line item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceSheetLine {
    pub account_id: Uuid,
    pub account_number: String,
    pub account_name: String,
    pub account_sub_type: Option<AccountSubType>,
    pub balance: Decimal,
    pub indent_level: i32,
    pub is_total: bool,
}

/// Balance Sheet report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceSheet {
    pub as_of_date: NaiveDate,
    pub total_assets: Decimal,
    pub total_liabilities: Decimal,
    pub total_equity: Decimal,
    pub assets: Vec<BalanceSheetLine>,
    pub liabilities: Vec<BalanceSheetLine>,
    pub equity: Vec<BalanceSheetLine>,
}

impl BalanceSheet {
    /// Returns true if assets equal liabilities plus equity
    #[must_use]
    pub fn is_balanced(&self) -> bool {
        self.total_assets == self.total_liabilities + self.total_equity
    }
}

/// Income Statement line item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncomeStatementLine {
    pub account_id: Uuid,
    pub account_number: String,
    pub account_name: String,
    pub account_sub_type: Option<AccountSubType>,
    pub amount: Decimal,
    pub indent_level: i32,
    pub is_total: bool,
}

/// Income Statement report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncomeStatement {
    pub period_start: NaiveDate,
    pub period_end: NaiveDate,
    pub total_revenue: Decimal,
    pub total_expenses: Decimal,
    pub net_income: Decimal,
    pub revenue_lines: Vec<IncomeStatementLine>,
    pub expense_lines: Vec<IncomeStatementLine>,
}

// ============================================================================
// Input Types
// ============================================================================

/// Input for creating a general ledger account
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateGlAccount {
    pub account_number: String,
    pub name: String,
    pub description: Option<String>,
    pub account_type: AccountType,
    pub account_sub_type: Option<AccountSubType>,
    pub parent_account_id: Option<Uuid>,
    pub is_header: Option<bool>,
    pub is_posting: Option<bool>,
    pub currency: Option<CurrencyCode>,
}

/// Input for updating a general ledger account
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateGlAccount {
    pub name: Option<String>,
    pub description: Option<String>,
    pub parent_account_id: Option<Uuid>,
    pub status: Option<AccountStatus>,
}

/// Input for creating a fiscal period
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateGlPeriod {
    pub period_name: String,
    pub fiscal_year: i32,
    pub period_number: i32,
    pub start_date: NaiveDate,
    pub end_date: NaiveDate,
}

/// Input for creating a journal entry with lines
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateJournalEntry {
    pub entry_date: NaiveDate,
    pub entry_type: Option<JournalEntryType>,
    pub description: String,
    pub lines: Vec<CreateJournalEntryLine>,
    pub source_document_type: Option<String>,
    pub source_document_id: Option<Uuid>,
    pub auto_post: Option<bool>,
}

/// Input for a journal entry line
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateJournalEntryLine {
    pub account_id: Uuid,
    pub description: Option<String>,
    pub debit_amount: Decimal,
    pub credit_amount: Decimal,
    pub reference_type: Option<String>,
    pub reference_id: Option<Uuid>,
}

impl CreateJournalEntryLine {
    /// Create a debit line for an account
    #[must_use]
    pub const fn debit(account_id: Uuid, amount: Decimal, description: Option<String>) -> Self {
        Self {
            account_id,
            description,
            debit_amount: amount,
            credit_amount: Decimal::ZERO,
            reference_type: None,
            reference_id: None,
        }
    }

    /// Create a credit line for an account
    #[must_use]
    pub const fn credit(account_id: Uuid, amount: Decimal, description: Option<String>) -> Self {
        Self {
            account_id,
            description,
            debit_amount: Decimal::ZERO,
            credit_amount: amount,
            reference_type: None,
            reference_id: None,
        }
    }
}

/// Configuration for automatic postings between accounts
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateAutoPostingConfig {
    pub config_name: String,
    pub cash_account_id: Uuid,
    pub accounts_receivable_account_id: Uuid,
    pub inventory_account_id: Uuid,
    pub accounts_payable_account_id: Uuid,
    pub unearned_revenue_account_id: Option<Uuid>,
    pub sales_revenue_account_id: Uuid,
    pub shipping_revenue_account_id: Option<Uuid>,
    pub cogs_account_id: Uuid,
    pub bad_debt_expense_account_id: Option<Uuid>,
    /// Account receiving unrealized FX gains/losses posted by period-end revaluation.
    #[serde(default)]
    pub fx_gain_loss_account_id: Option<Uuid>,
    /// Auto-post a journal entry when fixed-asset depreciation is posted (default off).
    #[serde(default)]
    pub auto_post_depreciation: bool,
    /// Auto-post a journal entry when deferred revenue is recognized (default off).
    #[serde(default)]
    pub auto_post_revenue_recognition: bool,
}

// ============================================================================
// Filter Types
// ============================================================================

/// Filter for listing general ledger accounts
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GlAccountFilter {
    pub account_type: Option<AccountType>,
    pub account_sub_type: Option<AccountSubType>,
    pub parent_account_id: Option<Uuid>,
    pub status: Option<AccountStatus>,
    pub is_posting: Option<bool>,
    pub is_header: Option<bool>,
    pub search: Option<String>,
    pub limit: Option<u32>,
    pub offset: Option<u32>,
}

/// Filter for listing fiscal periods
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct GlPeriodFilter {
    pub fiscal_year: Option<i32>,
    pub status: Option<PeriodStatus>,
    pub limit: Option<u32>,
    pub offset: Option<u32>,
}

/// Filter for listing journal entries
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct JournalEntryFilter {
    pub period_id: Option<Uuid>,
    pub entry_type: Option<JournalEntryType>,
    pub source: Option<JournalEntrySource>,
    pub status: Option<JournalEntryStatus>,
    pub account_id: Option<Uuid>,
    pub from_date: Option<NaiveDate>,
    pub to_date: Option<NaiveDate>,
    pub source_document_type: Option<String>,
    pub source_document_id: Option<Uuid>,
    pub search: Option<String>,
    pub limit: Option<u32>,
    pub offset: Option<u32>,
}

// ============================================================================
// FX Revaluation
// ============================================================================

/// `reference_type` stamped on journal entry lines created by FX revaluation.
///
/// Lines carrying this marker are base-currency adjustments, so they are
/// excluded when deriving an account's outstanding foreign-currency balance
/// for subsequent revaluations.
pub const FX_REVALUATION_REFERENCE: &str = "fx_revaluation";

/// Per-account result of a period-end FX revaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RevaluationLine {
    /// Account being revalued.
    pub account_id: Uuid,
    /// Denormalized account number.
    pub account_number: String,
    /// Denormalized account name.
    pub account_name: String,
    /// Currency the account is maintained in (differs from base currency).
    pub currency: CurrencyCode,
    /// Side (Debit/Credit) that increases this account.
    pub normal_balance: BalanceSide,
    /// Outstanding balance in the account's own currency (normal-balance
    /// terms), derived from posted lines excluding prior FX adjustments.
    pub foreign_balance: Decimal,
    /// Value currently carried on the books (base-currency terms).
    pub carrying_value: Decimal,
    /// Exchange rate used: 1 unit of account currency = `rate` base units.
    pub rate: Decimal,
    /// `foreign_balance * rate`, rounded to base-currency precision.
    pub revalued_value: Decimal,
    /// `revalued_value - carrying_value` in normal-balance terms.
    pub adjustment: Decimal,
    /// Unrealized FX gain (positive) or loss (negative) in base currency.
    pub unrealized_gain_loss: Decimal,
}

/// Result of a period-end FX revaluation run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RevaluationResult {
    /// Date the revaluation is effective (journal entry date).
    pub as_of_date: NaiveDate,
    /// Base (functional) currency balances were revalued into.
    pub base_currency: CurrencyCode,
    /// Sum of `unrealized_gain_loss` across all lines.
    pub total_unrealized_gain_loss: Decimal,
    /// Per-account revaluation detail (includes zero-adjustment accounts).
    pub lines: Vec<RevaluationLine>,
    /// Balanced, posted adjusting entry for the net delta; `None` when every
    /// evaluated account required no adjustment.
    pub journal_entry: Option<JournalEntry>,
}

/// Compute the unrealized FX gain/loss for one foreign-currency account.
///
/// `foreign_balance` is the account's outstanding balance in its own currency
/// (normal-balance terms, excluding prior revaluation adjustments); `rate`
/// converts one unit of the account currency into the base currency;
/// `base_decimal_places` is the base currency's precision.
#[must_use]
pub fn compute_revaluation_line(
    account: &GlAccount,
    foreign_balance: Decimal,
    rate: Decimal,
    base_decimal_places: u32,
) -> RevaluationLine {
    let normal_balance = account.account_type.normal_balance();
    let revalued_value = (foreign_balance * rate).round_dp(base_decimal_places);
    let adjustment = revalued_value - account.current_balance;
    // Growing a debit-normal account (asset) is a gain; growing a
    // credit-normal account (liability) is a loss.
    let unrealized_gain_loss = match normal_balance {
        BalanceSide::Credit => -adjustment,
        _ => adjustment,
    };
    RevaluationLine {
        account_id: account.id,
        account_number: account.account_number.clone(),
        account_name: account.name.clone(),
        currency: account.currency,
        normal_balance,
        foreign_balance,
        carrying_value: account.current_balance,
        rate,
        revalued_value,
        adjustment,
        unrealized_gain_loss,
    }
}

/// Build balanced journal entry lines for a set of revaluation adjustments.
///
/// Each non-zero adjustment posts on the side that moves the account toward
/// its revalued carrying amount; the net offset posts to `fx_account_id`
/// (credit for a net gain, debit for a net loss). Returns an empty vector
/// when no account requires adjustment.
#[must_use]
pub fn build_revaluation_journal_lines(
    lines: &[RevaluationLine],
    fx_account_id: Uuid,
) -> Vec<CreateJournalEntryLine> {
    let mut entry_lines = Vec::new();
    // Positive => the FX account must be credited (net gain).
    let mut fx_net = Decimal::ZERO;

    for line in lines {
        if line.adjustment.is_zero() {
            continue;
        }
        let amount = line.adjustment.abs();
        let increase = line.adjustment > Decimal::ZERO;
        let debit_side = match line.normal_balance {
            BalanceSide::Credit => !increase,
            _ => increase,
        };
        let (debit_amount, credit_amount) = if debit_side {
            fx_net += amount;
            (amount, Decimal::ZERO)
        } else {
            fx_net -= amount;
            (Decimal::ZERO, amount)
        };
        entry_lines.push(CreateJournalEntryLine {
            account_id: line.account_id,
            description: Some(format!(
                "FX revaluation of {} ({})",
                line.account_number, line.currency
            )),
            debit_amount,
            credit_amount,
            reference_type: Some(FX_REVALUATION_REFERENCE.to_string()),
            reference_id: Some(line.account_id),
        });
    }

    if !fx_net.is_zero() {
        let amount = fx_net.abs();
        let (debit_amount, credit_amount) =
            if fx_net > Decimal::ZERO { (Decimal::ZERO, amount) } else { (amount, Decimal::ZERO) };
        entry_lines.push(CreateJournalEntryLine {
            account_id: fx_account_id,
            description: Some("Unrealized FX gain/loss".to_string()),
            debit_amount,
            credit_amount,
            reference_type: Some(FX_REVALUATION_REFERENCE.to_string()),
            reference_id: None,
        });
    }

    entry_lines
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Generate a journal entry number using a timestamp
#[must_use]
pub fn generate_journal_entry_number() -> String {
    let timestamp = chrono::Utc::now().format("%Y%m%d%H%M%S%3f").to_string();
    let suffix = Uuid::new_v4().simple().to_string();
    format!("JE-{}-{}", timestamp, &suffix[..8])
}

/// Generate a period name in YYYY-MM format
#[must_use]
pub fn generate_period_name(year: i32, month: i32) -> String {
    format!("{year}-{month:02}")
}

/// Create a default Chart of Accounts
#[must_use]
pub fn create_default_chart_of_accounts() -> Vec<CreateGlAccount> {
    vec![
        // Assets (1xxx)
        CreateGlAccount {
            account_number: "1000".into(),
            name: "Assets".into(),
            description: Some("All asset accounts".into()),
            account_type: AccountType::Asset,
            account_sub_type: None,
            parent_account_id: None,
            is_header: Some(true),
            is_posting: Some(false),
            currency: None,
        },
        CreateGlAccount {
            account_number: "1010".into(),
            name: "Cash".into(),
            description: Some("Cash and cash equivalents".into()),
            account_type: AccountType::Asset,
            account_sub_type: Some(AccountSubType::Cash),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
        CreateGlAccount {
            account_number: "1100".into(),
            name: "Accounts Receivable".into(),
            description: Some("Customer receivables".into()),
            account_type: AccountType::Asset,
            account_sub_type: Some(AccountSubType::AccountsReceivable),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
        CreateGlAccount {
            account_number: "1200".into(),
            name: "Inventory".into(),
            description: Some("Merchandise inventory".into()),
            account_type: AccountType::Asset,
            account_sub_type: Some(AccountSubType::Inventory),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
        // Liabilities (2xxx)
        CreateGlAccount {
            account_number: "2000".into(),
            name: "Liabilities".into(),
            description: Some("All liability accounts".into()),
            account_type: AccountType::Liability,
            account_sub_type: None,
            parent_account_id: None,
            is_header: Some(true),
            is_posting: Some(false),
            currency: None,
        },
        CreateGlAccount {
            account_number: "2010".into(),
            name: "Accounts Payable".into(),
            description: Some("Supplier payables".into()),
            account_type: AccountType::Liability,
            account_sub_type: Some(AccountSubType::AccountsPayable),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
        // Equity (3xxx)
        CreateGlAccount {
            account_number: "3000".into(),
            name: "Equity".into(),
            description: Some("Owner's equity accounts".into()),
            account_type: AccountType::Equity,
            account_sub_type: None,
            parent_account_id: None,
            is_header: Some(true),
            is_posting: Some(false),
            currency: None,
        },
        CreateGlAccount {
            account_number: "3100".into(),
            name: "Retained Earnings".into(),
            description: Some("Accumulated profits".into()),
            account_type: AccountType::Equity,
            account_sub_type: Some(AccountSubType::RetainedEarnings),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
        // Revenue (4xxx)
        CreateGlAccount {
            account_number: "4000".into(),
            name: "Revenue".into(),
            description: Some("All revenue accounts".into()),
            account_type: AccountType::Revenue,
            account_sub_type: None,
            parent_account_id: None,
            is_header: Some(true),
            is_posting: Some(false),
            currency: None,
        },
        CreateGlAccount {
            account_number: "4010".into(),
            name: "Sales Revenue".into(),
            description: Some("Product sales".into()),
            account_type: AccountType::Revenue,
            account_sub_type: Some(AccountSubType::SalesRevenue),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
        // Expenses (5xxx)
        CreateGlAccount {
            account_number: "5000".into(),
            name: "Expenses".into(),
            description: Some("All expense accounts".into()),
            account_type: AccountType::Expense,
            account_sub_type: None,
            parent_account_id: None,
            is_header: Some(true),
            is_posting: Some(false),
            currency: None,
        },
        CreateGlAccount {
            account_number: "5010".into(),
            name: "Cost of Goods Sold".into(),
            description: Some("Direct cost of products sold".into()),
            account_type: AccountType::Expense,
            account_sub_type: Some(AccountSubType::CostOfGoodsSold),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
        CreateGlAccount {
            account_number: "5900".into(),
            name: "Bad Debt Expense".into(),
            description: Some("Uncollectible accounts written off".into()),
            account_type: AccountType::Expense,
            account_sub_type: Some(AccountSubType::OtherExpense),
            parent_account_id: None,
            is_header: Some(false),
            is_posting: Some(true),
            currency: None,
        },
    ]
}

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

    fn foreign_account(account_type: AccountType, carrying: Decimal) -> GlAccount {
        let now = Utc::now();
        GlAccount {
            id: Uuid::new_v4(),
            account_number: "1015".into(),
            name: "EUR Cash".into(),
            description: None,
            account_type,
            account_sub_type: None,
            parent_account_id: None,
            is_header: false,
            is_posting: true,
            normal_balance: account_type.normal_balance(),
            currency: CurrencyCode::EUR,
            status: AccountStatus::Active,
            current_balance: carrying,
            created_at: now,
            updated_at: now,
        }
    }

    #[test]
    fn revaluation_gain_on_debit_normal_account() {
        // Booked 1000 EUR at 1.00; rate is now 1.10 => 100 unrealized gain.
        let account = foreign_account(AccountType::Asset, dec!(1000));
        let line = compute_revaluation_line(&account, dec!(1000), dec!(1.10), 2);
        assert_eq!(line.revalued_value, dec!(1100.00));
        assert_eq!(line.adjustment, dec!(100.00));
        assert_eq!(line.unrealized_gain_loss, dec!(100.00));
    }

    #[test]
    fn revaluation_loss_on_debit_normal_account() {
        let account = foreign_account(AccountType::Asset, dec!(1000));
        let line = compute_revaluation_line(&account, dec!(1000), dec!(0.85), 2);
        assert_eq!(line.adjustment, dec!(-150.00));
        assert_eq!(line.unrealized_gain_loss, dec!(-150.00));
    }

    #[test]
    fn revaluation_on_credit_normal_account_flips_sign() {
        // A payable growing in base terms is a loss.
        let account = foreign_account(AccountType::Liability, dec!(500));
        let line = compute_revaluation_line(&account, dec!(500), dec!(1.20), 2);
        assert_eq!(line.adjustment, dec!(100.00));
        assert_eq!(line.unrealized_gain_loss, dec!(-100.00));
    }

    #[test]
    fn revaluation_noop_when_rate_unchanged() {
        let account = foreign_account(AccountType::Asset, dec!(1000));
        let line = compute_revaluation_line(&account, dec!(1000), dec!(1), 2);
        assert!(line.adjustment.is_zero());
        assert!(line.unrealized_gain_loss.is_zero());
        assert!(build_revaluation_journal_lines(&[line], Uuid::new_v4()).is_empty());
    }

    #[test]
    fn revaluation_rounds_to_base_precision() {
        let account = foreign_account(AccountType::Asset, dec!(0));
        let line = compute_revaluation_line(&account, dec!(100.333), dec!(1.005), 2);
        assert_eq!(line.revalued_value, dec!(100.83));
    }

    #[test]
    fn revaluation_journal_lines_are_balanced() {
        let asset = foreign_account(AccountType::Asset, dec!(1000));
        let liability = foreign_account(AccountType::Liability, dec!(500));
        let fx_account = Uuid::new_v4();

        let lines = vec![
            compute_revaluation_line(&asset, dec!(1000), dec!(1.10), 2), // +100 gain
            compute_revaluation_line(&liability, dec!(500), dec!(1.20), 2), // -100 loss
        ];
        let je_lines = build_revaluation_journal_lines(&lines, fx_account);

        // Asset debit 100, liability credit 100 — nets to zero, so no FX line.
        assert_eq!(je_lines.len(), 2);
        let debits: Decimal = je_lines.iter().map(|l| l.debit_amount).sum();
        let credits: Decimal = je_lines.iter().map(|l| l.credit_amount).sum();
        assert_eq!(debits, credits);
        assert!(je_lines.iter().all(|l| l.reference_type.as_deref() == Some("fx_revaluation")));
    }

    #[test]
    fn revaluation_journal_lines_offset_net_gain_to_fx_account() {
        let asset = foreign_account(AccountType::Asset, dec!(1000));
        let fx_account = Uuid::new_v4();
        let lines = vec![compute_revaluation_line(&asset, dec!(1000), dec!(1.10), 2)];
        let je_lines = build_revaluation_journal_lines(&lines, fx_account);

        assert_eq!(je_lines.len(), 2);
        assert_eq!(je_lines[0].debit_amount, dec!(100.00));
        assert_eq!(je_lines[1].account_id, fx_account);
        assert_eq!(je_lines[1].credit_amount, dec!(100.00));
    }

    #[test]
    fn revaluation_journal_lines_offset_net_loss_to_fx_account() {
        let liability = foreign_account(AccountType::Liability, dec!(500));
        let fx_account = Uuid::new_v4();
        let lines = vec![compute_revaluation_line(&liability, dec!(500), dec!(1.20), 2)];
        let je_lines = build_revaluation_journal_lines(&lines, fx_account);

        assert_eq!(je_lines.len(), 2);
        assert_eq!(je_lines[0].credit_amount, dec!(100.00));
        assert_eq!(je_lines[1].account_id, fx_account);
        assert_eq!(je_lines[1].debit_amount, dec!(100.00));
    }
}