kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Reputation system models for output commitments and reputation tracking

use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use rust_decimal_macros::dec;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use std::fmt;
use uuid::Uuid;

use super::user::ValidationError;

/// Output commitment made by a token issuer
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct OutputCommitment {
    /// Unique identifier for this commitment
    pub commitment_id: Uuid,
    /// Token associated with this commitment
    pub token_id: Uuid,
    /// User who made the commitment (token issuer)
    pub user_id: Uuid,
    /// Title/summary of the commitment
    pub title: String,
    /// Detailed description of what's being committed to
    pub description: String,
    /// Deadline for fulfilling this commitment
    pub deadline: DateTime<Utc>,
    /// Status of the commitment
    pub status: CommitmentStatus,
    /// Evidence URL or description (submitted by issuer)
    pub evidence: Option<String>,
    /// Evidence submission timestamp
    pub evidence_submitted_at: Option<DateTime<Utc>>,
    /// Verification result
    pub verification_result: Option<VerificationResult>,
    /// Admin who verified (if verified)
    pub verified_by_user_id: Option<Uuid>,
    /// Verification timestamp
    pub verified_at: Option<DateTime<Utc>>,
    /// Admin notes/comments
    pub verification_notes: Option<String>,
    /// Impact on reputation score (calculated based on importance)
    pub reputation_impact: Decimal,
    /// Category of commitment
    pub category: CommitmentCategory,
    /// Whether this commitment is public or private
    pub is_public: bool,
    /// Milestone number (for multi-milestone commitments)
    pub milestone_number: Option<i32>,
    /// Timestamp when the commitment was created
    pub created_at: DateTime<Utc>,
    /// Timestamp when the commitment was last updated
    pub updated_at: DateTime<Utc>,
}

/// Status of a commitment
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "snake_case")]
#[derive(Default)]
pub enum CommitmentStatus {
    /// Active commitment, not yet due
    #[default]
    Active,
    /// Awaiting evidence submission
    AwaitingEvidence,
    /// Evidence submitted, pending verification
    PendingVerification,
    /// Verified as fulfilled
    Fulfilled,
    /// Verified as not fulfilled
    Failed,
    /// Cancelled by issuer
    Cancelled,
    /// Expired without evidence
    Expired,
}

impl fmt::Display for CommitmentStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CommitmentStatus::Active => write!(f, "active"),
            CommitmentStatus::AwaitingEvidence => write!(f, "awaiting_evidence"),
            CommitmentStatus::PendingVerification => write!(f, "pending_verification"),
            CommitmentStatus::Fulfilled => write!(f, "fulfilled"),
            CommitmentStatus::Failed => write!(f, "failed"),
            CommitmentStatus::Cancelled => write!(f, "cancelled"),
            CommitmentStatus::Expired => write!(f, "expired"),
        }
    }
}

/// Verification result for a commitment
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "lowercase")]
#[derive(Default)]
pub enum VerificationResult {
    /// Commitment was successfully fulfilled
    #[default]
    Approved,
    /// Commitment was not fulfilled or evidence insufficient
    Rejected,
    /// Partially fulfilled (may have reduced reputation impact)
    Partial,
}

impl fmt::Display for VerificationResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            VerificationResult::Approved => write!(f, "approved"),
            VerificationResult::Rejected => write!(f, "rejected"),
            VerificationResult::Partial => write!(f, "partial"),
        }
    }
}

/// Category of commitment
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "snake_case")]
#[derive(Default)]
pub enum CommitmentCategory {
    /// Product development milestone
    Development,
    /// Content creation (blog post, video, etc.)
    Content,
    /// Community event or activity
    Community,
    /// Partnership or collaboration
    Partnership,
    /// Revenue or business metric
    Revenue,
    /// User growth metric
    Growth,
    /// Other category
    #[default]
    Other,
}

