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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
"""
回滚管理器
负责修复前的自动备份、失败时的自动回滚、状态恢复和审计日志
"""

import asyncio
import json
import sqlite3
import shutil
import hashlib
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Any, Tuple
from pathlib import Path
from dataclasses import dataclass, field
from enum import Enum
import logging
import threading
import zipfile
import tarfile

from data_models import SpaceInfo, ErrorInfo, RepairStrategy, RepairHistory
from config import get_config


class BackupType(Enum):
    """备份类型"""
    FILE = "file"
    DIRECTORY = "directory"
    GIT_STATE = "git_state"
    DATABASE = "database"
    CONFIGURATION = "configuration"


class RollbackStatus(Enum):
    """回滚状态"""
    PENDING = "pending"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"
    FAILED = "failed"
    PARTIAL = "partial"


@dataclass
class BackupInfo:
    """备份信息"""
    backup_id: str
    space_id: str
    backup_type: BackupType
    original_path: str
    backup_path: str
    timestamp: datetime
    file_hash: Optional[str] = None
    size_bytes: Optional[int] = None
    description: str = ""
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass
class RollbackInfo:
    """回滚信息"""
    rollback_id: str
    backup_id: str
    space_id: str
    rollback_type: BackupType
    status: RollbackStatus
    timestamp: datetime
    completed_at: Optional[datetime] = None
    error_message: Optional[str] = None
    affected_files: List[str] = field(default_factory=list)
    metadata: Dict[str, Any] = field(default_factory=dict)


