# V1 Block 24 — Backup & Export Library Package

**Purpose**: Encrypted backup and readable exports.

> **Note for Codex**: Follow tasks exactly; avoid exploratory calls when tasks specify names/files/tests.

## Chunk 24.1 — Encrypted backup implementation
Recommended level: **HIGH**

Status: Complete on 2026-06-26.

### Tasks
1. Within `packages/scheduler_backup`, implement function `Future<File> createEncryptedBackup({required String passphrase})` which copies SQLite file then AES-256 encrypts with `encrypt` package.
2. Implement `Future<void> restoreEncryptedBackup(File backup, String passphrase)`.
3. Store backup in `~/ADHD_Scheduler/backups/yyyymmdd_hhmm.db.enc`.
4. Add CLI script `bin/backup.dart` for manual backups.

### Acceptance criteria
1. Round‑trip backup/restore integration test passes using temp directory.
2. Wrong passphrase throws `BackupDecryptionException`.

### Completed implementation
1. Added package `packages/scheduler_backup` with public entry points `backup.dart` and `scheduler_backup.dart`.
2. Added `createEncryptedBackup({required String passphrase})` with optional test-path overrides.
3. Added `restoreEncryptedBackup(File backup, String passphrase)` with optional target-file override.
4. Added default source path `~/ADHD_Scheduler/scheduler.sqlite` and default backup directory `~/ADHD_Scheduler/backups/`.
5. Added backup naming as `yyyymmdd_hhmm.db.enc`, with collision suffixes when needed.
6. Added authenticated encrypted backup envelope using PBKDF2-HMAC-SHA256 with 200,000 iterations and AES-256-GCM.
7. Added `BackupDecryptionException` and `BackupException`.
8. Added CLI script `bin/backup.dart` for manual encrypted backups.
9. Added temp-directory round-trip and wrong-passphrase tests.

### Verification
1. `dart pub get`: passed.
2. `dart format .` in `packages/scheduler_backup`: passed.
3. `cd packages/scheduler_backup && dart analyze`: passed, no issues found.
4. `cd packages/scheduler_backup && dart test`: passed, 2 tests.
5. `dart analyze`: passed, no issues found.
6. `dart test`: passed after Chunk 24.2, 332 tests.
7. `scripts/test.sh`: not present or not executable.

---

## Chunk 24.2 — Readable export writers
Recommended level: **HIGH**

Status: Complete on 2026-06-26.

### Tasks
1. Implement `JsonExportWriter` and `CsvExportWriter` in `scheduler_export_json` package.
2. Hook into `ExportController` factory mapping.

### Acceptance criteria
1. Export command `bin/export.dart --json` outputs valid JSON verified by jsonDecode.

### Completed implementation
1. Added package `packages/scheduler_export_json` with public entry points `export_json.dart` and `scheduler_export_json.dart`.
2. Added concrete `JsonExportWriter` and `CsvExportWriter` implementations.
3. Added `readableExportWriterRegistry()` that maps `json` and `csv` writer factories for `ExportController`.
4. Added CLI script `bin/export.dart` supporting `--json`, `--csv`, `--owner`, and `--timezone`.
5. Added tests for JSON export, CSV quoting, and `bin/export.dart --json` output validated with `jsonDecode`.
6. Wired export-json tests into the root test wrapper.

### Verification
1. `dart pub get`: passed.
2. `dart format test/scheduler_core_test.dart packages/scheduler_export_json`: passed.
3. `cd packages/scheduler_export_json && dart analyze`: passed, no issues found.
4. `cd packages/scheduler_export_json && dart test`: passed, 3 tests.
5. `dart analyze`: passed, no issues found.
6. `dart test`: passed, 332 tests.
7. `scripts/test.sh`: not present or not executable.

---
