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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

/**
 * 增强版数据导出/导入工具
 * 支持加密数据的处理
 */

const fs = require('fs').promises
const crypto = require('crypto')
const redis = require('../src/models/redis')
const logger = require('../src/utils/logger')
const readline = require('readline')
const config = require('../config/config')

// 解析命令行参数
const args = process.argv.slice(2)
const command = args[0]
const params = {}

args.slice(1).forEach((arg) => {
  const [key, value] = arg.split('=')
  params[key.replace('--', '')] = value || true
})

// 创建 readline 接口
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

async function askConfirmation(question) {
  return new Promise((resolve) => {
    rl.question(`${question} (yes/no): `, (answer) => {
      resolve(answer.toLowerCase() === 'yes' || answer.toLowerCase() === 'y')
    })
  })
}

// Claude 账户解密函数
function decryptClaudeData(encryptedData) {
  if (!encryptedData || !config.security.encryptionKey) {
    return encryptedData
  }

  try {
    if (encryptedData.includes(':')) {
      const parts = encryptedData.split(':')
      const key = crypto.scryptSync(config.security.encryptionKey, 'salt', 32)
      const iv = Buffer.from(parts[0], 'hex')
      const encrypted = parts[1]

      const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv)
      let decrypted = decipher.update(encrypted, 'hex', 'utf8')
      decrypted += decipher.final('utf8')
      return decrypted
    }
    return encryptedData
  } catch (error) {
    logger.warn(`⚠️  Failed to decrypt data: ${error.message}`)
    return encryptedData
  }
}

// Gemini 账户解密函数
function decryptGeminiData(encryptedData) {
  if (!encryptedData || !config.security.encryptionKey) {
    return encryptedData
  }

  try {
    if (encryptedData.includes(':')) {
      const parts = encryptedData.split(':')
      const key = crypto.scryptSync(config.security.encryptionKey, 'gemini-account-salt', 32)
      const iv = Buffer.from(parts[0], 'hex')
      const encrypted = parts[1]

      const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv)
      let decrypted = decipher.update(encrypted, 'hex', 'utf8')
      decrypted += decipher.final('utf8')
      return decrypted
    }
    return encryptedData
  } catch (error) {
    logger.warn(`⚠️  Failed to decrypt data: ${error.message}`)
    return encryptedData
  }
}

// API Key 哈希函数(与apiKeyService保持一致)
function hashApiKey(apiKey) {
  if (!apiKey || !config.security.encryptionKey) {
    return apiKey
  }

  return crypto
    .createHash('sha256')
    .update(apiKey + config.security.encryptionKey)
    .digest('hex')
}

// 检查是否为明文API Key(通过格式判断,不依赖前缀)
function isPlaintextApiKey(apiKey) {
  if (!apiKey || typeof apiKey !== 'string') {
    return false
  }

  // SHA256哈希值固定为64个十六进制字符,如果是哈希值则返回false
  if (apiKey.length === 64 && /^[a-f0-9]+$/i.test(apiKey)) {
    return false // 已经是哈希值
  }

  // 其他情况都认为是明文API Key(包括sk-ant-、cr_、自定义前缀等)
  return true
}

// 数据加密函数(用于导入)
function encryptClaudeData(data) {
  if (!data || !config.security.encryptionKey) {
    return data
  }

  const key = crypto.scryptSync(config.security.encryptionKey, 'salt', 32)
  const iv = crypto.randomBytes(16)

  const cipher = crypto.createCipheriv('aes-256-cbc', key, iv)
  let encrypted = cipher.update(data, 'utf8', 'hex')
  encrypted += cipher.final('hex')

  return `${iv.toString('hex')}:${encrypted}`
}

function encryptGeminiData(data) {
  if (!data || !config.security.encryptionKey) {
    return data
  }

  const key = crypto.scryptSync(config.security.encryptionKey, 'gemini-account-salt', 32)
  const iv = crypto.randomBytes(16)

  const cipher = crypto.createCipheriv('aes-256-cbc', key, iv)
  let encrypted = cipher.update(data, 'utf8', 'hex')
  encrypted += cipher.final('hex')

  return `${iv.toString('hex')}:${encrypted}`
}

