work-tuimer 0.6.0

Simple, keyboard-driven TUI for time-tracking that allows you to quickly add time blocks and automatically group time if same task was done in different sessions
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
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
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
use crate::models::{DayData, TimePoint, WorkRecord};
use crate::timer::{TimerState, TimerStatus};
use anyhow::{Context, Result, anyhow};
use rusqlite::{Connection, OptionalExtension, Transaction, params};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{Duration as StdDuration, SystemTime};
use time::format_description::well_known::Rfc3339;
use time::macros::format_description;
use time::{Date, OffsetDateTime};

const DATABASE_FILE_NAME: &str = "work-tuimer.db";
const LEGACY_RUNNING_TIMER_FILE_NAME: &str = "running_timer.json";
const LEGACY_ACTIVE_TIMER_FILE_NAME: &str = "active_timer.json";
const JSON_MIGRATION_META_KEY: &str = "migration.json_to_sqlite.v1";

#[derive(Clone)]
pub struct Storage {
    repository: SqliteRepository,
}

#[derive(Clone)]
struct SqliteRepository {
    db_path: PathBuf,
}

/// High-level storage manager that provides transactional operations
/// and automatic external modification tracking.
pub struct StorageManager {
    storage: Storage,
    file_modified_times: HashMap<Date, Option<SystemTime>>,
}

pub struct StorageDiagnostics {
    pub database_path: PathBuf,
    pub migration_marker: Option<String>,
    pub days_count: u64,
    pub work_records_count: u64,
    pub active_timer_present: bool,
    pub legacy_day_json_files: u64,
    pub legacy_timer_json_files: u64,
}

impl StorageManager {
    /// Create a new StorageManager
    pub fn new() -> Result<Self> {
        Ok(StorageManager {
            storage: Storage::new()?,
            file_modified_times: HashMap::new(),
        })
    }

    /// Create a new StorageManager with a custom directory (for testing)
    #[doc(hidden)]
    #[allow(dead_code)]
    pub fn new_with_dir(data_dir: PathBuf) -> Result<Self> {
        Ok(StorageManager {
            storage: Storage::new_with_dir(data_dir)?,
            file_modified_times: HashMap::new(),
        })
    }

    /// Load day data with automatic revision tracking
    pub fn load_with_tracking(&mut self, date: Date) -> Result<DayData> {
        let data = self.storage.load(&date)?;
        let modified_time = self.storage.try_get_file_modified_time(&date)?;
        self.file_modified_times.insert(date, modified_time);
        Ok(data)
    }

    /// Check if day data changed externally and reload if needed.
    /// Returns Some(DayData) if data was modified and reloaded, None if no change.
    pub fn check_and_reload(&mut self, date: Date) -> Result<Option<DayData>> {
        let current_modified = self.storage.try_get_file_modified_time(&date)?;

        let is_tracked = self.file_modified_times.contains_key(&date);
        if !is_tracked {
            let data = self.storage.load(&date)?;
            self.file_modified_times.insert(date, current_modified);
            return Ok(Some(data));
        }

        let last_known = self.file_modified_times.get(&date).copied().flatten();
        // We only need inequality detection here. We intentionally do not rely
        // on ordering semantics across processes.
        if current_modified != last_known {
            let data = self.storage.load(&date)?;
            self.file_modified_times.insert(date, current_modified);
            Ok(Some(data))
        } else {
            Ok(None)
        }
    }

    /// Add a new work record (transactional: load -> add -> save -> track)
    #[allow(dead_code)]
    pub fn add_record(&mut self, date: Date, record: WorkRecord) -> Result<()> {
        let mut day_data = self.storage.load(&date)?;
        day_data.add_record(record);
        self.storage.save(&day_data)?;

        let modified_time = self.storage.try_get_file_modified_time(&date)?;
        self.file_modified_times.insert(date, modified_time);

        Ok(())
    }

    /// Update an existing work record (transactional: load -> update -> save -> track)
    #[allow(dead_code)]
    pub fn update_record(&mut self, date: Date, record: WorkRecord) -> Result<()> {
        let mut day_data = self.storage.load(&date)?;
        day_data.add_record(record);
        self.storage.save(&day_data)?;

        let modified_time = self.storage.try_get_file_modified_time(&date)?;
        self.file_modified_times.insert(date, modified_time);

        Ok(())
    }

    /// Remove a work record by ID (transactional: load -> remove -> save -> track)
    /// Returns the removed record if found.
    #[allow(dead_code)]
    pub fn remove_record(&mut self, date: Date, id: u32) -> Result<WorkRecord> {
        let mut day_data = self.storage.load(&date)?;

        let record = day_data
            .work_records
            .remove(&id)
            .context(format!("Record with ID {} not found", id))?;

        self.storage.save(&day_data)?;

        let modified_time = self.storage.try_get_file_modified_time(&date)?;
        self.file_modified_times.insert(date, modified_time);

        Ok(record)
    }

    /// Save day data and update tracking
    pub fn save(&mut self, day_data: &DayData) -> Result<()> {
        self.storage.save(day_data)?;

        let modified_time = self.storage.try_get_file_modified_time(&day_data.date)?;
        self.file_modified_times
            .insert(day_data.date, modified_time);

        Ok(())
    }

    /// Get the last known modification token for a date
    pub fn get_last_modified(&self, date: &Date) -> Option<SystemTime> {
        self.file_modified_times.get(date).copied().flatten()
    }

    #[allow(dead_code)]
    pub fn save_active_timer(&self, timer: &TimerState) -> Result<()> {
        self.storage.save_active_timer(timer)
    }

    pub fn load_active_timer(&self) -> Result<Option<TimerState>> {
        self.storage.load_active_timer()
    }

    #[allow(dead_code)]
    pub fn clear_active_timer(&self) -> Result<()> {
        self.storage.clear_active_timer()
    }

    fn create_timer_manager(&self) -> crate::timer::TimerManager {
        crate::timer::TimerManager::new(self.storage.clone())
    }

    pub fn start_timer(
        &self,
        task_name: String,
        description: Option<String>,
        project: Option<String>,
        customer: Option<String>,
        source_record_id: Option<u32>,
        source_record_date: Option<time::Date>,
    ) -> Result<TimerState> {
        let timer_manager = self.create_timer_manager();
        timer_manager.start(
            task_name,
            description,
            project,
            customer,
            source_record_id,
            source_record_date,
        )
    }

