kaccy-db 0.2.0

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

use rust_decimal::Decimal;
use sqlx::PgPool;
use uuid::Uuid;

use crate::error::Result;

/// Trade data from database
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct TradeRow {
    /// Unique identifier for the trade.
    pub trade_id: Uuid,
    /// User who bought the tokens.
    pub buyer_user_id: Uuid,
    /// User who sold the tokens, if a seller exists.
    pub seller_user_id: Option<Uuid>,
    /// Token that was traded.
    pub token_id: Uuid,
    /// Amount of tokens traded.
    pub amount: Decimal,
    /// Price per token in BTC.
    pub price_btc: Decimal,
    /// Total BTC value of the trade.
    pub total_btc: Decimal,
    /// Platform fee collected in BTC.
    pub platform_fee_btc: Decimal,
    /// Issuer royalty collected in BTC.
    pub issuer_royalty_btc: Decimal,
    /// Timestamp when the trade was executed.
    pub executed_at: chrono::DateTime<chrono::Utc>,
}

/// Trade with additional context
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct TradeWithContextRow {
    /// Unique identifier for the trade.
    pub trade_id: Uuid,
    /// User who bought the tokens.
    pub buyer_user_id: Uuid,
    /// User who sold the tokens, if applicable.
    pub seller_user_id: Option<Uuid>,
    /// Token that was traded.
    pub token_id: Uuid,
    /// Amount of tokens traded.
    pub amount: Decimal,
    /// Price per token in BTC.
    pub price_btc: Decimal,
    /// Total BTC value of the trade.
    pub total_btc: Decimal,
    /// Platform fee in BTC.
    pub platform_fee_btc: Decimal,
    /// Issuer royalty in BTC.
    pub issuer_royalty_btc: Decimal,
    /// Execution timestamp.
    pub executed_at: chrono::DateTime<chrono::Utc>,
    /// Username of the buyer.
    pub buyer_username: String,
    /// Username of the seller, if applicable.
    pub seller_username: Option<String>,
    /// Symbol of the traded token.
    pub token_symbol: String,
}

/// Repository for trade operations
pub struct TradeRepository {
    pool: PgPool,
}

impl TradeRepository {
    /// Create a new trade repository.
    pub fn new(pool: PgPool) -> Self {
        Self { pool }
    }

    /// Create a new trade
    #[allow(clippy::too_many_arguments)]
    pub async fn create(
        &self,
        buyer_user_id: Uuid,
        seller_user_id: Option<Uuid>,
        token_id: Uuid,
        amount: Decimal,
        price_btc: Decimal,
        total_btc: Decimal,
        platform_fee_btc: Decimal,
        issuer_royalty_btc: Decimal,
    ) -> Result<TradeRow> {
        let trade = sqlx::query_as::<_, TradeRow>(
            r#"
            INSERT INTO trades (buyer_user_id, seller_user_id, token_id, amount, price_btc, total_btc, platform_fee_btc, issuer_royalty_btc)
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
            RETURNING *
            "#,
        )
        .bind(buyer_user_id)
        .bind(seller_user_id)
        .bind(token_id)
        .bind(amount)
        .bind(price_btc)
        .bind(total_btc)
        .bind(platform_fee_btc)
        .bind(issuer_royalty_btc)
        .fetch_one(&self.pool)
        .await?;

        Ok(trade)
    }

    /// Get trades for a token
    pub async fn get_token_trades(
        &self,
        token_id: Uuid,
        limit: i64,
        offset: i64,
    ) -> Result<Vec<TradeWithContextRow>> {
        let trades = sqlx::query_as::<_, TradeWithContextRow>(
            r#"
            SELECT
                t.*,
                buyer.username as buyer_username,
                seller.username as seller_username,
                tok.symbol as token_symbol
            FROM trades t
            JOIN users buyer ON t.buyer_user_id = buyer.user_id
            LEFT JOIN users seller ON t.seller_user_id = seller.user_id
            JOIN tokens tok ON t.token_id = tok.token_id
            WHERE t.token_id = $1
            ORDER BY t.executed_at DESC
            LIMIT $2 OFFSET $3
            "#,
        )
        .bind(token_id)
        .bind(limit)
        .bind(offset)
        .fetch_all(&self.pool)
        .await?;

        Ok(trades)
    }