// 导出使用统计数据
async function exportUsageStats(keyId) {
  try {
    const stats = {
      total: {},
      daily: {},
      monthly: {},
      hourly: {},
      models: {}
    }

    // 导出总统计
    const totalKey = `usage:${keyId}`
    const totalData = await redis.client.hgetall(totalKey)
    if (totalData && Object.keys(totalData).length > 0) {
      stats.total = totalData
    }

    // 导出每日统计(最近30天)
    const today = new Date()
    for (let i = 0; i < 30; i++) {
      const date = new Date(today)
      date.setDate(date.getDate() - i)
      const dateStr = date.toISOString().split('T')[0]
      const dailyKey = `usage:daily:${keyId}:${dateStr}`

      const dailyData = await redis.client.hgetall(dailyKey)
      if (dailyData && Object.keys(dailyData).length > 0) {
        stats.daily[dateStr] = dailyData
      }
    }

    // 导出每月统计(最近12个月)
    for (let i = 0; i < 12; i++) {
      const date = new Date(today)
      date.setMonth(date.getMonth() - i)
      const monthStr = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`
      const monthlyKey = `usage:monthly:${keyId}:${monthStr}`

      const monthlyData = await redis.client.hgetall(monthlyKey)
      if (monthlyData && Object.keys(monthlyData).length > 0) {
        stats.monthly[monthStr] = monthlyData
      }
    }

    // 导出小时统计(最近24小时)
    for (let i = 0; i < 24; i++) {
      const date = new Date(today)
      date.setHours(date.getHours() - i)
      const dateStr = date.toISOString().split('T')[0]
      const hour = String(date.getHours()).padStart(2, '0')
      const hourKey = `${dateStr}:${hour}`
      const hourlyKey = `usage:hourly:${keyId}:${hourKey}`

      const hourlyData = await redis.client.hgetall(hourlyKey)
      if (hourlyData && Object.keys(hourlyData).length > 0) {
        stats.hourly[hourKey] = hourlyData
      }
    }

    // 导出模型统计
    // 每日模型统计
    const modelDailyPattern = `usage:${keyId}:model:daily:*`
    const modelDailyKeys = await redis.client.keys(modelDailyPattern)
    for (const key of modelDailyKeys) {
      const match = key.match(/usage:.+:model:daily:(.+):(\d{4}-\d{2}-\d{2})$/)
      if (match) {
        const model = match[1]
        const date = match[2]
        const data = await redis.client.hgetall(key)
        if (data && Object.keys(data).length > 0) {
          if (!stats.models[model]) {
            stats.models[model] = { daily: {}, monthly: {} }
          }
          stats.models[model].daily[date] = data
        }
      }
    }

    // 每月模型统计
    const modelMonthlyPattern = `usage:${keyId}:model:monthly:*`
    const modelMonthlyKeys = await redis.client.keys(modelMonthlyPattern)
    for (const key of modelMonthlyKeys) {
      const match = key.match(/usage:.+:model:monthly:(.+):(\d{4}-\d{2})$/)
      if (match) {
        const model = match[1]
        const month = match[2]
        const data = await redis.client.hgetall(key)
        if (data && Object.keys(data).length > 0) {
          if (!stats.models[model]) {
            stats.models[model] = { daily: {}, monthly: {} }
          }
          stats.models[model].monthly[month] = data
        }
      }
    }

    return stats
  } catch (error) {
    logger.warn(`⚠️  Failed to export usage stats for ${keyId}: ${error.message}`)
    return null
  }
}

// 导入使用统计数据
async function importUsageStats(keyId, stats) {
  try {
    if (!stats) {
      return
    }

    const pipeline = redis.client.pipeline()
    let importCount = 0

    // 导入总统计
    if (stats.total && Object.keys(stats.total).length > 0) {
      for (const [field, value] of Object.entries(stats.total)) {
        pipeline.hset(`usage:${keyId}`, field, value)
      }
      importCount++
    }

    // 导入每日统计
    if (stats.daily) {
      for (const [date, data] of Object.entries(stats.daily)) {
        for (const [field, value] of Object.entries(data)) {
          pipeline.hset(`usage:daily:${keyId}:${date}`, field, value)
        }
        importCount++
      }
    }

    // 导入每月统计
    if (stats.monthly) {
      for (const [month, data] of Object.entries(stats.monthly)) {
        for (const [field, value] of Object.entries(data)) {
          pipeline.hset(`usage:monthly:${keyId}:${month}`, field, value)
        }
        importCount++
      }
    }

    // 导入小时统计
    if (stats.hourly) {
      for (const [hour, data] of Object.entries(stats.hourly)) {
        for (const [field, value] of Object.entries(data)) {
          pipeline.hset(`usage:hourly:${keyId}:${hour}`, field, value)
        }
        importCount++
      }
    }

    // 导入模型统计
    if (stats.models) {
      for (const [model, modelStats] of Object.entries(stats.models)) {
        // 每日模型统计
        if (modelStats.daily) {
          for (const [date, data] of Object.entries(modelStats.daily)) {
            for (const [field, value] of Object.entries(data)) {
              pipeline.hset(`usage:${keyId}:model:daily:${model}:${date}`, field, value)
            }
            importCount++
          }
        }

        // 每月模型统计
        if (modelStats.monthly) {
          for (const [month, data] of Object.entries(modelStats.monthly)) {
            for (const [field, value] of Object.entries(data)) {
              pipeline.hset(`usage:${keyId}:model:monthly:${model}:${month}`, field, value)
            }
            importCount++
          }
        }
      }
    }

    await pipeline.exec()
    logger.info(`  📊 Imported ${importCount} usage stat entries for API Key ${keyId}`)
  } catch (error) {
    logger.warn(`⚠️  Failed to import usage stats for ${keyId}: ${error.message}`)
  }
}

// 数据脱敏函数
function sanitizeData(data, type) {
  const sanitized = { ...data }

  switch (type) {
    case 'apikey':
      if (sanitized.apiKey) {
        sanitized.apiKey = `${sanitized.apiKey.substring(0, 10)}...[REDACTED]`
      }
      break

    case 'claude_account':
      if (sanitized.email) {
        sanitized.email = '[REDACTED]'
      }
      if (sanitized.password) {
        sanitized.password = '[REDACTED]'
      }
      if (sanitized.accessToken) {
        sanitized.accessToken = '[REDACTED]'
      }
      if (sanitized.refreshToken) {
        sanitized.refreshToken = '[REDACTED]'
      }
      if (sanitized.claudeAiOauth) {
        sanitized.claudeAiOauth = '[REDACTED]'
      }
      if (sanitized.proxyPassword) {
        sanitized.proxyPassword = '[REDACTED]'
      }
      break

    case 'gemini_account':
      if (sanitized.geminiOauth) {
        sanitized.geminiOauth = '[REDACTED]'
      }
      if (sanitized.accessToken) {
        sanitized.accessToken = '[REDACTED]'
      }
      if (sanitized.refreshToken) {
        sanitized.refreshToken = '[REDACTED]'
      }
      if (sanitized.proxyPassword) {
        sanitized.proxyPassword = '[REDACTED]'
      }
      break

    case 'admin':
      if (sanitized.password) {
        sanitized.password = '[REDACTED]'
      }
      break
  }

  return sanitized
}

// 导出数据
async function exportData() {
  try {
    const outputFile = params.output || `backup-${new Date().toISOString().split('T')[0]}.json`
    const types = params.types ? params.types.split(',') : ['all']
    const shouldSanitize = params.sanitize === true
    const shouldDecrypt = params.decrypt !== false // 默认解密

    logger.info('🔄 Starting data export...')
    logger.info(`📁 Output file: ${outputFile}`)
    logger.info(`📋 Data types: ${types.join(', ')}`)
    logger.info(`🔒 Sanitize sensitive data: ${shouldSanitize ? 'YES' : 'NO'}`)
    logger.info(`🔓 Decrypt data: ${shouldDecrypt ? 'YES' : 'NO'}`)

    await redis.connect()
    logger.success('✅ Connected to Redis')

    const exportDataObj = {
      metadata: {
        version: '2.0',
        exportDate: new Date().toISOString(),
        sanitized: shouldSanitize,
        decrypted: shouldDecrypt,
        types
      },
      data: {}
    }

    // 导出 API Keys
    if (types.includes('all') || types.includes('apikeys')) {
      logger.info('📤 Exporting API Keys...')
      const keys = await redis.client.keys('apikey:*')
      const apiKeys = []

      for (const key of keys) {
        if (key === 'apikey:hash_map') {
          continue
        }

        const data = await redis.client.hgetall(key)
        if (data && Object.keys(data).length > 0) {
          // 获取该 API Key 的 ID
          const keyId = data.id

          // 导出使用统计数据
          if (keyId && (types.includes('all') || types.includes('stats'))) {
            data.usageStats = await exportUsageStats(keyId)
          }

          apiKeys.push(shouldSanitize ? sanitizeData(data, 'apikey') : data)
        }
      }

      exportDataObj.data.apiKeys = apiKeys
      logger.success(`✅ Exported ${apiKeys.length} API Keys`)
    }

    // 导出 Claude 账户
    if (types.includes('all') || types.includes('accounts')) {
      logger.info('📤 Exporting Claude accounts...')
      const keys = await redis.client.keys('claude:account:*')
      logger.info(`Found ${keys.length} Claude account keys in Redis`)
      const accounts = []

      for (const key of keys) {
        const data = await redis.client.hgetall(key)

        if (data && Object.keys(data).length > 0) {
          // 解密敏感字段
          if (shouldDecrypt && !shouldSanitize) {
            if (data.email) {
              data.email = decryptClaudeData(data.email)
            }
            if (data.password) {
              data.password = decryptClaudeData(data.password)
            }
            if (data.accessToken) {
              data.accessToken = decryptClaudeData(data.accessToken)
            }
            if (data.refreshToken) {
              data.refreshToken = decryptClaudeData(data.refreshToken)
            }
            if (data.claudeAiOauth) {
              const decrypted = decryptClaudeData(data.claudeAiOauth)
              try {
                data.claudeAiOauth = JSON.parse(decrypted)
              } catch (e) {
                data.claudeAiOauth = decrypted
              }
            }
          }

          accounts.push(shouldSanitize ? sanitizeData(data, 'claude_account') : data)
        }
      }

      exportDataObj.data.claudeAccounts = accounts
      logger.success(`✅ Exported ${accounts.length} Claude accounts`)

      // 导出 Gemini 账户
      logger.info('📤 Exporting Gemini accounts...')
      const geminiKeys = await redis.client.keys('gemini_account:*')
      logger.info(`Found ${geminiKeys.length} Gemini account keys in Redis`)
      const geminiAccounts = []

      for (const key of geminiKeys) {
        const data = await redis.client.hgetall(key)

        if (data && Object.keys(data).length > 0) {
          // 解密敏感字段
          if (shouldDecrypt && !shouldSanitize) {
            if (data.geminiOauth) {
              const decrypted = decryptGeminiData(data.geminiOauth)
              try {
                data.geminiOauth = JSON.parse(decrypted)
              } catch (e) {
                data.geminiOauth = decrypted
              }
            }
            if (data.accessToken) {
              data.accessToken = decryptGeminiData(data.accessToken)
            }
            if (data.refreshToken) {
              data.refreshToken = decryptGeminiData(data.refreshToken)
            }
          }

          geminiAccounts.push(shouldSanitize ? sanitizeData(data, 'gemini_account') : data)
        }
      }

      exportDataObj.data.geminiAccounts = geminiAccounts
      logger.success(`✅ Exported ${geminiAccounts.length} Gemini accounts`)
    }

    // 导出管理员
    if (types.includes('all') || types.includes('admins')) {
      logger.info('📤 Exporting admins...')
      const keys = await redis.client.keys('admin:*')
      const admins = []

      for (const key of keys) {
        if (key.includes('admin_username:')) {
          continue
        }

        const data = await redis.client.hgetall(key)
        if (data && Object.keys(data).length > 0) {
          admins.push(shouldSanitize ? sanitizeData(data, 'admin') : data)
        }
      }

      exportDataObj.data.admins = admins
      logger.success(`✅ Exported ${admins.length} admins`)
    }

    // 导出全局模型统计(如果需要)
    if (types.includes('all') || types.includes('stats')) {
      logger.info('📤 Exporting global model statistics...')
      const globalStats = {
        daily: {},
        monthly: {},
        hourly: {}
      }

      // 导出全局每日模型统计
      const globalDailyPattern = 'usage:model:daily:*'
      const globalDailyKeys = await redis.client.keys(globalDailyPattern)
      for (const key of globalDailyKeys) {
        const match = key.match(/usage:model:daily:(.+):(\d{4}-\d{2}-\d{2})$/)
        if (match) {
          const model = match[1]
          const date = match[2]
          const data = await redis.client.hgetall(key)
          if (data && Object.keys(data).length > 0) {
            if (!globalStats.daily[date]) {
              globalStats.daily[date] = {}
            }
            globalStats.daily[date][model] = data
          }
        }
      }

      // 导出全局每月模型统计
      const globalMonthlyPattern = 'usage:model:monthly:*'
      const globalMonthlyKeys = await redis.client.keys(globalMonthlyPattern)
      for (const key of globalMonthlyKeys) {
        const match = key.match(/usage:model:monthly:(.+):(\d{4}-\d{2})$/)
        if (match) {
          const model = match[1]
          const month = match[2]
          const data = await redis.client.hgetall(key)
          if (data && Object.keys(data).length > 0) {
            if (!globalStats.monthly[month]) {
              globalStats.monthly[month] = {}
            }
            globalStats.monthly[month][model] = data
          }
        }
      }

      // 导出全局每小时模型统计
      const globalHourlyPattern = 'usage:model:hourly:*'
      const globalHourlyKeys = await redis.client.keys(globalHourlyPattern)
      for (const key of globalHourlyKeys) {
        const match = key.match(/usage:model:hourly:(.+):(\d{4}-\d{2}-\d{2}:\d{2})$/)
        if (match) {
          const model = match[1]
          const hour = match[2]
          const data = await redis.client.hgetall(key)
          if (data && Object.keys(data).length > 0) {
            if (!globalStats.hourly[hour]) {
              globalStats.hourly[hour] = {}
            }
            globalStats.hourly[hour][model] = data
          }
        }
      }

      exportDataObj.data.globalModelStats = globalStats
      logger.success('✅ Exported global model statistics')
    }

    // 写入文件
    await fs.writeFile(outputFile, JSON.stringify(exportDataObj, null, 2))

    // 显示导出摘要
    console.log(`\n${'='.repeat(60)}`)
    console.log('✅ Export Complete!')
    console.log('='.repeat(60))
    console.log(`Output file: ${outputFile}`)
    console.log(`File size: ${(await fs.stat(outputFile)).size} bytes`)

    if (exportDataObj.data.apiKeys) {
      console.log(`API Keys: ${exportDataObj.data.apiKeys.length}`)
    }
    if (exportDataObj.data.claudeAccounts) {
      console.log(`Claude Accounts: ${exportDataObj.data.claudeAccounts.length}`)
    }
    if (exportDataObj.data.geminiAccounts) {
      console.log(`Gemini Accounts: ${exportDataObj.data.geminiAccounts.length}`)
    }
    if (exportDataObj.data.admins) {
      console.log(`Admins: ${exportDataObj.data.admins.length}`)
    }
    console.log('='.repeat(60))

    if (shouldSanitize) {
      logger.warn('⚠️  Sensitive data has been sanitized in this export.')
    }
    if (shouldDecrypt) {
      logger.info('🔓 Encrypted data has been decrypted for portability.')
    }
  } catch (error) {
    logger.error('💥 Export failed:', error)
    process.exit(1)
  } finally {
    await redis.disconnect()
    rl.close()
  }
}

// 显示帮助信息
function showHelp() {
  console.log(`
Enhanced Data Transfer Tool for Claude Relay Service

This tool handles encrypted data export/import between environments.

Usage:
  node scripts/data-transfer-enhanced.js <command> [options]

Commands:
  export    Export data from Redis to a JSON file
  import    Import data from a JSON file to Redis

Export Options:
  --output=FILE        Output filename (default: backup-YYYY-MM-DD.json)
  --types=TYPE,...     Data types: apikeys,accounts,admins,stats,all (default: all)
                       stats: Include usage statistics with API keys
  --sanitize           Remove sensitive data from export
  --decrypt=false      Keep data encrypted (default: true - decrypt for portability)

Import Options:
  --input=FILE         Input filename (required)
  --force              Overwrite existing data without asking
  --skip-conflicts     Skip conflicting data without asking

Important Notes:
  - The tool automatically handles encryption/decryption during import
  - If importing decrypted data, it will be re-encrypted automatically
  - If importing encrypted data, it will be stored as-is
  - Sanitized exports cannot be properly imported (missing sensitive data)
  - Automatic handling of plaintext API Keys
    * Uses your configured API_KEY_PREFIX from config (sk-, cr_, etc.)
    * Automatically detects plaintext vs hashed API Keys by format
    * Plaintext API Keys are automatically hashed during import
    * Hash mappings are created correctly for plaintext keys
    * Supports custom prefixes and legacy format detection
    * No manual conversion needed - just import your backup file

Examples:
  # Export all data with decryption (for migration)
  node scripts/data-transfer-enhanced.js export

  # Export without decrypting (for backup)
  node scripts/data-transfer-enhanced.js export --decrypt=false

  # Import data (auto-handles encryption and plaintext API keys)
  node scripts/data-transfer-enhanced.js import --input=backup.json

  # Import with force overwrite
  node scripts/data-transfer-enhanced.js import --input=backup.json --force
`)
}

// 导入数据
async function importData() {
  try {
    const inputFile = params.input
    if (!inputFile) {
      logger.error('❌ Please specify input file with --input=filename.json')
      process.exit(1)
    }

    const forceOverwrite = params.force === true
    const skipConflicts = params['skip-conflicts'] === true

    logger.info('🔄 Starting data import...')
    logger.info(`📁 Input file: ${inputFile}`)
    logger.info(
      `⚡ Mode: ${forceOverwrite ? 'FORCE OVERWRITE' : skipConflicts ? 'SKIP CONFLICTS' : 'ASK ON CONFLICT'}`
    )

    // 读取文件
    const fileContent = await fs.readFile(inputFile, 'utf8')
    const importDataObj = JSON.parse(fileContent)

    // 验证文件格式
    if (!importDataObj.metadata || !importDataObj.data) {
      logger.error('❌ Invalid backup file format')
      process.exit(1)
    }

    logger.info(`📅 Backup date: ${importDataObj.metadata.exportDate}`)
    logger.info(`🔒 Sanitized: ${importDataObj.metadata.sanitized ? 'YES' : 'NO'}`)
    logger.info(`🔓 Decrypted: ${importDataObj.metadata.decrypted ? 'YES' : 'NO'}`)

    if (importDataObj.metadata.sanitized) {
      logger.warn('⚠️  This backup contains sanitized data. Sensitive fields will be missing!')
      const proceed = await askConfirmation('Continue with sanitized data?')
      if (!proceed) {
        logger.info('❌ Import cancelled')
        return
      }
    }

    // 显示导入摘要
    console.log(`\n${'='.repeat(60)}`)
    console.log('📋 Import Summary:')
    console.log('='.repeat(60))
    if (importDataObj.data.apiKeys) {
      console.log(`API Keys to import: ${importDataObj.data.apiKeys.length}`)
    }
    if (importDataObj.data.claudeAccounts) {
      console.log(`Claude Accounts to import: ${importDataObj.data.claudeAccounts.length}`)
    }
    if (importDataObj.data.geminiAccounts) {
      console.log(`Gemini Accounts to import: ${importDataObj.data.geminiAccounts.length}`)
    }
    if (importDataObj.data.admins) {
      console.log(`Admins to import: ${importDataObj.data.admins.length}`)
    }
    console.log(`${'='.repeat(60)}\n`)

    // 确认导入
    const confirmed = await askConfirmation('⚠️  Proceed with import?')
    if (!confirmed) {
      logger.info('❌ Import cancelled')
      return
    }

    // 连接 Redis
    await redis.connect()
    logger.success('✅ Connected to Redis')

    const stats = {
      imported: 0,
      skipped: 0,
      errors: 0
    }

    // 导入 API Keys
    if (importDataObj.data.apiKeys) {
      logger.info('\n📥 Importing API Keys...')
      for (const apiKey of importDataObj.data.apiKeys) {
        try {
          const exists = await redis.client.exists(`apikey:${apiKey.id}`)

          if (exists && !forceOverwrite) {
            if (skipConflicts) {
              logger.warn(`⏭️  Skipped existing API Key: ${apiKey.name} (${apiKey.id})`)
              stats.skipped++
              continue
            } else {
              const overwrite = await askConfirmation(
                `API Key "${apiKey.name}" (${apiKey.id}) exists. Overwrite?`
              )
              if (!overwrite) {
                stats.skipped++
                continue
              }
            }
          }

          // 保存使用统计数据以便单独导入
          const { usageStats } = apiKey

          // 从apiKey对象中删除usageStats字段,避免存储到主键中
          const apiKeyData = { ...apiKey }
          delete apiKeyData.usageStats

          // 检查并处理API Key哈希
          let plainTextApiKey = null
          let hashedApiKey = null

          if (apiKeyData.apiKey && isPlaintextApiKey(apiKeyData.apiKey)) {
            // 如果是明文API Key,保存明文并计算哈希
            plainTextApiKey = apiKeyData.apiKey
            hashedApiKey = hashApiKey(plainTextApiKey)
            logger.info(`🔐 Detected plaintext API Key for: ${apiKey.name} (${apiKey.id})`)
          } else if (apiKeyData.apiKey) {
            // 如果已经是哈希值,直接使用
            hashedApiKey = apiKeyData.apiKey
            logger.info(`🔍 Using existing hashed API Key for: ${apiKey.name} (${apiKey.id})`)
          }

          // API Key字段始终存储哈希值
          if (hashedApiKey) {
            apiKeyData.apiKey = hashedApiKey
          }

          // 使用 hset 存储到哈希表
          const pipeline = redis.client.pipeline()
          for (const [field, value] of Object.entries(apiKeyData)) {
            pipeline.hset(`apikey:${apiKey.id}`, field, value)
          }
          await pipeline.exec()

          // 更新哈希映射:hash_map的key必须是哈希值
          if (!importDataObj.metadata.sanitized && hashedApiKey) {
            await redis.client.hset('apikey:hash_map', hashedApiKey, apiKey.id)
            logger.info(
              `📝 Updated hash mapping: ${hashedApiKey.substring(0, 8)}... -> ${apiKey.id}`
            )
          }

          // 导入使用统计数据
          if (usageStats) {
            await importUsageStats(apiKey.id, usageStats)
          }

          logger.success(`✅ Imported API Key: ${apiKey.name} (${apiKey.id})`)
          stats.imported++
        } catch (error) {
          logger.error(`❌ Failed to import API Key ${apiKey.id}:`, error.message)
          stats.errors++
        }
      }
    }

    // 导入 Claude 账户
    if (importDataObj.data.claudeAccounts) {
      logger.info('\n📥 Importing Claude accounts...')
      for (const account of importDataObj.data.claudeAccounts) {
        try {
          const exists = await redis.client.exists(`claude:account:${account.id}`)

          if (exists && !forceOverwrite) {
            if (skipConflicts) {
              logger.warn(`⏭️  Skipped existing Claude account: ${account.name} (${account.id})`)
              stats.skipped++
              continue
            } else {
              const overwrite = await askConfirmation(
                `Claude account "${account.name}" (${account.id}) exists. Overwrite?`
              )
              if (!overwrite) {
                stats.skipped++
                continue
              }
            }
          }

          // 复制账户数据以避免修改原始数据
          const accountData = { ...account }

          // 如果数据已解密且不是脱敏数据,需要重新加密
          if (importDataObj.metadata.decrypted && !importDataObj.metadata.sanitized) {
            logger.info(`🔐 Re-encrypting sensitive data for Claude account: ${account.name}`)

            if (accountData.email) {
              accountData.email = encryptClaudeData(accountData.email)
            }
            if (accountData.password) {
              accountData.password = encryptClaudeData(accountData.password)
            }
            if (accountData.accessToken) {
              accountData.accessToken = encryptClaudeData(accountData.accessToken)
            }
            if (accountData.refreshToken) {
              accountData.refreshToken = encryptClaudeData(accountData.refreshToken)
            }
            if (accountData.claudeAiOauth) {
              // 如果是对象,先序列化再加密
              const oauthStr =
                typeof accountData.claudeAiOauth === 'object'
                  ? JSON.stringify(accountData.claudeAiOauth)
                  : accountData.claudeAiOauth
              accountData.claudeAiOauth = encryptClaudeData(oauthStr)
            }
          }

          // 使用 hset 存储到哈希表
          const pipeline = redis.client.pipeline()
          for (const [field, value] of Object.entries(accountData)) {
            if (field === 'claudeAiOauth' && typeof value === 'object') {
              // 确保对象被序列化
              pipeline.hset(`claude:account:${account.id}`, field, JSON.stringify(value))
            } else {
              pipeline.hset(`claude:account:${account.id}`, field, value)
            }
          }
          await pipeline.exec()

          logger.success(`✅ Imported Claude account: ${account.name} (${account.id})`)
          stats.imported++
        } catch (error) {
          logger.error(`❌ Failed to import Claude account ${account.id}:`, error.message)
          stats.errors++
        }
      }
    }

    // 导入 Gemini 账户
    if (importDataObj.data.geminiAccounts) {
      logger.info('\n📥 Importing Gemini accounts...')
      for (const account of importDataObj.data.geminiAccounts) {
        try {
          const exists = await redis.client.exists(`gemini_account:${account.id}`)

          if (exists && !forceOverwrite) {
            if (skipConflicts) {
              logger.warn(`⏭️  Skipped existing Gemini account: ${account.name} (${account.id})`)
              stats.skipped++
              continue
            } else {
              const overwrite = await askConfirmation(
                `Gemini account "${account.name}" (${account.id}) exists. Overwrite?`
              )
              if (!overwrite) {
                stats.skipped++
                continue
              }
            }
          }

          // 复制账户数据以避免修改原始数据
          const accountData = { ...account }

          // 如果数据已解密且不是脱敏数据,需要重新加密
          if (importDataObj.metadata.decrypted && !importDataObj.metadata.sanitized) {
            logger.info(`🔐 Re-encrypting sensitive data for Gemini account: ${account.name}`)

            if (accountData.geminiOauth) {
              const oauthStr =
                typeof accountData.geminiOauth === 'object'
                  ? JSON.stringify(accountData.geminiOauth)
                  : accountData.geminiOauth
              accountData.geminiOauth = encryptGeminiData(oauthStr)
            }
            if (accountData.accessToken) {
              accountData.accessToken = encryptGeminiData(accountData.accessToken)
            }
            if (accountData.refreshToken) {
              accountData.refreshToken = encryptGeminiData(accountData.refreshToken)
            }
          }

          // 使用 hset 存储到哈希表
          const pipeline = redis.client.pipeline()
          for (const [field, value] of Object.entries(accountData)) {
            pipeline.hset(`gemini_account:${account.id}`, field, value)
          }
          await pipeline.exec()

          logger.success(`✅ Imported Gemini account: ${account.name} (${account.id})`)
          stats.imported++
        } catch (error) {
          logger.error(`❌ Failed to import Gemini account ${account.id}:`, error.message)
          stats.errors++
        }
      }
    }

    // 导入管理员账户
    if (importDataObj.data.admins) {
      logger.info('\n📥 Importing admins...')
      for (const admin of importDataObj.data.admins) {
        try {
          const exists = await redis.client.exists(`admin:${admin.id}`)

          if (exists && !forceOverwrite) {
            if (skipConflicts) {
              logger.warn(`⏭️  Skipped existing admin: ${admin.username} (${admin.id})`)
              stats.skipped++
              continue
            } else {
              const overwrite = await askConfirmation(
                `Admin "${admin.username}" (${admin.id}) exists. Overwrite?`
              )
              if (!overwrite) {
                stats.skipped++
                continue
              }
            }
          }

          // 使用 hset 存储到哈希表
          const pipeline = redis.client.pipeline()
          for (const [field, value] of Object.entries(admin)) {
            pipeline.hset(`admin:${admin.id}`, field, value)
          }
          await pipeline.exec()

          // 更新用户名映射
          await redis.client.set(`admin_username:${admin.username}`, admin.id)

          logger.success(`✅ Imported admin: ${admin.username} (${admin.id})`)
          stats.imported++
        } catch (error) {
          logger.error(`❌ Failed to import admin ${admin.id}:`, error.message)
          stats.errors++
        }
      }
    }

    // 导入全局模型统计
    if (importDataObj.data.globalModelStats) {
      logger.info('\n📥 Importing global model statistics...')
      try {
        const globalStats = importDataObj.data.globalModelStats
        const pipeline = redis.client.pipeline()
        let globalStatCount = 0

        // 导入每日统计
        if (globalStats.daily) {
          for (const [date, models] of Object.entries(globalStats.daily)) {
            for (const [model, data] of Object.entries(models)) {
              for (const [field, value] of Object.entries(data)) {
                pipeline.hset(`usage:model:daily:${model}:${date}`, field, value)
              }
              globalStatCount++
            }
          }
        }

        // 导入每月统计
        if (globalStats.monthly) {
          for (const [month, models] of Object.entries(globalStats.monthly)) {
            for (const [model, data] of Object.entries(models)) {
              for (const [field, value] of Object.entries(data)) {
                pipeline.hset(`usage:model:monthly:${model}:${month}`, field, value)
              }
              globalStatCount++
            }
          }
        }

        // 导入每小时统计
        if (globalStats.hourly) {
          for (const [hour, models] of Object.entries(globalStats.hourly)) {
            for (const [model, data] of Object.entries(models)) {
              for (const [field, value] of Object.entries(data)) {
                pipeline.hset(`usage:model:hourly:${model}:${hour}`, field, value)
              }
              globalStatCount++
            }
          }
        }

        await pipeline.exec()
        logger.success(`✅ Imported ${globalStatCount} global model stat entries`)
        stats.imported += globalStatCount
      } catch (error) {
        logger.error('❌ Failed to import global model stats:', error.message)
        stats.errors++
      }
    }

    // 显示导入结果
    console.log(`\n${'='.repeat(60)}`)
    console.log('✅ Import Complete!')
    console.log('='.repeat(60))
    console.log(`Successfully imported: ${stats.imported}`)
    console.log(`Skipped: ${stats.skipped}`)
    console.log(`Errors: ${stats.errors}`)
    console.log('='.repeat(60))
  } catch (error) {
    logger.error('💥 Import failed:', error)
    process.exit(1)
  } finally {
    await redis.disconnect()
    rl.close()
  }
}

// 主函数
async function main() {
  if (!command || command === '--help' || command === 'help') {
    showHelp()
    process.exit(0)
  }

  switch (command) {
    case 'export':
      await exportData()
      break

    case 'import':
      await importData()
      break

    default:
      logger.error(`❌ Unknown command: ${command}`)
      showHelp()
      process.exit(1)
  }
}

// 运行
main().catch((error) => {
  logger.error('💥 Unexpected error:', error)
  process.exit(1)
})