    pub fn stop_timer(&self) -> Result<crate::models::WorkRecord> {
        let timer_manager = self.create_timer_manager();
        timer_manager.stop()
    }

    pub fn pause_timer(&self) -> Result<TimerState> {
        let timer_manager = self.create_timer_manager();
        timer_manager.pause()
    }

    pub fn resume_timer(&self) -> Result<TimerState> {
        let timer_manager = self.create_timer_manager();
        timer_manager.resume()
    }

    #[allow(dead_code)]
    pub fn get_timer_elapsed(&self, timer: &TimerState) -> std::time::Duration {
        let timer_manager = self.create_timer_manager();
        timer_manager.get_elapsed_duration(timer)
    }
}

impl Storage {
    pub fn new() -> Result<Self> {
        let data_dir = Self::get_data_directory()?;
        Self::new_with_dir(data_dir)
    }

    /// Create a new Storage with a custom directory (for testing)
    #[doc(hidden)]
    #[allow(dead_code)]
    pub fn new_with_dir(data_dir: PathBuf) -> Result<Self> {
        fs::create_dir_all(&data_dir).context("Failed to create data directory")?;

        let db_path = data_dir.join(DATABASE_FILE_NAME);
        let repository = SqliteRepository::new(db_path);
        repository.initialize(&data_dir)?;

        Ok(Storage { repository })
    }

    fn get_data_directory() -> Result<PathBuf> {
        if let Some(data_dir) = dirs::data_local_dir() {
            let app_dir = data_dir.join("work-tuimer");
            if fs::create_dir_all(&app_dir).is_ok() {
                return Ok(app_dir);
            }
        }

        let local_data = PathBuf::from("./data");
        if fs::create_dir_all(&local_data).is_ok() {
            return Ok(local_data);
        }

        anyhow::bail!("Failed to create data directory in system location or ./data")
    }

    pub fn load(&self, date: &Date) -> Result<DayData> {
        self.repository.load_day(date)
    }

    pub fn save(&self, day_data: &DayData) -> Result<()> {
        self.repository.save_day(day_data)
    }

    /// Get a synthetic monotonic token derived from day revision state.
    /// Returns None if the day has never been written.
    ///
    /// This is not a wall-clock timestamp; it is used only for equality/
    /// inequality change detection.
    #[allow(dead_code)]
    pub fn get_file_modified_time(&self, date: &Date) -> Option<SystemTime> {
        match self.try_get_file_modified_time(date) {
            Ok(token) => token,
            Err(err) => {
                eprintln!(
                    "Failed to read day revision token for {} from storage: {err:#}",
                    date
                );
                None
            }
        }
    }

    pub fn try_get_file_modified_time(&self, date: &Date) -> Result<Option<SystemTime>> {
        self.repository.day_revision_token(date)
    }

    pub fn save_active_timer(&self, timer: &TimerState) -> Result<()> {
        self.repository.save_active_timer(timer)
    }

    pub fn load_active_timer(&self) -> Result<Option<TimerState>> {
        self.repository.load_active_timer()
    }

    pub fn clear_active_timer(&self) -> Result<()> {
        self.repository.clear_active_timer()
    }

    pub fn diagnostics(&self) -> Result<StorageDiagnostics> {
        self.repository.diagnostics()
    }

    pub fn list_dates_with_records(&self) -> Result<Vec<Date>> {
        self.repository.list_dates_with_records()
    }

    #[cfg(test)]
    fn get_db_path(&self) -> PathBuf {
        self.repository.db_path.clone()
    }
}

impl SqliteRepository {
    fn new(db_path: PathBuf) -> Self {
        Self { db_path }
    }

    fn initialize(&self, data_dir: &Path) -> Result<()> {
        let conn = self.open_connection()?;
        Self::apply_database_pragmas(&conn)?;
        Self::initialize_schema(&conn)?;
        Self::apply_schema_migrations(&conn)?;
        drop(conn);

        self
            .migrate_from_legacy_json_if_needed(data_dir)
            .context("Failed to migrate legacy JSON into SQLite. Fix or remove malformed legacy JSON files and restart")
    }

    fn open_connection(&self) -> Result<Connection> {
        let conn = Connection::open(&self.db_path).context(format!(
            "Failed to open SQLite database at {:?}",
            self.db_path
        ))?;
        conn.execute_batch("PRAGMA foreign_keys = ON;")
            .context("Failed to enable SQLite foreign key support")?;
        Ok(conn)
    }

    fn apply_database_pragmas(conn: &Connection) -> Result<()> {
        conn.execute_batch(
            "
            PRAGMA journal_mode = WAL;
            PRAGMA synchronous = NORMAL;
            ",
        )
        .context("Failed to configure SQLite pragmas")?;
        Ok(())
    }

    fn initialize_schema(conn: &Connection) -> Result<()> {
        conn.execute_batch(
            "
            CREATE TABLE IF NOT EXISTS meta (
                key TEXT PRIMARY KEY,
                value TEXT NOT NULL
            );

            CREATE TABLE IF NOT EXISTS day_meta (
                date TEXT PRIMARY KEY,
                last_id INTEGER NOT NULL DEFAULT 0 CHECK (last_id >= 0),
                revision INTEGER NOT NULL DEFAULT 0 CHECK (revision >= 0)
            );

            CREATE TABLE IF NOT EXISTS work_records (
                date TEXT NOT NULL,
                id INTEGER NOT NULL,
                name TEXT NOT NULL,
                start_minutes INTEGER NOT NULL,
                end_minutes INTEGER NOT NULL,
                total_minutes INTEGER NOT NULL,
                project TEXT NOT NULL DEFAULT '',
                customer TEXT NOT NULL DEFAULT '',
                description TEXT NOT NULL DEFAULT '',
                PRIMARY KEY (date, id),
                FOREIGN KEY (date) REFERENCES day_meta(date) ON DELETE CASCADE
            );

            CREATE INDEX IF NOT EXISTS idx_work_records_date_start
                ON work_records(date, start_minutes);

            CREATE TABLE IF NOT EXISTS active_timer (
                singleton_id INTEGER PRIMARY KEY CHECK (singleton_id = 1),
                id INTEGER,
                task_name TEXT NOT NULL,
                description TEXT,
                project TEXT,
                customer TEXT,
                start_time TEXT NOT NULL,
                end_time TEXT,
                date TEXT NOT NULL,
                status TEXT NOT NULL CHECK (status IN ('running', 'paused', 'stopped')),
                paused_duration_secs INTEGER NOT NULL CHECK (paused_duration_secs >= 0),
                paused_at TEXT,
                created_at TEXT NOT NULL,
                updated_at TEXT NOT NULL,
                source_record_id INTEGER,
                source_record_date TEXT
            );
            ",
        )
        .context("Failed to initialize SQLite schema")?;

        Ok(())
    }