    /// Get trades for a user (optimized with UNION to avoid N+1 query issues)
    ///
    /// Uses UNION ALL to efficiently query both buyer and seller indexes separately,
    /// which is much faster than OR conditions on large tables.
    pub async fn get_user_trades(
        &self,
        user_id: Uuid,
        limit: i64,
        offset: i64,
    ) -> Result<Vec<TradeWithContextRow>> {
        let trades = sqlx::query_as::<_, TradeWithContextRow>(
            r#"
            SELECT
                t.*,
                buyer.username as buyer_username,
                seller.username as seller_username,
                tok.symbol as token_symbol
            FROM (
                SELECT * FROM trades WHERE buyer_user_id = $1
                UNION ALL
                SELECT * FROM trades WHERE seller_user_id = $1
            ) t
            JOIN users buyer ON t.buyer_user_id = buyer.user_id
            LEFT JOIN users seller ON t.seller_user_id = seller.user_id
            JOIN tokens tok ON t.token_id = tok.token_id
            ORDER BY t.executed_at DESC
            LIMIT $2 OFFSET $3
            "#,
        )
        .bind(user_id)
        .bind(limit)
        .bind(offset)
        .fetch_all(&self.pool)
        .await?;

        Ok(trades)
    }

    /// Get trade summary for a token
    pub async fn get_token_summary(&self, token_id: Uuid) -> Result<TradeSummary> {
        let summary = sqlx::query_as::<_, TradeSummary>(
            r#"
            SELECT
                COUNT(*) as total_trades,
                COALESCE(SUM(total_btc), 0) as total_volume_btc,
                COALESCE(AVG(price_btc), 0) as avg_price_btc,
                COALESCE(MAX(price_btc), 0) as high_price_btc,
                COALESCE(MIN(price_btc), 0) as low_price_btc
            FROM trades
            WHERE token_id = $1
            "#,
        )
        .bind(token_id)
        .fetch_one(&self.pool)
        .await?;

        Ok(summary)
    }

    /// Get 24h volume for a token
    pub async fn get_24h_volume(&self, token_id: Uuid) -> Result<Decimal> {
        let (volume,): (Decimal,) = sqlx::query_as(
            r#"
            SELECT COALESCE(SUM(total_btc), 0)
            FROM trades
            WHERE token_id = $1
            AND executed_at > NOW() - INTERVAL '24 hours'
            "#,
        )
        .bind(token_id)
        .fetch_one(&self.pool)
        .await?;

        Ok(volume)
    }

    /// Get volume for a token within a specific time range
    ///
    /// # Arguments
    /// * `token_id` - Token ID
    /// * `hours` - Number of hours to look back (e.g., 24 for 24h, 168 for 7d, 720 for 30d)
    pub async fn get_volume_by_hours(&self, token_id: Uuid, hours: i32) -> Result<Decimal> {
        let (volume,): (Decimal,) = sqlx::query_as(
            r#"
            SELECT COALESCE(SUM(total_btc), 0)
            FROM trades
            WHERE token_id = $1
            AND executed_at > NOW() - INTERVAL '1 hour' * $2
            "#,
        )
        .bind(token_id)
        .bind(hours)
        .fetch_one(&self.pool)
        .await?;

        Ok(volume)
    }