impl fmt::Display for CommitmentCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CommitmentCategory::Development => write!(f, "development"),
            CommitmentCategory::Content => write!(f, "content"),
            CommitmentCategory::Community => write!(f, "community"),
            CommitmentCategory::Partnership => write!(f, "partnership"),
            CommitmentCategory::Revenue => write!(f, "revenue"),
            CommitmentCategory::Growth => write!(f, "growth"),
            CommitmentCategory::Other => write!(f, "other"),
        }
    }
}

impl OutputCommitment {
    /// Check if commitment is overdue
    pub fn is_overdue(&self) -> bool {
        self.deadline < Utc::now()
            && !matches!(
                self.status,
                CommitmentStatus::Fulfilled | CommitmentStatus::Cancelled
            )
    }

    /// Check if evidence can be submitted
    pub fn can_submit_evidence(&self) -> bool {
        matches!(
            self.status,
            CommitmentStatus::Active | CommitmentStatus::AwaitingEvidence
        )
    }

    /// Check if admin can verify
    pub fn can_verify(&self) -> bool {
        matches!(self.status, CommitmentStatus::PendingVerification)
    }

    /// Calculate days until deadline
    pub fn days_until_deadline(&self) -> i64 {
        (self.deadline - Utc::now()).num_days()
    }

    /// Get reputation impact based on verification result
    pub fn calculate_actual_reputation_impact(&self) -> Decimal {
        match self.verification_result {
            Some(VerificationResult::Approved) => self.reputation_impact,
            Some(VerificationResult::Partial) => self.reputation_impact / dec!(2),
            Some(VerificationResult::Rejected) => -self.reputation_impact,
            None => dec!(0),
        }
    }
}

impl fmt::Display for OutputCommitment {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Commitment({}, {}, status={}, deadline={})",
            self.commitment_id,
            self.title,
            self.status,
            self.deadline.format("%Y-%m-%d")
        )
    }
}

/// Request to create a new commitment
#[derive(Debug, Deserialize)]
pub struct CreateCommitmentRequest {
    /// Short title of the commitment
    pub title: String,
    /// Detailed description of what is being committed to
    pub description: String,
    /// Deadline by which the commitment must be fulfilled
    pub deadline: DateTime<Utc>,
    /// Category of the commitment
    pub category: CommitmentCategory,
    /// Reputation points at stake (positive)
    pub reputation_impact: Decimal,
    /// Whether this commitment is publicly visible
    pub is_public: bool,
    /// Milestone number for multi-milestone commitments
    pub milestone_number: Option<i32>,
}

impl CreateCommitmentRequest {
    /// Validate the create-commitment request fields
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.title.is_empty() {
            return Err(ValidationError("Title is required".to_string()));
        }
        if self.title.len() > 200 {
            return Err(ValidationError(
                "Title must be at most 200 characters".to_string(),
            ));
        }
        if self.description.is_empty() {
            return Err(ValidationError("Description is required".to_string()));
        }
        if self.description.len() > 5000 {
            return Err(ValidationError(
                "Description must be at most 5000 characters".to_string(),
            ));
        }
        if self.deadline <= Utc::now() {
            return Err(ValidationError(
                "Deadline must be in the future".to_string(),
            ));
        }
        // Reasonable deadline range (1 day to 2 years)
        let days_until = (self.deadline - Utc::now()).num_days();
        if days_until < 1 {
            return Err(ValidationError(
                "Deadline must be at least 1 day in the future".to_string(),
            ));
        }
        if days_until > 730 {
            return Err(ValidationError(
                "Deadline cannot be more than 2 years in the future".to_string(),
            ));
        }
        if self.reputation_impact < dec!(0) {
            return Err(ValidationError(
                "Reputation impact cannot be negative".to_string(),
            ));
        }
        if self.reputation_impact > dec!(100) {
            return Err(ValidationError(
                "Reputation impact cannot exceed 100 points".to_string(),
            ));
        }
        Ok(())
    }
}

/// Request to submit evidence for a commitment
#[derive(Debug, Deserialize)]
pub struct SubmitEvidenceRequest {
    /// URL or textual description of the evidence
    pub evidence: String,
}