    fn apply_schema_migrations(conn: &Connection) -> Result<()> {
        Self::ensure_column_exists(conn, "work_records", "project", "TEXT NOT NULL DEFAULT ''")?;
        Self::ensure_column_exists(conn, "work_records", "customer", "TEXT NOT NULL DEFAULT ''")?;
        Self::ensure_column_exists(conn, "active_timer", "project", "TEXT")?;
        Self::ensure_column_exists(conn, "active_timer", "customer", "TEXT")?;
        Ok(())
    }

    fn ensure_column_exists(
        conn: &Connection,
        table: &str,
        column: &str,
        definition: &str,
    ) -> Result<()> {
        if Self::has_column(conn, table, column)? {
            return Ok(());
        }

        let alter_sql = format!("ALTER TABLE {table} ADD COLUMN {column} {definition}");
        conn.execute(&alter_sql, [])
            .context(format!("Failed to add column {column} to {table}"))?;
        Ok(())
    }

    fn has_column(conn: &Connection, table: &str, column: &str) -> Result<bool> {
        let pragma_sql = format!("PRAGMA table_info({table})");
        let mut stmt = conn
            .prepare(&pragma_sql)
            .context(format!("Failed to inspect table schema for {table}"))?;
        let mut rows = stmt.query([]).context("Failed to query table_info")?;

        while let Some(row) = rows.next().context("Failed to read table_info row")? {
            let column_name = row.get::<_, String>(1)?;
            if column_name == column {
                return Ok(true);
            }
        }

        Ok(false)
    }

    fn load_day(&self, date: &Date) -> Result<DayData> {
        let conn = self.open_connection()?;
        let date_key = format_date(*date);

        let last_id_opt = conn
            .query_row(
                "SELECT last_id FROM day_meta WHERE date = ?1",
                params![date_key.clone()],
                |row| row.get::<_, i64>(0),
            )
            .optional()
            .context("Failed to query day metadata")?;

        let Some(last_id_raw) = last_id_opt else {
            return Ok(DayData::new(*date));
        };

        let last_id = i64_to_u32(last_id_raw, "day_meta.last_id")?;

        let mut day_data = DayData {
            date: *date,
            last_id,
            work_records: HashMap::new(),
        };

        let mut stmt = conn
            .prepare(
                "
                SELECT id, name, start_minutes, end_minutes, total_minutes, project, customer, description
                FROM work_records
                WHERE date = ?1
                ORDER BY id
                ",
            )
            .context("Failed to prepare work records query")?;

        let mut rows = stmt
            .query(params![date_key])
            .context("Failed to query work records")?;

        while let Some(row) = rows.next().context("Failed to read work record row")? {
            let id = i64_to_u32(row.get::<_, i64>(0)?, "work_records.id")?;
            let name = row.get::<_, String>(1)?;
            let start_minutes = i64_to_u32(row.get::<_, i64>(2)?, "work_records.start_minutes")?;
            let end_minutes = i64_to_u32(row.get::<_, i64>(3)?, "work_records.end_minutes")?;
            let total_minutes = i64_to_u32(row.get::<_, i64>(4)?, "work_records.total_minutes")?;
            let project = row.get::<_, Option<String>>(5)?.unwrap_or_default();
            let customer = row.get::<_, Option<String>>(6)?.unwrap_or_default();
            let description = row.get::<_, Option<String>>(7)?.unwrap_or_default();

            let start = TimePoint::from_minutes_since_midnight(start_minutes)
                .map_err(|e| anyhow!(e))
                .context("Invalid start_minutes value in database")?;
            let end = TimePoint::from_minutes_since_midnight(end_minutes)
                .map_err(|e| anyhow!(e))
                .context("Invalid end_minutes value in database")?;

            let record = WorkRecord {
                id,
                name,
                start,
                end,
                total_minutes,
                project,
                customer,
                description,
            };

            day_data.work_records.insert(id, record);
        }

        Ok(day_data)
    }

    fn save_day(&self, day_data: &DayData) -> Result<()> {
        let mut conn = self.open_connection()?;
        let tx = conn
            .transaction()
            .context("Failed to begin day save transaction")?;

        Self::write_day_data_tx(&tx, day_data, true)?;

        tx.commit()
            .context("Failed to commit day save transaction")?;
        Ok(())
    }

    fn day_revision_token(&self, date: &Date) -> Result<Option<SystemTime>> {
        let conn = self.open_connection()?;
        let date_key = format_date(*date);

        let revision_opt = conn
            .query_row(
                "SELECT revision FROM day_meta WHERE date = ?1",
                params![date_key],
                |row| row.get::<_, i64>(0),
            )
            .optional()
            .context("Failed to query day revision")?;

        match revision_opt {
            None => Ok(None),
            Some(revision) => {
                if revision < 0 {
                    anyhow::bail!("Invalid negative revision in database: {}", revision);
                }
                // Convert revision counter into a synthetic token. We only use
                // this value as a monotonic change marker, not as a real time.
                let token = SystemTime::UNIX_EPOCH
                    .checked_add(StdDuration::from_secs(revision as u64))
                    .context("Failed to convert revision to SystemTime")?;
                Ok(Some(token))
            }
        }
    }

    fn save_active_timer(&self, timer: &TimerState) -> Result<()> {
        let mut conn = self.open_connection()?;
        let tx = conn
            .transaction()
            .context("Failed to begin active timer transaction")?;

        Self::save_active_timer_tx(&tx, timer)?;
        tx.commit()
            .context("Failed to commit active timer transaction")?;

        Ok(())
    }