    /// Get trade summary for a token within a specific time range
    ///
    /// # Arguments
    /// * `token_id` - Token ID
    /// * `hours` - Number of hours to look back (e.g., 24 for 24h, 168 for 7d, 720 for 30d)
    pub async fn get_token_summary_by_hours(
        &self,
        token_id: Uuid,
        hours: i32,
    ) -> Result<TradeSummary> {
        let summary = sqlx::query_as::<_, TradeSummary>(
            r#"
            SELECT
                COUNT(*) as total_trades,
                COALESCE(SUM(total_btc), 0) as total_volume_btc,
                COALESCE(AVG(price_btc), 0) as avg_price_btc,
                COALESCE(MAX(price_btc), 0) as high_price_btc,
                COALESCE(MIN(price_btc), 0) as low_price_btc
            FROM trades
            WHERE token_id = $1
            AND executed_at > NOW() - INTERVAL '1 hour' * $2
            "#,
        )
        .bind(token_id)
        .bind(hours)
        .fetch_one(&self.pool)
        .await?;

        Ok(summary)
    }

    /// Get total platform fees
    pub async fn get_total_platform_fees(&self) -> Result<Decimal> {
        let (fees,): (Decimal,) =
            sqlx::query_as(r#"SELECT COALESCE(SUM(platform_fee_btc), 0) FROM trades"#)
                .fetch_one(&self.pool)
                .await?;

        Ok(fees)
    }

    /// Get total platform fees within a specific time range
    ///
    /// # Arguments
    /// * `hours` - Number of hours to look back
    pub async fn get_platform_fees_by_hours(&self, hours: i32) -> Result<Decimal> {
        let (fees,): (Decimal,) = sqlx::query_as(
            r#"
            SELECT COALESCE(SUM(platform_fee_btc), 0)
            FROM trades
            WHERE executed_at > NOW() - INTERVAL '1 hour' * $1
            "#,
        )
        .bind(hours)
        .fetch_one(&self.pool)
        .await?;

        Ok(fees)
    }

    /// Batch create multiple trades (optimized for bulk operations)
    ///
    /// Creates multiple trades in a single transaction.
    /// Useful for migrations, bulk imports, or settlement operations.
    #[allow(clippy::too_many_arguments)]
    pub async fn batch_create(&self, trades: Vec<CreateTradeParams>) -> Result<u64> {
        if trades.is_empty() {
            return Ok(0);
        }

        let mut tx = self.pool.begin().await?;
        let mut count = 0u64;

        for trade in trades {
            let result = sqlx::query(
                r#"
                INSERT INTO trades (buyer_user_id, seller_user_id, token_id, amount, price_btc, total_btc, platform_fee_btc, issuer_royalty_btc)
                VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
                "#,
            )
            .bind(trade.buyer_user_id)
            .bind(trade.seller_user_id)
            .bind(trade.token_id)
            .bind(trade.amount)
            .bind(trade.price_btc)
            .bind(trade.total_btc)
            .bind(trade.platform_fee_btc)
            .bind(trade.issuer_royalty_btc)
            .execute(&mut *tx)
            .await?;

            count += result.rows_affected();
        }

        tx.commit().await?;
        Ok(count)
    }

    /// Get comprehensive trading statistics for a user
    ///
    /// Returns buy/sell breakdown, total volume, and profit/loss indicators.
    pub async fn get_user_trade_stats(&self, user_id: Uuid) -> Result<UserTradeStats> {
        let stats = sqlx::query_as::<_, UserTradeStats>(
            r#"
            SELECT
                COUNT(CASE WHEN buyer_user_id = $1 THEN 1 END) as buy_count,
                COUNT(CASE WHEN seller_user_id = $1 THEN 1 END) as sell_count,
                COALESCE(SUM(CASE WHEN buyer_user_id = $1 THEN total_btc ELSE 0 END), 0) as total_bought_btc,
                COALESCE(SUM(CASE WHEN seller_user_id = $1 THEN total_btc ELSE 0 END), 0) as total_sold_btc,
                COALESCE(SUM(CASE WHEN buyer_user_id = $1 THEN platform_fee_btc ELSE 0 END), 0) as fees_paid_btc
            FROM trades
            WHERE buyer_user_id = $1 OR seller_user_id = $1
            "#,
        )
        .bind(user_id)
        .fetch_one(&self.pool)
        .await?;

        Ok(stats)
    }

    /// Get most active traders by trade count
    ///
    /// Returns users sorted by number of trades (both buy and sell).
    pub async fn get_most_active_traders(&self, limit: i64) -> Result<Vec<TraderActivity>> {
        let traders = sqlx::query_as::<_, TraderActivity>(
            r#"
            SELECT
                user_id,
                trade_count,
                total_volume_btc
            FROM (
                SELECT
                    buyer_user_id as user_id,
                    COUNT(*) as trade_count,
                    COALESCE(SUM(total_btc), 0) as total_volume_btc
                FROM trades
                GROUP BY buyer_user_id
                UNION ALL
                SELECT
                    seller_user_id as user_id,
                    COUNT(*) as trade_count,
                    COALESCE(SUM(total_btc), 0) as total_volume_btc
                FROM trades
                WHERE seller_user_id IS NOT NULL
                GROUP BY seller_user_id
            ) combined
            GROUP BY user_id
            ORDER BY SUM(trade_count) DESC
            LIMIT $1
            "#,
        )
        .bind(limit)
        .fetch_all(&self.pool)
        .await?;

        Ok(traders)
    }

    /// Get most traded tokens by volume
    ///
    /// Returns tokens sorted by total trading volume in BTC.
    pub async fn get_most_traded_tokens(&self, limit: i64) -> Result<Vec<TokenTradingVolume>> {
        let tokens = sqlx::query_as::<_, TokenTradingVolume>(
            r#"
            SELECT
                token_id,
                COUNT(*) as trade_count,
                COALESCE(SUM(total_btc), 0) as total_volume_btc,
                COALESCE(SUM(amount), 0) as total_amount
            FROM trades
            GROUP BY token_id
            ORDER BY total_volume_btc DESC
            LIMIT $1
            "#,
        )
        .bind(limit)
        .fetch_all(&self.pool)
        .await?;

        Ok(tokens)
    }

    /// Get recent trades across the entire platform (for activity feed)
    ///
    /// Returns most recent trades with user and token context.
    pub async fn get_recent_trades(&self, limit: i64) -> Result<Vec<TradeWithContextRow>> {
        let trades = sqlx::query_as::<_, TradeWithContextRow>(
            r#"
            SELECT
                t.*,
                buyer.username as buyer_username,
                seller.username as seller_username,
                tok.symbol as token_symbol
            FROM trades t
            JOIN users buyer ON t.buyer_user_id = buyer.user_id
            LEFT JOIN users seller ON t.seller_user_id = seller.user_id
            JOIN tokens tok ON t.token_id = tok.token_id
            ORDER BY t.executed_at DESC
            LIMIT $1
            "#,
        )
        .bind(limit)
        .fetch_all(&self.pool)
        .await?;

        Ok(trades)
    }

    /// Get daily trading statistics for a time range
    ///
    /// Returns daily aggregated statistics useful for trend analysis.
    pub async fn get_daily_trade_stats(
        &self,
        start: chrono::DateTime<chrono::Utc>,
        end: chrono::DateTime<chrono::Utc>,
    ) -> Result<Vec<DailyTradeStats>> {
        let stats = sqlx::query_as::<_, DailyTradeStats>(
            r#"
            SELECT
                DATE(executed_at) as date,
                COUNT(*) as trade_count,
                COUNT(DISTINCT buyer_user_id) as unique_buyers,
                COUNT(DISTINCT token_id) as unique_tokens,
                COALESCE(SUM(total_btc), 0) as total_volume_btc,
                COALESCE(SUM(platform_fee_btc), 0) as total_fees_btc
            FROM trades
            WHERE executed_at >= $1 AND executed_at <= $2
            GROUP BY DATE(executed_at)
            ORDER BY DATE(executed_at) DESC
            "#,
        )
        .bind(start)
        .bind(end)
        .fetch_all(&self.pool)
        .await?;

        Ok(stats)
    }

    /// Get total issuer royalties paid
    pub async fn get_total_royalties(&self) -> Result<Decimal> {
        let (royalties,): (Decimal,) =
            sqlx::query_as(r#"SELECT COALESCE(SUM(issuer_royalty_btc), 0) FROM trades"#)
                .fetch_one(&self.pool)
                .await?;

        Ok(royalties)
    }

    /// Count trades for a specific token
    pub async fn count_trades_by_token(&self, token_id: Uuid) -> Result<i64> {
        let (count,): (i64,) = sqlx::query_as(r#"SELECT COUNT(*) FROM trades WHERE token_id = $1"#)
            .bind(token_id)
            .fetch_one(&self.pool)
            .await?;

        Ok(count)
    }

    /// Get count of unique traders (buyers or sellers)
    pub async fn get_unique_traders_count(&self) -> Result<i64> {
        let (count,): (i64,) = sqlx::query_as(
            r#"
            SELECT COUNT(DISTINCT user_id) FROM (
                SELECT buyer_user_id as user_id FROM trades
                UNION
                SELECT seller_user_id as user_id FROM trades WHERE seller_user_id IS NOT NULL
            ) unique_users
            "#,
        )
        .fetch_one(&self.pool)
        .await?;

        Ok(count)
    }

    /// Get price history for a token (useful for charting)
    ///
    /// Returns chronological price points with volume.
    pub async fn get_price_history(&self, token_id: Uuid, limit: i64) -> Result<Vec<PricePoint>> {
        let prices = sqlx::query_as::<_, PricePoint>(
            r#"
            SELECT
                executed_at as timestamp,
                price_btc,
                amount,
                total_btc
            FROM trades
            WHERE token_id = $1
            ORDER BY executed_at DESC
            LIMIT $2
            "#,
        )
        .bind(token_id)
        .bind(limit)
        .fetch_all(&self.pool)
        .await?;

        Ok(prices)
    }

    /// Get average trade size for a token
    pub async fn get_average_trade_size(&self, token_id: Uuid) -> Result<Decimal> {
        let avg = sqlx::query_scalar::<_, Option<Decimal>>(
            r#"SELECT COALESCE(AVG(amount), 0) FROM trades WHERE token_id = $1"#,
        )
        .bind(token_id)
        .fetch_one(&self.pool)
        .await?;

        Ok(avg.unwrap_or(Decimal::ZERO))
    }

    /// Get total count of all trades
    pub async fn count_total_trades(&self) -> Result<i64> {
        let (count,): (i64,) = sqlx::query_as(r#"SELECT COUNT(*) FROM trades"#)
            .fetch_one(&self.pool)
            .await?;

        Ok(count)
    }
}

/// Time range for trade queries
#[derive(Debug, Clone, Copy)]
pub enum TimeRange {
    /// Last 24 hours
    Hours24,
    /// Last 7 days
    Days7,
    /// Last 30 days
    Days30,
    /// All time
    AllTime,
}

impl TimeRange {
    /// Convert time range to hours
    pub fn to_hours(&self) -> Option<i32> {
        match self {
            TimeRange::Hours24 => Some(24),
            TimeRange::Days7 => Some(24 * 7),   // 168 hours
            TimeRange::Days30 => Some(24 * 30), // 720 hours
            TimeRange::AllTime => None,
        }
    }
}

/// Trade summary statistics
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct TradeSummary {
    /// Total number of trades.
    pub total_trades: i64,
    /// Cumulative BTC volume traded.
    pub total_volume_btc: Decimal,
    /// Average trade price in BTC.
    pub avg_price_btc: Decimal,
    /// Highest trade price in BTC.
    pub high_price_btc: Decimal,
    /// Lowest trade price in BTC.
    pub low_price_btc: Decimal,
}

/// Parameters for creating a trade (used in batch operations)
#[derive(Debug, Clone)]
pub struct CreateTradeParams {
    /// User who is buying.
    pub buyer_user_id: Uuid,
    /// User who is selling, if applicable.
    pub seller_user_id: Option<Uuid>,
    /// Token being traded.
    pub token_id: Uuid,
    /// Amount of tokens.
    pub amount: Decimal,
    /// Price per token in BTC.
    pub price_btc: Decimal,
    /// Total BTC value.
    pub total_btc: Decimal,
    /// Platform fee in BTC.
    pub platform_fee_btc: Decimal,
    /// Issuer royalty in BTC.
    pub issuer_royalty_btc: Decimal,
}

/// User trading statistics
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct UserTradeStats {
    /// Number of buy-side trades.
    pub buy_count: Option<i64>,
    /// Number of sell-side trades.
    pub sell_count: Option<i64>,
    /// Total BTC spent as buyer.
    pub total_bought_btc: Decimal,
    /// Total BTC received as seller.
    pub total_sold_btc: Decimal,
    /// Total fees paid in BTC.
    pub fees_paid_btc: Decimal,
}

/// Trader activity statistics
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct TraderActivity {
    /// User identifier.
    pub user_id: Uuid,
    /// Total number of trades.
    pub trade_count: Option<i64>,
    /// Total BTC volume traded.
    pub total_volume_btc: Option<Decimal>,
}

/// Token trading volume statistics
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct TokenTradingVolume {
    /// Token identifier.
    pub token_id: Uuid,
    /// Number of trades involving this token.
    pub trade_count: i64,
    /// Total BTC volume traded.
    pub total_volume_btc: Decimal,
    /// Total token amount traded.
    pub total_amount: Decimal,
}

/// Daily trade statistics
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct DailyTradeStats {
    /// Date of the aggregation bucket.
    pub date: chrono::NaiveDate,
    /// Number of trades on this date.
    pub trade_count: i64,
    /// Number of distinct buyers on this date.
    pub unique_buyers: i64,
    /// Number of distinct tokens traded on this date.
    pub unique_tokens: i64,
    /// Total BTC volume on this date.
    pub total_volume_btc: Decimal,
    /// Total fees collected on this date.
    pub total_fees_btc: Decimal,
}

/// Price point for charting
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct PricePoint {
    /// Timestamp of the trade.
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Trade price in BTC.
    pub price_btc: Decimal,
    /// Token amount traded.
    pub amount: Decimal,
    /// Total BTC value.
    pub total_btc: Decimal,
}

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

    #[test]
    fn test_time_range_to_hours() {
        assert_eq!(TimeRange::Hours24.to_hours(), Some(24));
        assert_eq!(TimeRange::Days7.to_hours(), Some(168));
        assert_eq!(TimeRange::Days30.to_hours(), Some(720));
        assert_eq!(TimeRange::AllTime.to_hours(), None);
    }

    #[test]
    fn test_trade_summary_structure() {
        let summary = TradeSummary {
            total_trades: 100,
            total_volume_btc: Decimal::new(500000000, 8),
            avg_price_btc: Decimal::new(100000, 8),
            high_price_btc: Decimal::new(150000, 8),
            low_price_btc: Decimal::new(50000, 8),
        };

        assert_eq!(summary.total_trades, 100);
        assert!(summary.high_price_btc > summary.low_price_btc);
    }

    #[test]
    fn test_create_trade_params_structure() {
        let params = CreateTradeParams {
            buyer_user_id: Uuid::new_v4(),
            seller_user_id: Some(Uuid::new_v4()),
            token_id: Uuid::new_v4(),
            amount: Decimal::new(100, 0),
            price_btc: Decimal::new(50000, 8),
            total_btc: Decimal::new(500000, 8),
            platform_fee_btc: Decimal::new(5000, 8),
            issuer_royalty_btc: Decimal::new(2500, 8),
        };

        assert_eq!(params.amount, Decimal::new(100, 0));
        assert!(params.seller_user_id.is_some());
    }

    #[test]
    fn test_user_trade_stats_structure() {
        let stats = UserTradeStats {
            buy_count: Some(50),
            sell_count: Some(30),
            total_bought_btc: Decimal::new(5000000, 8),
            total_sold_btc: Decimal::new(3000000, 8),
            fees_paid_btc: Decimal::new(50000, 8),
        };

        assert_eq!(stats.buy_count, Some(50));
        assert_eq!(stats.sell_count, Some(30));
    }

    #[test]
    fn test_trader_activity_structure() {
        let activity = TraderActivity {
            user_id: Uuid::new_v4(),
            trade_count: Some(100),
            total_volume_btc: Some(Decimal::new(10000000, 8)),
        };

        assert_eq!(activity.trade_count, Some(100));
    }

    #[test]
    fn test_token_trading_volume_structure() {
        let volume = TokenTradingVolume {
            token_id: Uuid::new_v4(),
            trade_count: 500,
            total_volume_btc: Decimal::new(50000000, 8),
            total_amount: Decimal::new(10000, 0),
        };

        assert_eq!(volume.trade_count, 500);
        assert_eq!(volume.total_amount, Decimal::new(10000, 0));
    }

    #[test]
    fn test_daily_trade_stats_structure() {
        let stats = DailyTradeStats {
            date: chrono::NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            trade_count: 1000,
            unique_buyers: 250,
            unique_tokens: 50,
            total_volume_btc: Decimal::new(100000000, 8),
            total_fees_btc: Decimal::new(1000000, 8),
        };

        assert_eq!(stats.trade_count, 1000);
        assert_eq!(stats.unique_buyers, 250);
        assert_eq!(stats.unique_tokens, 50);
    }

    #[test]
    fn test_price_point_structure() {
        let point = PricePoint {
            timestamp: chrono::Utc::now(),
            price_btc: Decimal::new(50000, 8),
            amount: Decimal::new(100, 0),
            total_btc: Decimal::new(500000, 8),
        };

        assert_eq!(point.price_btc, Decimal::new(50000, 8));
        assert_eq!(point.amount, Decimal::new(100, 0));
    }

    #[test]
    fn test_batch_create_empty_vector() {
        let trades: Vec<CreateTradeParams> = vec![];
        assert_eq!(trades.len(), 0);
    }

    #[test]
    fn test_trade_row_structure() {
        let trade = TradeRow {
            trade_id: Uuid::new_v4(),
            buyer_user_id: Uuid::new_v4(),
            seller_user_id: Some(Uuid::new_v4()),
            token_id: Uuid::new_v4(),
            amount: Decimal::new(100, 0),
            price_btc: Decimal::new(50000, 8),
            total_btc: Decimal::new(500000, 8),
            platform_fee_btc: Decimal::new(5000, 8),
            issuer_royalty_btc: Decimal::new(2500, 8),
            executed_at: chrono::Utc::now(),
        };

        assert_eq!(trade.amount, Decimal::new(100, 0));
        assert!(trade.seller_user_id.is_some());
    }

    #[test]
    fn test_trade_with_context_row_structure() {
        let trade = TradeWithContextRow {
            trade_id: Uuid::new_v4(),
            buyer_user_id: Uuid::new_v4(),
            seller_user_id: Some(Uuid::new_v4()),
            token_id: Uuid::new_v4(),
            amount: Decimal::new(100, 0),
            price_btc: Decimal::new(50000, 8),
            total_btc: Decimal::new(500000, 8),
            platform_fee_btc: Decimal::new(5000, 8),
            issuer_royalty_btc: Decimal::new(2500, 8),
            executed_at: chrono::Utc::now(),
            buyer_username: "alice".to_string(),
            seller_username: Some("bob".to_string()),
            token_symbol: "BTC".to_string(),
        };

        assert_eq!(trade.buyer_username, "alice");
        assert_eq!(trade.seller_username, Some("bob".to_string()));
        assert_eq!(trade.token_symbol, "BTC");
    }
}