impl SubmitEvidenceRequest {
    /// Validate the evidence submission request
    pub fn validate(&self) -> Result<(), ValidationError> {
        if self.evidence.is_empty() {
            return Err(ValidationError("Evidence is required".to_string()));
        }
        if self.evidence.len() > 10000 {
            return Err(ValidationError(
                "Evidence must be at most 10000 characters".to_string(),
            ));
        }
        Ok(())
    }
}

/// Request to verify a commitment (admin only)
#[derive(Debug, Deserialize)]
pub struct VerifyCommitmentRequest {
    /// Verification outcome
    pub result: VerificationResult,
    /// Optional reviewer notes or explanation
    pub notes: Option<String>,
}

impl VerifyCommitmentRequest {
    /// Validate the verification request fields
    pub fn validate(&self) -> Result<(), ValidationError> {
        if let Some(ref notes) = self.notes {
            if notes.len() > 2000 {
                return Err(ValidationError(
                    "Notes must be at most 2000 characters".to_string(),
                ));
            }
        }
        Ok(())
    }
}

/// Reputation event tracking changes to user reputation
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct ReputationEvent {
    /// Unique identifier for this reputation event
    pub event_id: Uuid,
    /// User whose reputation was changed
    pub user_id: Uuid,
    /// Type of event that caused reputation change
    pub event_type: ReputationEventType,
    /// Change in reputation score (can be positive or negative)
    pub score_change: Decimal,
    /// Reputation score after this event
    pub new_score: Decimal,
    /// Related commitment (if applicable)
    pub commitment_id: Option<Uuid>,
    /// Related trade (if applicable)
    pub trade_id: Option<Uuid>,
    /// Description of the event
    pub description: String,
    /// Admin who triggered this event (if manually adjusted)
    pub triggered_by_user_id: Option<Uuid>,
    /// Timestamp when this event was recorded
    pub created_at: DateTime<Utc>,
}

/// Type of reputation event
#[derive(Debug, Clone, Copy, Serialize, Deserialize, sqlx::Type, PartialEq, Eq)]
#[sqlx(type_name = "varchar", rename_all = "snake_case")]
#[derive(Default)]
pub enum ReputationEventType {
    /// Commitment fulfilled successfully
    CommitmentFulfilled,
    /// Commitment failed or rejected
    CommitmentFailed,
    /// Commitment partially fulfilled
    CommitmentPartial,
    /// Successful trade completion
    TradeFeedback,
    /// Manual adjustment by admin
    #[default]
    ManualAdjustment,
    /// KYC verification completed
    KycVerified,
    /// Community report or flag
    CommunityReport,
    /// Token performance milestone
    TokenMilestone,
    /// Time-based decay
    TimeDecay,
}

impl fmt::Display for ReputationEventType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReputationEventType::CommitmentFulfilled => write!(f, "commitment_fulfilled"),
            ReputationEventType::CommitmentFailed => write!(f, "commitment_failed"),
            ReputationEventType::CommitmentPartial => write!(f, "commitment_partial"),
            ReputationEventType::TradeFeedback => write!(f, "trade_feedback"),
            ReputationEventType::ManualAdjustment => write!(f, "manual_adjustment"),
            ReputationEventType::KycVerified => write!(f, "kyc_verified"),
            ReputationEventType::CommunityReport => write!(f, "community_report"),
            ReputationEventType::TokenMilestone => write!(f, "token_milestone"),
            ReputationEventType::TimeDecay => write!(f, "time_decay"),
        }
    }
}

impl fmt::Display for ReputationEvent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "ReputationEvent({}, user={}, type={}, change={:+})",
            self.event_id, self.user_id, self.event_type, self.score_change
        )
    }
}

/// Reputation score calculator
pub struct ReputationCalculator;

impl ReputationCalculator {
    /// Base reputation for new users
    pub const BASE_REPUTATION: Decimal = dec!(50);

    /// Minimum possible reputation
    pub const MIN_REPUTATION: Decimal = dec!(0);