class BackupStrategy:
    """备份策略"""
    
    def __init__(self, backup_dir: str = "backups"):
        self.logger = logging.getLogger(__name__)
        self.backup_dir = Path(backup_dir)
        self.backup_dir.mkdir(exist_ok=True)
        
        # 创建子目录
        (self.backup_dir / "files").mkdir(exist_ok=True)
        (self.backup_dir / "git_states").mkdir(exist_ok=True)
        (self.backup_dir / "databases").mkdir(exist_ok=True)
        (self.backup_dir / "configs").mkdir(exist_ok=True)
        
        # 数据库初始化
        self.db_path = self.backup_dir / "rollback.db"
        self._init_database()
    
    def _init_database(self) -> None:
        """初始化数据库"""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            
            # 备份记录表
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS backups (
                    backup_id TEXT PRIMARY KEY,
                    space_id TEXT NOT NULL,
                    backup_type TEXT NOT NULL,
                    original_path TEXT NOT NULL,
                    backup_path TEXT NOT NULL,
                    timestamp TEXT NOT NULL,
                    file_hash TEXT,
                    size_bytes INTEGER,
                    description TEXT,
                    metadata TEXT
                )
            """)
            
            # 回滚记录表
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS rollbacks (
                    rollback_id TEXT PRIMARY KEY,
                    backup_id TEXT NOT NULL,
                    space_id TEXT NOT NULL,
                    rollback_type TEXT NOT NULL,
                    status TEXT NOT NULL,
                    timestamp TEXT NOT NULL,
                    completed_at TEXT,
                    error_message TEXT,
                    affected_files TEXT,
                    metadata TEXT,
                    FOREIGN KEY (backup_id) REFERENCES backups (backup_id)
                )
            """)
            
            # 审计日志表
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS audit_log (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    event_type TEXT NOT NULL,
                    space_id TEXT NOT NULL,
                    timestamp TEXT NOT NULL,
                    actor TEXT,
                    action TEXT NOT NULL,
                    details TEXT,
                    success BOOLEAN
                )
            """)
            
            conn.commit()
    
    async def create_backup(self, space_id: str, target_path: str, 
                          backup_type: BackupType, description: str = "") -> str:
        """创建备份"""
        backup_id = self._generate_backup_id(space_id, backup_type)
        backup_info = None
        
        try:
            if backup_type == BackupType.FILE:
                backup_info = await self._backup_file(space_id, target_path, backup_id, description)
            elif backup_type == BackupType.DIRECTORY:
                backup_info = await self._backup_directory(space_id, target_path, backup_id, description)
            elif backup_type == BackupType.GIT_STATE:
                backup_info = await self._backup_git_state(space_id, backup_id, description)
            elif backup_type == BackupType.DATABASE:
                backup_info = await self._backup_database(space_id, target_path, backup_id, description)
            elif backup_type == BackupType.CONFIGURATION:
                backup_info = await self._backup_configuration(space_id, target_path, backup_id, description)
            
            if backup_info:
                await self._save_backup_info(backup_info)
                await self._log_audit_event("backup_created", space_id, "创建备份", {
                    "backup_id": backup_id,
                    "type": backup_type.value,
                    "target": target_path
                }, True)
                
                self.logger.info(f"备份创建成功: {backup_id}")
                return backup_id
            else:
                raise Exception("备份创建失败")
        
        except Exception as e:
            self.logger.error(f"创建备份失败: {e}")
            await self._log_audit_event("backup_failed", space_id, "创建备份失败", {
                "target": target_path,
                "type": backup_type.value,
                "error": str(e)
            }, False)
            raise
    
    async def _backup_file(self, space_id: str, file_path: str, backup_id: str, description: str) -> BackupInfo:
        """备份单个文件"""
        source_path = Path(file_path)
        if not source_path.exists():
            raise FileNotFoundError(f"源文件不存在: {file_path}")
        
        # 生成备份路径
        backup_subdir = self.backup_dir / "files" / space_id
        backup_subdir.mkdir(exist_ok=True)
        
        backup_filename = f"{backup_id}_{source_path.name}"
        backup_path = backup_subdir / backup_filename
        
        # 复制文件
        shutil.copy2(source_path, backup_path)
        
        # 计算文件哈希和大小
        file_hash = await self._calculate_file_hash(source_path)
        size_bytes = source_path.stat().st_size
        
        return BackupInfo(
            backup_id=backup_id,
            space_id=space_id,
            backup_type=BackupType.FILE,
            original_path=str(source_path.absolute()),
            backup_path=str(backup_path.absolute()),
            timestamp=datetime.now(),
            file_hash=file_hash,
            size_bytes=size_bytes,
            description=description,
            metadata={"original_filename": source_path.name}
        )
    
    async def _backup_directory(self, space_id: str, dir_path: str, backup_id: str, description: str) -> BackupInfo:
        """备份目录"""
        source_path = Path(dir_path)
        if not source_path.exists() or not source_path.is_dir():
            raise FileNotFoundError(f"源目录不存在或不是目录: {dir_path}")
        
        # 生成备份路径
        backup_subdir = self.backup_dir / "files" / space_id
        backup_subdir.mkdir(exist_ok=True)
        
        backup_filename = f"{backup_id}_dir.tar.gz"
        backup_path = backup_subdir / backup_filename
        
        # 创建压缩备份
        with tarfile.open(backup_path, "w:gz") as tar:
            tar.add(source_path, arcname=source_path.name)
        
        # 计算哈希和大小
        file_hash = await self._calculate_file_hash(backup_path)
        size_bytes = backup_path.stat().st_size
        
        return BackupInfo(
            backup_id=backup_id,
            space_id=space_id,
            backup_type=BackupType.DIRECTORY,
            original_path=str(source_path.absolute()),
            backup_path=str(backup_path.absolute()),
            timestamp=datetime.now(),
            file_hash=file_hash,
            size_bytes=size_bytes,
            description=description,
            metadata={"original_dirname": source_path.name}
        )
    
    async def _backup_git_state(self, space_id: str, backup_id: str, description: str) -> BackupInfo:
        """备份 Git 状态"""
        import git
        
        try:
            repo = git.Repo(".")
            
            # 生成备份路径
            backup_subdir = self.backup_dir / "git_states" / space_id
            backup_subdir.mkdir(exist_ok=True)
            
            backup_path = backup_subdir / f"{backup_id}_git_state.json"
            
            # 获取 Git 状态信息
            git_state = {
                "current_branch": repo.active_branch.name,
                "current_commit": repo.head.commit.hexsha,
                "untracked_files": repo.untracked_files,
                "modified_files": [item.a_path for item in repo.index.diff(None)],
                "staged_files": [item.a_path for item in repo.index.diff("HEAD")],
                "remote_urls": {remote.name: remote.url for remote in repo.remotes},
                "timestamp": datetime.now().isoformat()
            }
            
            # 保存到文件
            with open(backup_path, 'w', encoding='utf-8') as f:
                json.dump(git_state, f, indent=2, ensure_ascii=False)
            
            # 计算哈希和大小
            file_hash = await self._calculate_file_hash(backup_path)
            size_bytes = backup_path.stat().st_size
            
            return BackupInfo(
                backup_id=backup_id,
                space_id=space_id,
                backup_type=BackupType.GIT_STATE,
                original_path=".",
                backup_path=str(backup_path.absolute()),
                timestamp=datetime.now(),
                file_hash=file_hash,
                size_bytes=size_bytes,
                description=description,
                metadata=git_state
            )
            
        except Exception as e:
            raise Exception(f"备份 Git 状态失败: {e}")
    
    async def _backup_database(self, space_id: str, db_path: str, backup_id: str, description: str) -> BackupInfo:
        """备份数据库"""
        source_path = Path(db_path)
        if not source_path.exists():
            raise FileNotFoundError(f"数据库文件不存在: {db_path}")
        
        # 生成备份路径
        backup_subdir = self.backup_dir / "databases" / space_id
        backup_subdir.mkdir(exist_ok=True)
        
        backup_filename = f"{backup_id}_db.sqlite"
        backup_path = backup_subdir / backup_filename
        
        # 复制数据库文件
        shutil.copy2(source_path, backup_path)
        
        # 计算哈希和大小
        file_hash = await self._calculate_file_hash(backup_path)
        size_bytes = backup_path.stat().st_size
        
        return BackupInfo(
            backup_id=backup_id,
            space_id=space_id,
            backup_type=BackupType.DATABASE,
            original_path=str(source_path.absolute()),
            backup_path=str(backup_path.absolute()),
            timestamp=datetime.now(),
            file_hash=file_hash,
            size_bytes=size_bytes,
            description=description,
            metadata={"database_type": "sqlite"}
        )
    
    async def _backup_configuration(self, space_id: str, config_path: str, backup_id: str, description: str) -> BackupInfo:
        """备份配置文件"""
        source_path = Path(config_path)
        if not source_path.exists():
            raise FileNotFoundError(f"配置文件不存在: {config_path}")
        
        # 生成备份路径
        backup_subdir = self.backup_dir / "configs" / space_id
        backup_subdir.mkdir(exist_ok=True)
        
        backup_filename = f"{backup_id}_config.json"
        backup_path = backup_subdir / backup_filename
        
        # 如果是 JSON 或 YAML 文件,验证格式后再备份
        if source_path.suffix in ['.json', '.yaml', '.yml']:
            try:
                with open(source_path, 'r', encoding='utf-8') as f:
                    if source_path.suffix == '.json':
                        json.load(f)
                    # YAML 验证可以在这里添加
            except Exception as e:
                raise Exception(f"配置文件格式错误: {e}")
        
        # 复制文件
        shutil.copy2(source_path, backup_path)
        
        # 计算哈希和大小
        file_hash = await self._calculate_file_hash(backup_path)
        size_bytes = backup_path.stat().st_size
        
        return BackupInfo(
            backup_id=backup_id,
            space_id=space_id,
            backup_type=BackupType.CONFIGURATION,
            original_path=str(source_path.absolute()),
            backup_path=str(backup_path.absolute()),
            timestamp=datetime.now(),
            file_hash=file_hash,
            size_bytes=size_bytes,
            description=description,
            metadata={"config_type": source_path.suffix}
        )
    
    async def _calculate_file_hash(self, file_path: Path) -> str:
        """计算文件哈希"""
        hash_sha256 = hashlib.sha256()
        with open(file_path, 'rb') as f:
            for chunk in iter(lambda: f.read(4096), b""):
                hash_sha256.update(chunk)
        return hash_sha256.hexdigest()
    
    def _generate_backup_id(self, space_id: str, backup_type: BackupType) -> str:
        """生成备份 ID"""
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        return f"{space_id}_{backup_type.value}_{timestamp}"
    
    async def _save_backup_info(self, backup_info: BackupInfo) -> None:
        """保存备份信息到数据库"""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                INSERT INTO backups 
                (backup_id, space_id, backup_type, original_path, backup_path, timestamp,
                 file_hash, size_bytes, description, metadata)
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """, (
                backup_info.backup_id,
                backup_info.space_id,
                backup_info.backup_type.value,
                backup_info.original_path,
                backup_info.backup_path,
                backup_info.timestamp.isoformat(),
                backup_info.file_hash,
                backup_info.size_bytes,
                backup_info.description,
                json.dumps(backup_info.metadata)
            ))
            conn.commit()
    
    async def get_backup_info(self, backup_id: str) -> Optional[BackupInfo]:
        """获取备份信息"""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                SELECT backup_id, space_id, backup_type, original_path, backup_path, timestamp,
                       file_hash, size_bytes, description, metadata
                FROM backups WHERE backup_id = ?
            """, (backup_id,))
            
            row = cursor.fetchone()
            if row:
                return BackupInfo(
                    backup_id=row[0],
                    space_id=row[1],
                    backup_type=BackupType(row[2]),
                    original_path=row[3],
                    backup_path=row[4],
                    timestamp=datetime.fromisoformat(row[5]),
                    file_hash=row[6],
                    size_bytes=row[7],
                    description=row[8] or "",
                    metadata=json.loads(row[9]) if row[9] else {}
                )
            return None
    
    async def list_backups(self, space_id: Optional[str] = None, 
                          backup_type: Optional[BackupType] = None,
                          limit: int = 100) -> List[BackupInfo]:
        """列出备份"""
        with sqlite3.connect(self.db_path) as conn:
            cursor = conn.cursor()
            
            query = "SELECT * FROM backups WHERE 1=1"
            params = []
            
            if space_id:
                query += " AND space_id = ?"
                params.append(space_id)
            
            if backup_type:
                query += " AND backup_type = ?"
                params.append(backup_type.value)
            
            query += " ORDER BY timestamp DESC LIMIT ?"
            params.append(limit)
            
            cursor.execute(query, params)
            rows = cursor.fetchall()
            
            backups = []
            for row in rows:
                backups.append(BackupInfo(
                    backup_id=row[0],
                    space_id=row[1],
                    backup_type=BackupType(row[2]),
                    original_path=row[3],
                    backup_path=row[4],
                    timestamp=datetime.fromisoformat(row[5]),
                    file_hash=row[6],
                    size_bytes=row[7],
                    description=row[8] or "",
                    metadata=json.loads(row[9]) if row[9] else {}
                ))
            
            return backups


class StateRecovery:
    """状态恢复"""
    
    def __init__(self, backup_strategy: BackupStrategy):
        self.logger = logging.getLogger(__name__)
        self.backup_strategy = backup_strategy
    
    async def restore_from_backup(self, backup_id: str, target_path: Optional[str] = None) -> bool:
        """从备份恢复"""
        try:
            backup_info = await self.backup_strategy.get_backup_info(backup_id)
            if not backup_info:
                raise FileNotFoundError(f"备份不存在: {backup_id}")
            
            backup_path = Path(backup_info.backup_path)
            if not backup_path.exists():
                raise FileNotFoundError(f"备份文件不存在: {backup_path}")
            
            # 确定恢复目标路径
            if target_path:
                restore_path = Path(target_path)
            else:
                restore_path = Path(backup_info.original_path)
            
            success = False
            
            if backup_info.backup_type == BackupType.FILE:
                success = await self._restore_file(backup_path, restore_path)
            elif backup_info.backup_type == BackupType.DIRECTORY:
                success = await self._restore_directory(backup_path, restore_path)
            elif backup_info.backup_type == BackupType.GIT_STATE:
                success = await self._restore_git_state(backup_info, restore_path)
            elif backup_info.backup_type == BackupType.DATABASE:
                success = await self._restore_file(backup_path, restore_path)
            elif backup_info.backup_type == BackupType.CONFIGURATION:
                success = await self._restore_file(backup_path, restore_path)
            
            if success:
                await self.backup_strategy._log_audit_event(
                    "restore_success", backup_info.space_id, "从备份恢复", {
                        "backup_id": backup_id,
                        "target": str(restore_path)
                    }, True
                )
                self.logger.info(f"恢复成功: {backup_id} -> {restore_path}")
            else:
                await self.backup_strategy._log_audit_event(
                    "restore_failed", backup_info.space_id, "恢复失败", {
                        "backup_id": backup_id,
                        "target": str(restore_path)
                    }, False
                )
                self.logger.error(f"恢复失败: {backup_id}")
            
            return success
            
        except Exception as e:
            self.logger.error(f"恢复异常: {e}")
            return False
    
    async def _restore_file(self, backup_path: Path, restore_path: Path) -> bool:
        """恢复文件"""
        try:
            # 确保目标目录存在
            restore_path.parent.mkdir(parents=True, exist_ok=True)
            
            # 备份当前文件(如果存在)
            if restore_path.exists():
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
                current_backup = restore_path.with_suffix(f".{timestamp}.bak")
                shutil.copy2(restore_path, current_backup)
            
            # 恢复文件
            shutil.copy2(backup_path, restore_path)
            return True
            
        except Exception as e:
            self.logger.error(f"恢复文件失败: {e}")
            return False
    
    async def _restore_directory(self, backup_path: Path, restore_path: Path) -> bool:
        """恢复目录"""
        try:
            # 备份当前目录(如果存在)
            if restore_path.exists():
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
                current_backup = restore_path.with_name(f"{restore_path.name}_{timestamp}_bak")
                shutil.move(str(restore_path), str(current_backup))
            
            # 解压恢复目录
            with tarfile.open(backup_path, "r:gz") as tar:
                tar.extractall(restore_path.parent)
            
            return True
            
        except Exception as e:
            self.logger.error(f"恢复目录失败: {e}")
            return False
    
    async def _restore_git_state(self, backup_info: BackupInfo, repo_path: Path) -> bool:
        """恢复 Git 状态"""
        try:
            import git
            
            git_state = backup_info.metadata
            repo = git.Repo(str(repo_path))
            
            # 恢复到指定提交
            if 'current_commit' in git_state:
                commit = git_state['current_commit']
                repo.git.checkout(commit)
            
            # 清理未跟踪的文件
            if git_state.get('untracked_files'):
                for untracked_file in git_state['untracked_files']:
                    file_path = repo_path / untracked_file
                    if file_path.exists():
                        file_path.unlink()
            
            # 恢复修改的文件
            if git_state.get('modified_files'):
                repo.git.reset('--hard', 'HEAD')
            
            return True
            
        except Exception as e:
            self.logger.error(f"恢复 Git 状态失败: {e}")
            return False


class AuditLogger:
    """审计日志"""
    
    def __init__(self, backup_strategy: BackupStrategy):
        self.backup_strategy = backup_strategy
        self.logger = logging.getLogger(__name__)
    
    async def _log_audit_event(self, event_type: str, space_id: str, action: str, 
                            details: Dict[str, Any], success: bool, actor: str = "system") -> None:
        """记录审计事件"""
        try:
            with sqlite3.connect(self.backup_strategy.db_path) as conn:
                cursor = conn.cursor()
                cursor.execute("""
                    INSERT INTO audit_log 
                    (event_type, space_id, timestamp, actor, action, details, success)
                    VALUES (?, ?, ?, ?, ?, ?, ?)
                """, (
                    event_type,
                    space_id,
                    datetime.now().isoformat(),
                    actor,
                    action,
                    json.dumps(details),
                    success
                ))
                conn.commit()
                
        except Exception as e:
            self.logger.error(f"记录审计日志失败: {e}")
    
    async def get_audit_logs(self, space_id: Optional[str] = None, 
                          event_type: Optional[str] = None,
                          limit: int = 100) -> List[Dict[str, Any]]:
        """获取审计日志"""
        try:
            with sqlite3.connect(self.backup_strategy.db_path) as conn:
                cursor = conn.cursor()
                
                query = "SELECT * FROM audit_log WHERE 1=1"
                params = []
                
                if space_id:
                    query += " AND space_id = ?"
                    params.append(space_id)
                
                if event_type:
                    query += " AND event_type = ?"
                    params.append(event_type)
                
                query += " ORDER BY timestamp DESC LIMIT ?"
                params.append(limit)
                
                cursor.execute(query, params)
                rows = cursor.fetchall()
                
                logs = []
                for row in rows:
                    logs.append({
                        "id": row[0],
                        "event_type": row[1],
                        "space_id": row[2],
                        "timestamp": row[3],
                        "actor": row[4],
                        "action": row[5],
                        "details": json.loads(row[6]) if row[6] else {},
                        "success": bool(row[7])
                    })
                
                return logs
                
        except Exception as e:
            self.logger.error(f"获取审计日志失败: {e}")
            return []
    
    async def generate_audit_report(self, space_id: str, 
                                 start_date: Optional[datetime] = None,
                                 end_date: Optional[datetime] = None) -> Dict[str, Any]:
        """生成审计报告"""
        try:
            with sqlite3.connect(self.backup_strategy.db_path) as conn:
                cursor = conn.cursor()
                
                query = """
                    SELECT event_type, COUNT(*) as count, 
                           SUM(CASE WHEN success = 1 THEN 1 ELSE 0 END) as success_count,
                           SUM(CASE WHEN success = 0 THEN 1 ELSE 0 END) as failure_count
                    FROM audit_log 
                    WHERE space_id = ?
                """
                params = [space_id]
                
                if start_date:
                    query += " AND timestamp >= ?"
                    params.append(start_date.isoformat())
                
                if end_date:
                    query += " AND timestamp <= ?"
                    params.append(end_date.isoformat())
                
                query += " GROUP BY event_type"
                
                cursor.execute(query, params)
                rows = cursor.fetchall()
                
                report = {
                    "space_id": space_id,
                    "period": {
                        "start": start_date.isoformat() if start_date else None,
                        "end": end_date.isoformat() if end_date else None
                    },
                    "summary": {},
                    "total_events": 0,
                    "total_success": 0,
                    "total_failure": 0
                }
                
                for row in rows:
                    event_type, count, success_count, failure_count = row
                    report["summary"][event_type] = {
                        "total": count,
                        "success": success_count,
                        "failure": failure_count,
                        "success_rate": success_count / count if count > 0 else 0
                    }
                    
                    report["total_events"] += count
                    report["total_success"] += success_count
                    report["total_failure"] += failure_count
                
                if report["total_events"] > 0:
                    report["overall_success_rate"] = report["total_success"] / report["total_events"]
                else:
                    report["overall_success_rate"] = 0
                
                return report
                
        except Exception as e:
            self.logger.error(f"生成审计报告失败: {e}")
            return {}


class RollbackManager:
    """回滚管理器主类"""
    
    def __init__(self, backup_dir: str = "backups"):
        self.logger = logging.getLogger(__name__)
        self.backup_strategy = BackupStrategy(backup_dir)
        self.state_recovery = StateRecovery(self.backup_strategy)
        self.audit_logger = AuditLogger(self.backup_strategy)
        
        # 回滚统计
        self.rollback_stats = {
            "total_rollbacks": 0,
            "successful_rollbacks": 0,
            "failed_rollbacks": 0
        }
    
    async def create_backup_set(self, space_id: str, targets: List[Tuple[str, BackupType]], 
                              description: str = "") -> List[str]:
        """创建备份集合"""
        backup_ids = []
        
        try:
            for target_path, backup_type in targets:
                backup_id = await self.backup_strategy.create_backup(
                    space_id, target_path, backup_type, f"{description} - {backup_type.value}"
                )
                backup_ids.append(backup_id)
            
            self.logger.info(f"备份集合创建成功: {space_id} - {len(backup_ids)} 个备份")
            return backup_ids
            
        except Exception as e:
            self.logger.error(f"创建备份集合失败: {e}")
            raise
    
    async def execute_rollback(self, backup_id: str, target_path: Optional[str] = None) -> bool:
        """执行回滚"""
        rollback_id = self._generate_rollback_id()
        
        try:
            # 记录回滚开始
            await self._record_rollback_start(rollback_id, backup_id)
            
            # 执行恢复
            success = await self.state_recovery.restore_from_backup(backup_id, target_path)
            
            # 记录回滚结果
            await self._record_rollback_complete(rollback_id, success)
            
            # 更新统计
            self.rollback_stats["total_rollbacks"] += 1
            if success:
                self.rollback_stats["successful_rollbacks"] += 1
                self.logger.info(f"回滚成功: {backup_id}")
            else:
                self.rollback_stats["failed_rollbacks"] += 1
                self.logger.error(f"回滚失败: {backup_id}")
            
            return success
            
        except Exception as e:
            self.logger.error(f"回滚执行异常: {e}")
            await self._record_rollback_complete(rollback_id, False, str(e))
            self.rollback_stats["total_rollbacks"] += 1
            self.rollback_stats["failed_rollbacks"] += 1
            return False
    
    def _generate_rollback_id(self) -> str:
        """生成回滚 ID"""
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
        return f"rollback_{timestamp}"
    
    async def _record_rollback_start(self, rollback_id: str, backup_id: str) -> None:
        """记录回滚开始"""
        backup_info = await self.backup_strategy.get_backup_info(backup_id)
        if not backup_info:
            raise Exception(f"备份信息不存在: {backup_id}")
        
        with sqlite3.connect(self.backup_strategy.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                INSERT INTO rollbacks 
                (rollback_id, backup_id, space_id, rollback_type, status, timestamp)
                VALUES (?, ?, ?, ?, ?, ?)
            """, (
                rollback_id,
                backup_id,
                backup_info.space_id,
                backup_info.backup_type.value,
                RollbackStatus.PENDING.value,
                datetime.now().isoformat()
            ))
            conn.commit()
    
    async def _record_rollback_complete(self, rollback_id: str, success: bool, 
                                     error_message: Optional[str] = None) -> None:
        """记录回滚完成"""
        status = RollbackStatus.COMPLETED if success else RollbackStatus.FAILED
        
        with sqlite3.connect(self.backup_strategy.db_path) as conn:
            cursor = conn.cursor()
            cursor.execute("""
                UPDATE rollbacks 
                SET status = ?, completed_at = ?, error_message = ?
                WHERE rollback_id = ?
            """, (
                status.value,
                datetime.now().isoformat(),
                error_message,
                rollback_id
            ))
            conn.commit()
    
    async def cleanup_old_backups(self, days: int = 30) -> None:
        """清理旧备份"""
        try:
            cutoff_date = datetime.now() - timedelta(days=days)
            
            # 获取需要清理的备份
            backups = await self.backup_strategy.list_backups()
            old_backups = [
                backup for backup in backups 
                if backup.timestamp < cutoff_date
            ]
            
            for backup in old_backups:
                try:
                    # 删除备份文件
                    backup_path = Path(backup.backup_path)
                    if backup_path.exists():
                        backup_path.unlink()
                        
                        # 删除备份信息文件(如果有)
                        info_file = backup_path.with_suffix('.json')
                        if info_file.exists():
                            info_file.unlink()
                    
                    # 删除数据库记录
                    with sqlite3.connect(self.backup_strategy.db_path) as conn:
                        cursor = conn.cursor()
                        cursor.execute("DELETE FROM backups WHERE backup_id = ?", (backup.backup_id,))
                        cursor.execute("DELETE FROM rollbacks WHERE backup_id = ?", (backup.backup_id,))
                        conn.commit()
                    
                    self.logger.info(f"清理旧备份: {backup.backup_id}")
                    
                except Exception as e:
                    self.logger.error(f"清理备份失败 {backup.backup_id}: {e}")
                    
        except Exception as e:
            self.logger.error(f"清理旧备份异常: {e}")
    
    def get_rollback_stats(self) -> Dict[str, Any]:
        """获取回滚统计"""
        return self.rollback_stats.copy()
    
    async def get_rollback_history(self, space_id: Optional[str] = None, 
                                limit: int = 50) -> List[RollbackInfo]:
        """获取回滚历史"""
        try:
            with sqlite3.connect(self.backup_strategy.db_path) as conn:
                cursor = conn.cursor()
                
                query = "SELECT * FROM rollbacks WHERE 1=1"
                params = []
                
                if space_id:
                    query += " AND space_id = ?"
                    params.append(space_id)
                
                query += " ORDER BY timestamp DESC LIMIT ?"
                params.append(limit)
                
                cursor.execute(query, params)
                rows = cursor.fetchall()
                
                rollbacks = []
                for row in rows:
                    rollbacks.append(RollbackInfo(
                        rollback_id=row[0],
                        backup_id=row[1],
                        space_id=row[2],
                        rollback_type=BackupType(row[3]),
                        rollback_status=RollbackStatus(row[4]),
                        timestamp=datetime.fromisoformat(row[5]),
                        completed_at=datetime.fromisoformat(row[6]) if row[6] else None,
                        error_message=row[7],
                        affected_files=json.loads(row[8]) if row[8] else [],
                        metadata=json.loads(row[9]) if row[9] else {}
                    ))
                
                return rollbacks
                
        except Exception as e:
            self.logger.error(f"获取回滚历史失败: {e}")
            return []


if __name__ == "__main__":
    # 示例用法
    async def main():
        # 创建回滚管理器
        rollback_manager = RollbackManager("test_backups")
        
        # 创建备份
        backup_id = await rollback_manager.backup_strategy.create_backup(
            "test-space", 
            "example.txt", 
            BackupType.FILE, 
            "测试备份"
        )
        print(f"创建备份: {backup_id}")
        
        # 执行回滚
        success = await rollback_manager.execute_rollback(backup_id)
        print(f"回滚结果: {success}")
        
        # 获取统计信息
        stats = rollback_manager.get_rollback_stats()
        print(f"统计信息: {stats}")
        
        # 获取审计日志
        logs = await rollback_manager.audit_logger.get_audit_logs("test-space")
        print(f"审计日志数量: {len(logs)}")
    
    asyncio.run(main())