    fn load_active_timer(&self) -> Result<Option<TimerState>> {
        let conn = self.open_connection()?;

        let timer_row = conn
            .query_row(
                "
                SELECT
                    id,
                    task_name,
                    description,
                    project,
                    customer,
                    start_time,
                    end_time,
                    date,
                    status,
                    paused_duration_secs,
                    paused_at,
                    created_at,
                    updated_at,
                    source_record_id,
                    source_record_date
                FROM active_timer
                WHERE singleton_id = 1
                ",
                [],
                |row| {
                    Ok((
                        row.get::<_, Option<i64>>(0)?,
                        row.get::<_, String>(1)?,
                        row.get::<_, Option<String>>(2)?,
                        row.get::<_, Option<String>>(3)?,
                        row.get::<_, Option<String>>(4)?,
                        row.get::<_, String>(5)?,
                        row.get::<_, Option<String>>(6)?,
                        row.get::<_, String>(7)?,
                        row.get::<_, String>(8)?,
                        row.get::<_, i64>(9)?,
                        row.get::<_, Option<String>>(10)?,
                        row.get::<_, String>(11)?,
                        row.get::<_, String>(12)?,
                        row.get::<_, Option<i64>>(13)?,
                        row.get::<_, Option<String>>(14)?,
                    ))
                },
            )
            .optional()
            .context("Failed to query active timer")?;

        let Some(row) = timer_row else {
            return Ok(None);
        };

        let id = row
            .0
            .map(|v| i64_to_u32(v, "active_timer.id"))
            .transpose()?;
        let task_name = row.1;
        let description = row.2;
        let project = row.3;
        let customer = row.4;
        let start_time = parse_datetime(&row.5, "active_timer.start_time")?;
        let end_time = row
            .6
            .as_deref()
            .map(|v| parse_datetime(v, "active_timer.end_time"))
            .transpose()?;
        let date = parse_date(&row.7).context("Invalid active_timer.date")?;
        let status = parse_timer_status(&row.8)?;
        let paused_duration_secs = row.9;
        let paused_at = row
            .10
            .as_deref()
            .map(|v| parse_datetime(v, "active_timer.paused_at"))
            .transpose()?;
        let created_at = parse_datetime(&row.11, "active_timer.created_at")?;
        let updated_at = parse_datetime(&row.12, "active_timer.updated_at")?;
        let source_record_id = row
            .13
            .map(|v| i64_to_u32(v, "active_timer.source_record_id"))
            .transpose()?;
        let source_record_date = row
            .14
            .as_deref()
            .map(parse_date)
            .transpose()
            .context("Invalid active_timer.source_record_date")?;

        Ok(Some(TimerState {
            id,
            task_name,
            description,
            project,
            customer,
            start_time,
            end_time,
            date,
            status,
            paused_duration_secs,
            paused_at,
            created_at,
            updated_at,
            source_record_id,
            source_record_date,
        }))
    }

    fn clear_active_timer(&self) -> Result<()> {
        let conn = self.open_connection()?;
        conn.execute("DELETE FROM active_timer WHERE singleton_id = 1", [])
            .context("Failed to clear active timer")?;
        Ok(())
    }

    fn diagnostics(&self) -> Result<StorageDiagnostics> {
        let conn = self.open_connection()?;

        let migration_marker = conn
            .query_row(
                "SELECT value FROM meta WHERE key = ?1",
                params![JSON_MIGRATION_META_KEY],
                |row| row.get::<_, String>(0),
            )
            .optional()
            .context("Failed to query migration marker")?;

        let days_count = i64_to_u64(
            conn.query_row("SELECT COUNT(*) FROM day_meta", [], |row| {
                row.get::<_, i64>(0)
            })
            .context("Failed to count day rows")?,
            "day_meta count",
        )?;

        let work_records_count = i64_to_u64(
            conn.query_row("SELECT COUNT(*) FROM work_records", [], |row| {
                row.get::<_, i64>(0)
            })
            .context("Failed to count work record rows")?,
            "work_records count",
        )?;

        let active_timer_present = conn
            .query_row(
                "SELECT 1 FROM active_timer WHERE singleton_id = 1 LIMIT 1",
                [],
                |_row| Ok(()),
            )
            .optional()
            .context("Failed to check active timer existence")?
            .is_some();

        let database_path = self.db_path.clone();
        let data_dir = database_path
            .parent()
            .context("Database path has no parent directory")?;

        let (legacy_day_json_files, legacy_timer_json_files) =
            Self::count_legacy_json_files(data_dir)?;

        Ok(StorageDiagnostics {
            database_path,
            migration_marker,
            days_count,
            work_records_count,
            active_timer_present,
            legacy_day_json_files,
            legacy_timer_json_files,
        })
    }

    fn list_dates_with_records(&self) -> Result<Vec<Date>> {
        let conn = self.open_connection()?;
        let mut stmt = conn
            .prepare(
                "
                SELECT DISTINCT date
                FROM work_records
                ORDER BY date
                ",
            )
            .context("Failed to prepare query for exportable dates")?;

        let mut rows = stmt
            .query([])
            .context("Failed to query dates with work records")?;
        let mut dates = Vec::new();

        while let Some(row) = rows.next().context("Failed to read date row")? {
            let raw_date = row.get::<_, String>(0)?;
            let date = parse_date(&raw_date)
                .context(format!("Failed to parse date '{}' from storage", raw_date))?;
            dates.push(date);
        }

        Ok(dates)
    }

    fn count_legacy_json_files(data_dir: &Path) -> Result<(u64, u64)> {
        let entries = fs::read_dir(data_dir).context("Failed to read data directory")?;

        let mut day_files_count = 0_u64;
        let mut timer_files_count = 0_u64;

        for entry in entries {
            let entry = entry.context("Failed to read directory entry")?;
            let path = entry.path();

            if !path.is_file() {
                continue;
            }

            let Some(file_name_os) = path.file_name() else {
                continue;
            };
            let Some(file_name) = file_name_os.to_str() else {
                continue;
            };

            if !file_name.ends_with(".json") {
                continue;
            }

            if matches!(
                file_name,
                LEGACY_RUNNING_TIMER_FILE_NAME | LEGACY_ACTIVE_TIMER_FILE_NAME
            ) {
                timer_files_count += 1;
                continue;
            }

            let Some(date_part) = file_name.strip_suffix(".json") else {
                continue;
            };

            if parse_date(date_part).is_ok() {
                day_files_count += 1;
            }
        }

        Ok((day_files_count, timer_files_count))
    }