    /// Maximum possible reputation
    pub const MAX_REPUTATION: Decimal = dec!(100);

    /// Reputation bonus for KYC verification
    pub const KYC_BONUS: Decimal = dec!(10);

    /// Create a new reputation event
    #[allow(clippy::too_many_arguments)]
    pub fn create_event(
        user_id: Uuid,
        event_type: ReputationEventType,
        score_change: Decimal,
        current_score: Decimal,
        description: String,
        commitment_id: Option<Uuid>,
        trade_id: Option<Uuid>,
        triggered_by_user_id: Option<Uuid>,
    ) -> ReputationEvent {
        let new_score = Self::clamp_score(current_score + score_change);

        ReputationEvent {
            event_id: Uuid::new_v4(),
            user_id,
            event_type,
            score_change,
            new_score,
            commitment_id,
            trade_id,
            description,
            triggered_by_user_id,
            created_at: Utc::now(),
        }
    }

    /// Clamp reputation score within valid range
    pub fn clamp_score(score: Decimal) -> Decimal {
        if score < Self::MIN_REPUTATION {
            Self::MIN_REPUTATION
        } else if score > Self::MAX_REPUTATION {
            Self::MAX_REPUTATION
        } else {
            score
        }
    }

    /// Calculate reputation change for a commitment based on verification result
    pub fn calculate_commitment_impact(
        commitment: &OutputCommitment,
        result: VerificationResult,
    ) -> (Decimal, ReputationEventType) {
        match result {
            VerificationResult::Approved => (
                commitment.reputation_impact,
                ReputationEventType::CommitmentFulfilled,
            ),
            VerificationResult::Partial => (
                commitment.reputation_impact / dec!(2),
                ReputationEventType::CommitmentPartial,
            ),
            VerificationResult::Rejected => (
                -commitment.reputation_impact,
                ReputationEventType::CommitmentFailed,
            ),
        }
    }

    /// Calculate time-based reputation decay (optional feature)
    /// Returns negative change if user has been inactive
    #[allow(dead_code)]
    pub fn calculate_time_decay(days_since_last_activity: i64, current_score: Decimal) -> Decimal {
        // Only decay if score is above base and user has been inactive for 90+ days
        if days_since_last_activity < 90 || current_score <= Self::BASE_REPUTATION {
            return dec!(0);
        }

        // Decay 1 point per 30 days of inactivity (up to max of 10 points)
        let decay_amount = Decimal::from(days_since_last_activity / 30).min(dec!(10));
        -decay_amount
    }

    /// Get reputation tier based on score
    pub fn get_reputation_tier(score: Decimal) -> ReputationTier {
        if score >= dec!(90) {
            ReputationTier::Excellent
        } else if score >= dec!(75) {
            ReputationTier::VeryGood
        } else if score >= dec!(60) {
            ReputationTier::Good
        } else if score >= dec!(40) {
            ReputationTier::Fair
        } else if score >= dec!(25) {
            ReputationTier::Poor
        } else {
            ReputationTier::VeryPoor
        }
    }

    /// Calculate commitment fulfillment rate for a user
    pub fn calculate_fulfillment_rate(
        total_commitments: i32,
        fulfilled_commitments: i32,
    ) -> Decimal {
        if total_commitments == 0 {
            return dec!(0);
        }
        (Decimal::from(fulfilled_commitments) / Decimal::from(total_commitments)) * dec!(100)
    }
}

/// Reputation tier classification
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum ReputationTier {
    /// Score below 25 — very poor standing
    VeryPoor,
    /// Score 25–39 — poor standing
    Poor,
    /// Score 40–59 — acceptable standing
    Fair,
    /// Score 60–74 — good standing
    Good,
    /// Score 75–89 — very good standing
    VeryGood,
    /// Score 90+ — excellent standing
    Excellent,
}

impl fmt::Display for ReputationTier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReputationTier::VeryPoor => write!(f, "Very Poor"),
            ReputationTier::Poor => write!(f, "Poor"),
            ReputationTier::Fair => write!(f, "Fair"),
            ReputationTier::Good => write!(f, "Good"),
            ReputationTier::VeryGood => write!(f, "Very Good"),
            ReputationTier::Excellent => write!(f, "Excellent"),
        }
    }
}

/// Reputation statistics for a user
#[derive(Debug, Serialize)]
pub struct ReputationStats {
    /// User this stats summary belongs to
    pub user_id: Uuid,
    /// Current reputation score (0–100)
    pub current_score: Decimal,
    /// Current reputation tier
    pub tier: ReputationTier,
    /// Total number of commitments created
    pub total_commitments: i32,
    /// Number of commitments that were fulfilled
    pub fulfilled_commitments: i32,
    /// Number of commitments that failed or were rejected
    pub failed_commitments: i32,
    /// Number of commitments currently pending
    pub pending_commitments: i32,
    /// Fulfillment rate as a percentage (0–100)
    pub fulfillment_rate_percent: Decimal,
    /// Cumulative reputation points gained across all events
    pub total_reputation_gained: Decimal,
    /// Cumulative reputation points lost across all events
    pub total_reputation_lost: Decimal,
}

/// Circuit breaker for reputation-based actions
pub struct ReputationCircuitBreaker;

impl ReputationCircuitBreaker {
    /// Minimum reputation to create a token
    pub const MIN_REPUTATION_CREATE_TOKEN: Decimal = dec!(40);

    /// Minimum reputation to create high-value commitments (impact > 20)
    pub const MIN_REPUTATION_HIGH_COMMITMENT: Decimal = dec!(60);

    /// Maximum pending commitments allowed
    pub const MAX_PENDING_COMMITMENTS: i32 = 10;

    /// Check if user can create a token
    pub fn can_create_token(reputation_score: Decimal) -> bool {
        reputation_score >= Self::MIN_REPUTATION_CREATE_TOKEN
    }

    /// Check if user can create a high-value commitment
    pub fn can_create_high_value_commitment(
        reputation_score: Decimal,
        commitment_impact: Decimal,
    ) -> bool {
        if commitment_impact <= dec!(20) {
            return true; // Low-impact commitments allowed for everyone
        }
        reputation_score >= Self::MIN_REPUTATION_HIGH_COMMITMENT
    }

    /// Check if user can create more commitments
    pub fn can_create_commitment(pending_count: i32, failed_count: i32) -> Result<(), String> {
        if pending_count >= Self::MAX_PENDING_COMMITMENTS {
            return Err(format!(
                "Maximum pending commitments reached ({})",
                Self::MAX_PENDING_COMMITMENTS
            ));
        }

        // Additional check: if user has 3+ recent failures, require some successes first
        if failed_count >= 3 && pending_count > 0 {
            return Err("Please fulfill existing commitments before creating new ones".to_string());
        }

        Ok(())
    }