    fn migrate_from_legacy_json_if_needed(&self, data_dir: &Path) -> Result<()> {
        let mut conn = self.open_connection()?;
        let tx = conn
            .transaction()
            .context("Failed to begin JSON migration transaction")?;

        if Self::get_meta_value_tx(&tx, JSON_MIGRATION_META_KEY)?.is_some() {
            tx.commit()
                .context("Failed to finalize already-completed migration transaction")?;
            return Ok(());
        }

        Self::import_legacy_day_files_tx(&tx, data_dir)?;
        Self::import_legacy_timer_tx(&tx, data_dir)?;
        Self::set_meta_value_tx(&tx, JSON_MIGRATION_META_KEY, "done")?;

        tx.commit().context("Failed to commit JSON migration")?;
        Ok(())
    }

    fn write_day_data_tx(
        tx: &Transaction<'_>,
        day_data: &DayData,
        increment_revision: bool,
    ) -> Result<()> {
        let date_key = format_date(day_data.date);

        tx.execute(
            "
            INSERT INTO day_meta(date, last_id, revision)
            VALUES (?1, 0, 0)
            ON CONFLICT(date) DO NOTHING
            ",
            params![date_key.clone()],
        )
        .context("Failed to upsert day_meta row")?;

        tx.execute(
            "DELETE FROM work_records WHERE date = ?1",
            params![date_key.clone()],
        )
        .context("Failed to clear existing day work records")?;

        {
            let mut stmt = tx
                .prepare(
                    "
                    INSERT INTO work_records (
                        date,
                        id,
                        name,
                        start_minutes,
                        end_minutes,
                        total_minutes,
                        project,
                        customer,
                        description
                    )
                    VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)
                    ",
                )
                .context("Failed to prepare work record insert statement")?;

            for record in day_data.work_records.values() {
                stmt.execute(params![
                    date_key.clone(),
                    i64::from(record.id),
                    &record.name,
                    i64::from(record.start.to_minutes_since_midnight()),
                    i64::from(record.end.to_minutes_since_midnight()),
                    i64::from(record.total_minutes),
                    &record.project,
                    &record.customer,
                    &record.description,
                ])
                .context("Failed to insert work record")?;
            }
        }

        if increment_revision {
            // Revision is an i64 counter. It would require an impractical number
            // of writes to overflow, and serves only as a monotonic change token.
            tx.execute(
                "
                UPDATE day_meta
                SET last_id = ?1,
                    revision = revision + 1
                WHERE date = ?2
                ",
                params![i64::from(day_data.last_id), date_key],
            )
            .context("Failed to update day_meta with incremented revision")?;
        } else {
            // Migration initializes revision to at least 1 so downstream change
            // detection has a stable non-zero token from first persisted state.
            tx.execute(
                "
                UPDATE day_meta
                SET last_id = ?1,
                    revision = CASE WHEN revision = 0 THEN 1 ELSE revision END
                WHERE date = ?2
                ",
                params![i64::from(day_data.last_id), date_key],
            )
            .context("Failed to update day_meta during migration")?;
        }