    /// Get recommended reputation impact based on user's current score
    pub fn recommend_commitment_impact(reputation_score: Decimal) -> Decimal {
        if reputation_score >= dec!(80) {
            dec!(30) // High reputation users can make high-impact commitments
        } else if reputation_score >= dec!(60) {
            dec!(20)
        } else if reputation_score >= dec!(40) {
            dec!(10)
        } else {
            dec!(5) // Low reputation users should start small
        }
    }
}

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

    #[test]
    fn test_commitment_is_overdue() {
        let mut commitment = OutputCommitment {
            commitment_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            title: "Test".to_string(),
            description: "Test commitment".to_string(),
            deadline: Utc::now() - chrono::Duration::days(1),
            status: CommitmentStatus::Active,
            evidence: None,
            evidence_submitted_at: None,
            verification_result: None,
            verified_by_user_id: None,
            verified_at: None,
            verification_notes: None,
            reputation_impact: dec!(10),
            category: CommitmentCategory::Development,
            is_public: true,
            milestone_number: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        assert!(commitment.is_overdue());

        commitment.status = CommitmentStatus::Fulfilled;
        assert!(!commitment.is_overdue());
    }

    #[test]
    fn test_reputation_calculator_clamp() {
        assert_eq!(ReputationCalculator::clamp_score(dec!(-10)), dec!(0));
        assert_eq!(ReputationCalculator::clamp_score(dec!(150)), dec!(100));
        assert_eq!(ReputationCalculator::clamp_score(dec!(75)), dec!(75));
    }

    #[test]
    fn test_reputation_tiers() {
        assert_eq!(
            ReputationCalculator::get_reputation_tier(dec!(95)),
            ReputationTier::Excellent
        );
        assert_eq!(
            ReputationCalculator::get_reputation_tier(dec!(70)),
            ReputationTier::Good
        );
        assert_eq!(
            ReputationCalculator::get_reputation_tier(dec!(20)),
            ReputationTier::VeryPoor
        );
    }

    #[test]
    fn test_commitment_impact_calculation() {
        let commitment = OutputCommitment {
            commitment_id: Uuid::new_v4(),
            token_id: Uuid::new_v4(),
            user_id: Uuid::new_v4(),
            title: "Test".to_string(),
            description: "Test commitment".to_string(),
            deadline: Utc::now() + chrono::Duration::days(30),
            status: CommitmentStatus::Active,
            evidence: None,
            evidence_submitted_at: None,
            verification_result: None,
            verified_by_user_id: None,
            verified_at: None,
            verification_notes: None,
            reputation_impact: dec!(20),
            category: CommitmentCategory::Development,
            is_public: true,
            milestone_number: None,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        };

        let (change, event_type) = ReputationCalculator::calculate_commitment_impact(
            &commitment,
            VerificationResult::Approved,
        );
        assert_eq!(change, dec!(20));
        assert_eq!(event_type, ReputationEventType::CommitmentFulfilled);

        let (change, _) = ReputationCalculator::calculate_commitment_impact(
            &commitment,
            VerificationResult::Partial,
        );
        assert_eq!(change, dec!(10));

        let (change, event_type) = ReputationCalculator::calculate_commitment_impact(
            &commitment,
            VerificationResult::Rejected,
        );
        assert_eq!(change, dec!(-20));
        assert_eq!(event_type, ReputationEventType::CommitmentFailed);
    }

    #[test]
    fn test_circuit_breaker() {
        assert!(ReputationCircuitBreaker::can_create_token(dec!(50)));
        assert!(!ReputationCircuitBreaker::can_create_token(dec!(30)));

        assert!(ReputationCircuitBreaker::can_create_high_value_commitment(
            dec!(70),
            dec!(25)
        ));
        assert!(!ReputationCircuitBreaker::can_create_high_value_commitment(
            dec!(50),
            dec!(25)
        ));
        assert!(ReputationCircuitBreaker::can_create_high_value_commitment(
            dec!(50),
            dec!(15)
        ));
    }

    #[test]
    fn test_fulfillment_rate() {
        let rate = ReputationCalculator::calculate_fulfillment_rate(10, 8);
        assert_eq!(rate, dec!(80));

        let rate = ReputationCalculator::calculate_fulfillment_rate(0, 0);
        assert_eq!(rate, dec!(0));
    }

    #[test]
    fn test_create_commitment_validation() {
        let valid_request = CreateCommitmentRequest {
            title: "Valid commitment".to_string(),
            description: "This is a valid commitment description".to_string(),
            deadline: Utc::now() + chrono::Duration::days(30),
            category: CommitmentCategory::Development,
            reputation_impact: dec!(15),
            is_public: true,
            milestone_number: None,
        };
        assert!(valid_request.validate().is_ok());

        let invalid_request = CreateCommitmentRequest {
            title: "".to_string(),
            description: "desc".to_string(),
            deadline: Utc::now() + chrono::Duration::days(30),
            category: CommitmentCategory::Development,
            reputation_impact: dec!(15),
            is_public: true,
            milestone_number: None,
        };
        assert!(invalid_request.validate().is_err());
    }
}