        Ok(())
    }

    fn save_active_timer_tx(tx: &Transaction<'_>, timer: &TimerState) -> Result<()> {
        let id = timer.id.map(i64::from);
        let start_time = format_datetime(timer.start_time)?;
        let end_time = timer.end_time.map(format_datetime).transpose()?;
        let date = format_date(timer.date);
        let status = timer_status_to_str(timer.status);
        let project = timer.project.as_deref();
        let customer = timer.customer.as_deref();
        let paused_at = timer.paused_at.map(format_datetime).transpose()?;
        let created_at = format_datetime(timer.created_at)?;
        let updated_at = format_datetime(timer.updated_at)?;
        let source_record_id = timer.source_record_id.map(i64::from);
        let source_record_date = timer.source_record_date.map(format_date);

        tx.execute(
            "
            INSERT INTO active_timer (
                singleton_id,
                id,
                task_name,
                description,
                project,
                customer,
                start_time,
                end_time,
                date,
                status,
                paused_duration_secs,
                paused_at,
                created_at,
                updated_at,
                source_record_id,
                source_record_date
            )
            VALUES (
                1,
                ?1,
                ?2,
                ?3,
                ?4,
                ?5,
                ?6,
                ?7,
                ?8,
                ?9,
                ?10,
                ?11,
                ?12,
                ?13,
                ?14,
                ?15
            )
            ON CONFLICT(singleton_id) DO UPDATE SET
                id = excluded.id,
                task_name = excluded.task_name,
                description = excluded.description,
                project = excluded.project,
                customer = excluded.customer,
                start_time = excluded.start_time,
                end_time = excluded.end_time,
                date = excluded.date,
                status = excluded.status,
                paused_duration_secs = excluded.paused_duration_secs,
                paused_at = excluded.paused_at,
                created_at = excluded.created_at,
                updated_at = excluded.updated_at,
                source_record_id = excluded.source_record_id,
                source_record_date = excluded.source_record_date
            ",
            params![
                id,
                &timer.task_name,
                timer.description.as_deref(),
                project,
                customer,
                start_time,
                end_time,
                date,
                status,
                timer.paused_duration_secs,
                paused_at,
                created_at,
                updated_at,
                source_record_id,
                source_record_date,
            ],
        )
        .context("Failed to upsert active timer")?;

        Ok(())
    }

    fn get_meta_value_tx(tx: &Transaction<'_>, key: &str) -> Result<Option<String>> {
        tx.query_row(
            "SELECT value FROM meta WHERE key = ?1",
            params![key],
            |row| row.get::<_, String>(0),
        )
        .optional()
        .context(format!("Failed to query meta key '{}'", key))
    }

    fn set_meta_value_tx(tx: &Transaction<'_>, key: &str, value: &str) -> Result<()> {
        tx.execute(
            "
            INSERT INTO meta(key, value)
            VALUES (?1, ?2)
            ON CONFLICT(key) DO UPDATE SET value = excluded.value
            ",
            params![key, value],
        )
        .context(format!("Failed to upsert meta key '{}'", key))?;
        Ok(())
    }

    fn day_exists_tx(tx: &Transaction<'_>, date_key: &str) -> Result<bool> {
        let exists = tx
            .query_row(
                "SELECT 1 FROM day_meta WHERE date = ?1 LIMIT 1",
                params![date_key],
                |_row| Ok(()),
            )
            .optional()
            .context("Failed to check day existence")?
            .is_some();
        Ok(exists)
    }

    fn import_legacy_day_files_tx(tx: &Transaction<'_>, data_dir: &Path) -> Result<()> {
        let entries = fs::read_dir(data_dir).context("Failed to read data directory")?;
        let mut paths = Vec::new();

        for entry in entries {
            let entry = entry.context("Failed to read directory entry")?;
            paths.push(entry.path());
        }

        paths.sort();

        for path in paths {
            if !path.is_file() {
                continue;
            }

            let Some(file_name_os) = path.file_name() else {
                continue;
            };
            let Some(file_name) = file_name_os.to_str() else {
                continue;
            };

            if !file_name.ends_with(".json") {
                continue;
            }

            if matches!(
                file_name,
                LEGACY_RUNNING_TIMER_FILE_NAME | LEGACY_ACTIVE_TIMER_FILE_NAME
            ) {
                continue;
            }

            let Some(date_part) = file_name.strip_suffix(".json") else {
                continue;
            };

            let parsed_file_date = match parse_date(date_part) {
                Ok(date) => date,
                Err(_) => continue,
            };

            let date_key = format_date(parsed_file_date);
            if Self::day_exists_tx(tx, &date_key)? {
                eprintln!(
                    "Skipping legacy JSON import for {} from {:?}: data already exists in SQLite",
                    date_key, path
                );
                continue;
            }

            let contents = fs::read_to_string(&path)
                .context(format!("Failed to read legacy JSON file: {:?}", path))?;
            let day_data: DayData = serde_json::from_str(&contents)
                .context(format!("Failed to parse legacy day JSON: {:?}", path))?;

            Self::write_day_data_tx(tx, &day_data, false)?;
        }

        Ok(())
    }

    fn import_legacy_timer_tx(tx: &Transaction<'_>, data_dir: &Path) -> Result<()> {
        let timer_exists = tx
            .query_row(
                "SELECT 1 FROM active_timer WHERE singleton_id = 1 LIMIT 1",
                [],
                |_row| Ok(()),
            )
            .optional()
            .context("Failed to check existing active timer")?
            .is_some();

        if timer_exists {
            return Ok(());
        }

        let running_timer_path = data_dir.join(LEGACY_RUNNING_TIMER_FILE_NAME);
        let active_timer_path = data_dir.join(LEGACY_ACTIVE_TIMER_FILE_NAME);

        if running_timer_path.exists() && active_timer_path.exists() {
            eprintln!(
                "Found both legacy timer files ({:?}, {:?}); preferring {:?}",
                running_timer_path, active_timer_path, running_timer_path
            );
        }

        let timer_paths = [running_timer_path, active_timer_path];

        for path in timer_paths {
            if !path.exists() {
                continue;
            }

            let contents = fs::read_to_string(&path)
                .context(format!("Failed to read legacy timer JSON: {:?}", path))?;
            let timer: TimerState = serde_json::from_str(&contents)
                .context(format!("Failed to parse legacy timer JSON: {:?}", path))?;

            Self::save_active_timer_tx(tx, &timer)?;
            break;
        }

        Ok(())
    }
}

fn format_date(date: Date) -> String {
    format!(
        "{:04}-{:02}-{:02}",
        date.year(),
        date.month() as u8,
        date.day()
    )
}

fn parse_date(value: &str) -> Result<Date> {
    Date::parse(value, format_description!("[year]-[month]-[day]"))
        .context(format!("Invalid date: '{}'. Expected YYYY-MM-DD", value))
}

fn format_datetime(value: OffsetDateTime) -> Result<String> {
    value
        .format(&Rfc3339)
        .context("Failed to serialize datetime as RFC3339")
}

fn parse_datetime(value: &str, field: &str) -> Result<OffsetDateTime> {
    OffsetDateTime::parse(value, &Rfc3339).context(format!(
        "Invalid RFC3339 datetime in {}: '{}'",
        field, value
    ))
}

fn timer_status_to_str(status: TimerStatus) -> &'static str {
    match status {
        TimerStatus::Running => "running",
        TimerStatus::Paused => "paused",
        TimerStatus::Stopped => "stopped",
    }
}

fn parse_timer_status(value: &str) -> Result<TimerStatus> {
    match value {
        "running" => Ok(TimerStatus::Running),
        "paused" => Ok(TimerStatus::Paused),
        "stopped" => Ok(TimerStatus::Stopped),
        _ => anyhow::bail!("Invalid timer status in database: {}", value),
    }
}

fn i64_to_u32(value: i64, field_name: &str) -> Result<u32> {
    u32::try_from(value).context(format!(
        "Invalid value in {}: {} (must fit in u32)",
        field_name, value
    ))
}

fn i64_to_u64(value: i64, field_name: &str) -> Result<u64> {
    u64::try_from(value).context(format!(
        "Invalid value in {}: {} (must fit in u64)",
        field_name, value
    ))
}

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

    fn create_test_date() -> Date {
        Date::from_calendar_date(2025, time::Month::November, 6).unwrap()
    }

    fn create_test_record(id: u32, name: &str) -> WorkRecord {
        let start = TimePoint::new(9, 0).unwrap();
        let end = TimePoint::new(17, 0).unwrap();
        WorkRecord::new(id, name.to_string(), start, end)
    }

    fn create_test_timer() -> TimerState {
        let now = OffsetDateTime::now_utc();
        TimerState {
            id: None,
            task_name: "Test Timer".to_string(),
            description: Some("Timer description".to_string()),
            project: Some("Platform".to_string()),
            customer: Some("ACME".to_string()),
            start_time: now,
            end_time: None,
            date: now.date(),
            status: TimerStatus::Running,
            paused_duration_secs: 0,
            paused_at: None,
            created_at: now,
            updated_at: now,
            source_record_id: None,
            source_record_date: None,
        }
    }

    #[test]
    fn test_new_storage_with_temp_dir_initializes_database() {
        let temp_dir = TempDir::new().unwrap();
        let storage = Storage::new_with_dir(temp_dir.path().to_path_buf()).unwrap();

        assert!(temp_dir.path().exists());
        assert!(storage.get_db_path().exists());
    }

    #[test]
    fn test_save_and_load_day_data_round_trip() {
        let temp_dir = TempDir::new().unwrap();
        let storage = Storage::new_with_dir(temp_dir.path().to_path_buf()).unwrap();
        let date = create_test_date();

        let mut day_data = DayData::new(date);
        day_data.add_record(create_test_record(1, "Coding"));
        day_data.add_record(create_test_record(2, "Meeting"));

        storage.save(&day_data).unwrap();
        let loaded = storage.load(&date).unwrap();

        assert_eq!(loaded.date, date);
        assert_eq!(loaded.last_id, 2);
        assert_eq!(loaded.work_records.len(), 2);
        assert_eq!(loaded.work_records.get(&1).unwrap().name, "Coding");
        assert_eq!(loaded.work_records.get(&2).unwrap().name, "Meeting");
    }

    #[test]
    fn test_list_dates_with_records_only_returns_non_empty_days() {
        let temp_dir = TempDir::new().unwrap();
        let storage = Storage::new_with_dir(temp_dir.path().to_path_buf()).unwrap();

        let date1 = Date::from_calendar_date(2025, time::Month::November, 6).unwrap();
        let date2 = Date::from_calendar_date(2025, time::Month::November, 7).unwrap();
        let date3 = Date::from_calendar_date(2025, time::Month::November, 8).unwrap();

        let mut day1 = DayData::new(date1);
        day1.add_record(create_test_record(1, "Coding"));
        storage.save(&day1).unwrap();

        // Saving an empty day should not make it exportable.
        storage.save(&DayData::new(date2)).unwrap();

        let mut day3 = DayData::new(date3);
        day3.add_record(create_test_record(1, "Meeting"));
        storage.save(&day3).unwrap();

        let dates = storage.list_dates_with_records().unwrap();
        assert_eq!(dates, vec![date1, date3]);
    }

    #[test]
    fn test_get_file_modified_time_uses_revision_token() {
        let temp_dir = TempDir::new().unwrap();
        let storage = Storage::new_with_dir(temp_dir.path().to_path_buf()).unwrap();
        let date = create_test_date();

        assert!(storage.get_file_modified_time(&date).is_none());

        let mut day_data = DayData::new(date);
        day_data.add_record(create_test_record(1, "Task 1"));
        storage.save(&day_data).unwrap();
        let first = storage.get_file_modified_time(&date);

        day_data.add_record(create_test_record(2, "Task 2"));
        storage.save(&day_data).unwrap();
        let second = storage.get_file_modified_time(&date);

        assert!(first.is_some());
        assert!(second.is_some());
        assert_ne!(first, second);
    }

    #[test]
    fn test_save_load_and_clear_active_timer() {
        let temp_dir = TempDir::new().unwrap();
        let storage = Storage::new_with_dir(temp_dir.path().to_path_buf()).unwrap();
        let timer = create_test_timer();

        storage.save_active_timer(&timer).unwrap();
        let loaded = storage.load_active_timer().unwrap();
        assert!(loaded.is_some());
        let loaded_timer = loaded.unwrap();
        assert_eq!(loaded_timer.task_name, "Test Timer");
        assert_eq!(loaded_timer.project.as_deref(), Some("Platform"));
        assert_eq!(loaded_timer.customer.as_deref(), Some("ACME"));

        storage.clear_active_timer().unwrap();
        assert!(storage.load_active_timer().unwrap().is_none());
    }

    #[test]
    fn test_storage_manager_check_and_reload_detects_external_changes() {
        let temp_dir = TempDir::new().unwrap();
        let mut manager1 = StorageManager::new_with_dir(temp_dir.path().to_path_buf()).unwrap();
        let mut manager2 = StorageManager::new_with_dir(temp_dir.path().to_path_buf()).unwrap();
        let date = create_test_date();

        manager1.load_with_tracking(date).unwrap();
        manager2
            .add_record(date, create_test_record(1, "External Change"))
            .unwrap();

        let reloaded = manager1.check_and_reload(date).unwrap();
        assert!(reloaded.is_some());
        assert_eq!(reloaded.unwrap().work_records.len(), 1);
    }

    #[test]
    fn test_json_migration_imports_legacy_day_data_and_timer() {
        let temp_dir = TempDir::new().unwrap();
        let data_dir = temp_dir.path();

        let date = create_test_date();
        let mut day_data = DayData::new(date);
        day_data.add_record(create_test_record(1, "Legacy Task"));

        let day_json_path = data_dir.join("2025-11-06.json");
        fs::write(
            &day_json_path,
            serde_json::to_string_pretty(&day_data).unwrap(),
        )
        .unwrap();

        let timer = create_test_timer();
        let timer_json_path = data_dir.join(LEGACY_RUNNING_TIMER_FILE_NAME);
        fs::write(
            &timer_json_path,
            serde_json::to_string_pretty(&timer).unwrap(),
        )
        .unwrap();

        let storage = Storage::new_with_dir(data_dir.to_path_buf()).unwrap();

        let loaded_day = storage.load(&date).unwrap();
        assert_eq!(loaded_day.work_records.len(), 1);
        assert_eq!(loaded_day.work_records.get(&1).unwrap().name, "Legacy Task");

        let loaded_timer = storage.load_active_timer().unwrap();
        assert!(loaded_timer.is_some());
        assert_eq!(loaded_timer.unwrap().task_name, "Test Timer");

        // Legacy files should be preserved as backup.
        assert!(day_json_path.exists());
        assert!(timer_json_path.exists());
    }

    #[test]
    fn test_json_migration_is_idempotent() {
        let temp_dir = TempDir::new().unwrap();
        let data_dir = temp_dir.path();

        let date = create_test_date();
        let mut day_data = DayData::new(date);
        day_data.add_record(create_test_record(1, "Legacy Task"));

        let day_json_path = data_dir.join("2025-11-06.json");
        fs::write(
            &day_json_path,
            serde_json::to_string_pretty(&day_data).unwrap(),
        )
        .unwrap();

        let storage1 = Storage::new_with_dir(data_dir.to_path_buf()).unwrap();
        let loaded1 = storage1.load(&date).unwrap();
        assert_eq!(loaded1.work_records.len(), 1);

        let storage2 = Storage::new_with_dir(data_dir.to_path_buf()).unwrap();
        let loaded2 = storage2.load(&date).unwrap();
        assert_eq!(loaded2.work_records.len(), 1);
        assert_eq!(loaded2.work_records.get(&1).unwrap().name, "Legacy Task");
    }

    #[test]
    fn test_json_migration_fails_on_malformed_legacy_day_file() {
        let temp_dir = TempDir::new().unwrap();
        let data_dir = temp_dir.path();

        let malformed_day_path = data_dir.join("2025-11-08.json");
        fs::write(&malformed_day_path, "{ invalid json").unwrap();

        let result = Storage::new_with_dir(data_dir.to_path_buf());
        assert!(result.is_err());
        let err = result.err().expect("expected malformed migration to fail");
        let err_msg = format!("{err:#}");
        let malformed_path_str = malformed_day_path.display().to_string();
        assert!(
            err_msg.contains(&malformed_path_str),
            "error message '{}' did not include malformed file path '{}'",
            err_msg,
            malformed_path_str
        );

        let db_path = data_dir.join(DATABASE_FILE_NAME);
        let conn = Connection::open(db_path).unwrap();
        let day_rows: i64 = conn
            .query_row("SELECT COUNT(*) FROM day_meta", [], |row| row.get(0))
            .unwrap();
        assert_eq!(day_rows, 0);
    }

    #[test]
    fn test_json_migration_prefers_running_timer_when_both_timer_files_exist() {
        let temp_dir = TempDir::new().unwrap();
        let data_dir = temp_dir.path();

        let mut running_timer = create_test_timer();
        running_timer.task_name = "running timer file".to_string();
        fs::write(
            data_dir.join(LEGACY_RUNNING_TIMER_FILE_NAME),
            serde_json::to_string_pretty(&running_timer).unwrap(),
        )
        .unwrap();

        let mut active_timer = create_test_timer();
        active_timer.task_name = "active timer file".to_string();
        fs::write(
            data_dir.join(LEGACY_ACTIVE_TIMER_FILE_NAME),
            serde_json::to_string_pretty(&active_timer).unwrap(),
        )
        .unwrap();

        let storage = Storage::new_with_dir(data_dir.to_path_buf()).unwrap();
        let loaded = storage.load_active_timer().unwrap().unwrap();

        assert_eq!(loaded.task_name, "running timer file");
    }

    #[test]
    fn test_storage_diagnostics_reports_expected_values() {
        let temp_dir = TempDir::new().unwrap();
        let storage = Storage::new_with_dir(temp_dir.path().to_path_buf()).unwrap();

        let date = create_test_date();
        let mut day_data = DayData::new(date);
        day_data.add_record(create_test_record(1, "Task One"));
        day_data.add_record(create_test_record(2, "Task Two"));
        storage.save(&day_data).unwrap();

        fs::write(
            temp_dir.path().join("2025-11-09.json"),
            serde_json::to_string_pretty(&day_data).unwrap(),
        )
        .unwrap();

        let diagnostics = storage.diagnostics().unwrap();

        assert_eq!(diagnostics.migration_marker.as_deref(), Some("done"));
        assert_eq!(diagnostics.days_count, 1);
        assert_eq!(diagnostics.work_records_count, 2);
        assert!(!diagnostics.active_timer_present);
        assert_eq!(diagnostics.legacy_day_json_files, 1);
        assert_eq!(diagnostics.legacy_timer_json_files, 0);
    }

    #[test]
    fn test_schema_migration_adds_project_and_customer_columns() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join(DATABASE_FILE_NAME);

        {
            let conn = Connection::open(&db_path).unwrap();
            conn.execute_batch(
                "
                CREATE TABLE meta (
                    key TEXT PRIMARY KEY,
                    value TEXT NOT NULL
                );

                CREATE TABLE day_meta (
                    date TEXT PRIMARY KEY,
                    last_id INTEGER NOT NULL DEFAULT 0 CHECK (last_id >= 0),
                    revision INTEGER NOT NULL DEFAULT 0 CHECK (revision >= 0)
                );

                CREATE TABLE work_records (
                    date TEXT NOT NULL,
                    id INTEGER NOT NULL,
                    name TEXT NOT NULL,
                    start_minutes INTEGER NOT NULL,
                    end_minutes INTEGER NOT NULL,
                    total_minutes INTEGER NOT NULL,
                    description TEXT NOT NULL DEFAULT '',
                    PRIMARY KEY (date, id),
                    FOREIGN KEY (date) REFERENCES day_meta(date) ON DELETE CASCADE
                );

                CREATE TABLE active_timer (
                    singleton_id INTEGER PRIMARY KEY CHECK (singleton_id = 1),
                    id INTEGER,
                    task_name TEXT NOT NULL,
                    description TEXT,
                    start_time TEXT NOT NULL,
                    end_time TEXT,
                    date TEXT NOT NULL,
                    status TEXT NOT NULL CHECK (status IN ('running', 'paused', 'stopped')),
                    paused_duration_secs INTEGER NOT NULL CHECK (paused_duration_secs >= 0),
                    paused_at TEXT,
                    created_at TEXT NOT NULL,
                    updated_at TEXT NOT NULL,
                    source_record_id INTEGER,
                    source_record_date TEXT
                );
                ",
            )
            .unwrap();
        }

        let _storage = Storage::new_with_dir(temp_dir.path().to_path_buf()).unwrap();
        let conn = Connection::open(db_path).unwrap();

        let work_records_has_project: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM pragma_table_info('work_records') WHERE name = 'project'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        let work_records_has_customer: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM pragma_table_info('work_records') WHERE name = 'customer'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        let active_timer_has_project: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM pragma_table_info('active_timer') WHERE name = 'project'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        let active_timer_has_customer: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM pragma_table_info('active_timer') WHERE name = 'customer'",
                [],
                |row| row.get(0),
            )
            .unwrap();

        assert_eq!(work_records_has_project, 1);
        assert_eq!(work_records_has_customer, 1);
        assert_eq!(active_timer_has_project, 1);
        assert_eq!(active_timer_has_customer, 1);
    }
}