rtimelogger 0.5.0

A simple cross-platform CLI tool to track working hours, lunch breaks, and calculate surplus time
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
use chrono::{NaiveTime, Utc};
use rusqlite::{Connection, OptionalExtension, Result, ToSql, params};
use serde::Serialize;
mod migrate;
pub use migrate::run_pending_migrations;

/// Represents a work session entry
#[derive(Debug, Clone, Serialize)]
pub struct WorkSession {
    pub id: i32,
    pub date: String,
    pub position: String, // "A" (office) or "R" (remote)
    pub start: String,
    pub lunch: i32,
    pub end: String,
}

/// Represents a single punch event (in/out)
#[derive(Debug, Clone, Serialize)]
pub struct Event {
    pub id: i32,
    pub date: String,
    pub time: String,     // HH:MM
    pub kind: String,     // "in" or "out"
    pub position: String, // O,R,H,C
    pub lunch_break: i32, // minutes, typically set on out
    pub source: String,
    pub meta: String,
    pub created_at: String, // ISO timestamp
}

fn row_to_worksession(row: &rusqlite::Row) -> Result<WorkSession> {
    Ok(WorkSession {
        id: row.get(0)?,
        date: row.get(1)?,
        position: row.get(2)?,
        start: row.get(3)?,
        lunch: row.get(4)?,
        end: row.get(5)?,
    })
}

fn row_to_event(row: &rusqlite::Row) -> Result<Event> {
    Ok(Event {
        id: row.get(0)?,
        date: row.get(1)?,
        time: row.get(2)?,
        kind: row.get(3)?,
        position: row.get(4)?,
        lunch_break: row.get(5)?,
        source: row.get(6)?,
        meta: row.get(7)?,
        created_at: row.get(8)?,
    })
}

// Generic helper to build a query with optional filters for period and position
fn build_filtered_query(
    base_query: &str,
    period: Option<&str>,
    pos: Option<&str>,
) -> Result<(String, Vec<String>)> {
    let mut query = base_query.to_string();
    let mut conditions = Vec::new();
    let mut params: Vec<String> = Vec::new();

    if let Some(p) = period {
        if p.len() == 4 {
            conditions.push("strftime('%Y', date) = ?".to_string());
            params.push(p.to_string());
        } else if p.len() == 7 {
            conditions.push("strftime('%Y-%m', date) = ?".to_string());
            params.push(p.to_string());
        } else {
            return Err(rusqlite::Error::InvalidQuery);
        }
    }

    if let Some(pos_filter) = pos {
        conditions.push("position = ?".to_string());
        params.push(pos_filter.to_uppercase());
    }

    if !conditions.is_empty() {
        query.push_str(" WHERE ");
        query.push_str(&conditions.join(" AND "));
    }

    Ok((query, params))
}

/// Initialize the database schema.
/// Ensures table `work_sessions` exists and adds missing `position` column if required.
pub fn init_db(conn: &Connection) -> Result<()> {
    conn.execute_batch(
        "
        CREATE TABLE IF NOT EXISTS work_sessions (
            id           INTEGER PRIMARY KEY AUTOINCREMENT,
            date         TEXT NOT NULL,          -- YYYY-MM-DD
            position     TEXT NOT NULL DEFAULT 'O' CHECK (position IN ('O','R','H','C','M')),
            start_time   TEXT NOT NULL DEFAULT '',
            lunch_break  INTEGER NOT NULL DEFAULT 0,
            end_time     TEXT NOT NULL DEFAULT ''
        );

        CREATE TABLE IF NOT EXISTS log (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            date TEXT NOT NULL,
            operation TEXT NOT NULL,
            target TEXT DEFAULT '',
            message TEXT NOT NULL
        );

        CREATE TABLE IF NOT EXISTS events (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            date TEXT NOT NULL,          -- YYYY-MM-DD
            time TEXT NOT NULL,          -- HH:MM
            kind TEXT NOT NULL CHECK (kind IN ('in','out')),
            position TEXT NOT NULL CHECK (position IN ('O','R','H','C','M')),
            lunch_break INTEGER NOT NULL DEFAULT 0, -- minutes, typically set on out
            source TEXT NOT NULL,
            meta TEXT,
            created_at TEXT NOT NULL     -- ISO 8601 timestamp
        );
        ",
    )?;
    run_pending_migrations(conn)?;
    Ok(())
}

/// Aggregate the day's position using events: if no events, returns None.
/// If all event positions are the same, returns that position ("O","R","H","C").
/// If multiple distinct positions exist, returns "M" for Mixed.
pub fn aggregate_position_from_events(conn: &Connection, date: &str) -> Result<Option<String>> {
    let mut stmt = conn.prepare_cached("SELECT DISTINCT position FROM events WHERE date = ?1")?;
    let rows = stmt.query_map([date], |row| row.get::<_, String>(0))?;
    let mut distinct: Vec<String> = Vec::new();
    for r in rows {
        distinct.push(r?);
    }
    if distinct.is_empty() {
        return Ok(None);
    }
    distinct.sort();
    distinct.dedup();
    if distinct.len() == 1 {
        Ok(Some(distinct[0].clone()))
    } else {
        Ok(Some("M".to_string()))
    }
}

/// Insert a new work session
pub fn add_session(
    conn: &Connection,
    date: &str,
    position: &str,
    start: &str,
    lunch: u32,
    end: &str,
) -> Result<()> {
    conn.execute(
        "INSERT INTO work_sessions (date, position, start_time, lunch_break, end_time)
         VALUES (?1, ?2, ?3, ?4, ?5)",
        params![date, position, start, lunch, end],
    )?;
    Ok(())
}

pub fn delete_session(conn: &Connection, id: i32) -> Result<usize> {
    conn.execute("DELETE FROM work_sessions WHERE id = ?", [id])
}

/// Delete all work_sessions for a given date. Returns number of rows deleted.
pub fn delete_sessions_by_date(conn: &Connection, date: &str) -> Result<usize> {
    conn.execute("DELETE FROM work_sessions WHERE date = ?1", params![date])
}

/// Delete all events for a given date. Returns number of rows deleted.
pub fn delete_events_by_date(conn: &Connection, date: &str) -> Result<usize> {
    conn.execute("DELETE FROM events WHERE date = ?1", params![date])
}

/// Delete events by a list of ids. Returns number of rows deleted.
pub fn delete_events_by_ids(conn: &Connection, ids: &[i32]) -> Result<usize> {
    if ids.is_empty() {
        return Ok(0);
    }
    // Build a query with the appropriate number of placeholders
    let mut sql = String::from("DELETE FROM events WHERE id IN (");
    sql.push_str(&vec!["?"; ids.len()].join(","));
    sql.push(')');
    let params_vec: Vec<&dyn ToSql> = ids.iter().map(|i| i as &dyn ToSql).collect();
    let mut stmt = conn.prepare_cached(&sql)?;
    let changed = stmt.execute(rusqlite::params_from_iter(params_vec))?;
    Ok(changed)
}

/// Return all saved work sessions, optionally filtered by year or year-month.
pub fn list_sessions(
    conn: &Connection,
    period: Option<&str>,
    pos: Option<&str>,
) -> Result<Vec<WorkSession>> {
    let base_query =
        "SELECT id, date, position, start_time, lunch_break, end_time FROM work_sessions";
    let (mut query, params) = build_filtered_query(base_query, period, pos)?;

    query.push_str(" ORDER BY date ASC");

    let mut stmt = conn.prepare_cached(&query)?;
    let params_refs: Vec<&dyn ToSql> = params.iter().map(|s| s as &dyn ToSql).collect();
    let rows = stmt.query_map(params_refs.as_slice(), row_to_worksession)?;

    rows.collect::<Result<Vec<_>, _>>()
}

/// Generic upsert helper for a single field in `work_sessions` table.
fn upsert_field<T: ToSql>(
    conn: &Connection,
    date: &str,
    field: &str,
    value: T,
    default_pos: &str,
) -> Result<()> {
    let update_sql = format!("UPDATE work_sessions SET {} = ?1 WHERE date = ?2", field);
    let mut stmt = conn.prepare_cached(&update_sql)?;
    let rows = stmt.execute(params![&value, date])?;

    if rows == 0 {
        let insert_sql = format!(
            "INSERT INTO work_sessions (date, position, {}) VALUES (?1, ?2, ?3)",
            field
        );
        let mut ins = conn.prepare_cached(&insert_sql)?;
        ins.execute(params![date, default_pos, &value])?;
    }
    Ok(())
}

/// Insert or update the position (A=office, R=remote) for a given date.
pub fn upsert_position(conn: &Connection, date: &str, pos: &str) -> Result<()> {
    upsert_field(conn, date, "position", pos, pos)
}

/// Insert or update the start time (HH:MM) for a given date.
pub fn upsert_start(conn: &Connection, date: &str, start: &str) -> Result<()> {
    // Custom logic: only update if start_time is empty
    let mut stmt = conn.prepare_cached(
        "UPDATE work_sessions SET start_time = ?1 WHERE date = ?2 AND (start_time = '' OR start_time IS NULL)",
    )?;
    if stmt.execute(params![start, date])? == 0 {
        let exists = conn
            .query_row(
                "SELECT 1 FROM work_sessions WHERE date = ?1",
                [date],
                |_| Ok(()),
            )
            .optional()?
            .is_some();
        if !exists {
            upsert_field(conn, date, "start_time", start, "O")?;
        }
    }
    Ok(())
}

/// Insert or update the lunch break (minutes) for a given date.
pub fn upsert_lunch(conn: &Connection, date: &str, lunch: i32) -> Result<()> {
    upsert_field(conn, date, "lunch_break", lunch, "O")
}

/// Insert or update the end time (HH:MM) for a given date.
pub fn upsert_end(conn: &Connection, date: &str, end: &str) -> Result<()> {
    upsert_field(conn, date, "end_time", end, "O")
}

pub fn ttlog(conn: &Connection, operation: &str, target: &str, message: &str) -> Result<()> {
    let now = Utc::now().to_rfc3339(); // ISO 8601
    let mut stmt = conn.prepare_cached(
        "INSERT INTO log (date, operation, target, message) VALUES (?1, ?2, ?3, ?4)",
    )?;
    stmt.execute(params![&now, operation, target, message])?;
    Ok(())
}

/// Retrieve a single work session by id
pub fn get_session(conn: &Connection, id: i32) -> Result<Option<WorkSession>> {
    let mut stmt = conn.prepare_cached(
        "SELECT id, date, position, start_time, lunch_break, end_time FROM work_sessions WHERE id = ?1",
    )?;

    match stmt.query_row([id], row_to_worksession) {
        Ok(ws) => Ok(Some(ws)),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(e),
    }
}

/// Retrieve work sessions for a specific date
pub fn list_sessions_by_date(conn: &Connection, date: &str) -> Result<Vec<WorkSession>> {
    let query = "SELECT id, date, position, start_time, lunch_break, end_time FROM work_sessions WHERE date = ?1 ORDER BY date ASC";
    let mut stmt = conn.prepare_cached(query)?;
    let rows = stmt.query_map([date], row_to_worksession)?;

    rows.collect::<Result<Vec<_>, _>>()
}

/// List events for a specific date (ordered by time asc)
pub fn list_events_by_date(conn: &Connection, date: &str) -> Result<Vec<Event>> {
    let mut stmt = conn.prepare_cached(
        "SELECT id, date, time, kind, position, lunch_break, source, meta, created_at FROM events WHERE date = ?1 ORDER BY time ASC",
    )?;
    let rows = stmt.query_map([date], row_to_event)?;

    rows.collect::<Result<Vec<_>, _>>()
}

/// List all events in the database ordered by date and time
pub fn list_events(conn: &Connection) -> Result<Vec<Event>> {
    let mut stmt = conn.prepare_cached(
        "SELECT id, date, time, kind, position, lunch_break, source, meta, created_at FROM events ORDER BY date ASC, time ASC",
    )?;
    let rows = stmt.query_map([], row_to_event)?;

    rows.collect::<Result<Vec<_>, _>>()
}

/// List events filtered by optional period (YYYY or YYYY-MM) and position
pub fn list_events_filtered(
    conn: &Connection,
    period: Option<&str>,
    pos: Option<&str>,
) -> Result<Vec<Event>> {
    let base_query =
        "SELECT id, date, time, kind, position, lunch_break, source, meta, created_at FROM events";
    let (mut query, params) = build_filtered_query(base_query, period, pos)?;

    query.push_str(" ORDER BY date ASC, time ASC");

    let mut stmt = conn.prepare_cached(&query)?;
    let param_refs: Vec<&dyn ToSql> = params.iter().map(|s| s as &dyn ToSql).collect();
    let rows = stmt.query_map(param_refs.as_slice(), row_to_event)?;
    rows.collect::<Result<Vec<_>, _>>()
}

/// Find last out event before a given time on the same date
pub fn last_out_before(conn: &Connection, date: &str, time: &str) -> Result<Option<Event>> {
    let mut stmt = conn.prepare_cached(
        "SELECT id, date, time, kind, position, lunch_break, source, meta, created_at FROM events WHERE date = ?1 AND kind = 'out' AND time < ?2 ORDER BY time DESC LIMIT 1",
    )?;
    match stmt.query_row([date, time], row_to_event) {
        Ok(ev) => Ok(Some(ev)),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(e),
    }
}

/// Update lunch_break for a specific event (typically an 'out')
pub fn set_event_lunch(conn: &Connection, event_id: i32, lunch: i32) -> Result<()> {
    conn.execute(
        "UPDATE events SET lunch_break = ?1 WHERE id = ?2",
        params![lunch, event_id],
    )?;
    Ok(())
}

/// Update time for a specific event
pub fn set_event_time(conn: &Connection, event_id: i32, new_time: &str) -> Result<()> {
    conn.execute(
        "UPDATE events SET time = ?1 WHERE id = ?2",
        params![new_time, event_id],
    )?;
    Ok(())
}

/// Update position for a specific event
pub fn set_event_position(conn: &Connection, event_id: i32, new_pos: &str) -> Result<()> {
    conn.execute(
        "UPDATE events SET position = ?1 WHERE id = ?2",
        params![new_pos, event_id],
    )?;
    Ok(())
}

// Helper used by force_set_* to update or insert a legacy work_sessions row when forcing a single field.
fn force_set_field<T: ToSql>(
    conn: &Connection,
    date: &str,
    field: &str,
    value: T,
    default_pos: &str,
) -> Result<()> {
    let sql = format!("UPDATE work_sessions SET {} = ?1 WHERE date = ?2", field);
    let changed = conn.execute(&sql, params![&value, date])?;
    if changed == 0 {
        // Insert full row with only this field populated (others default)
        let insert_sql = match field {
            "position" => "INSERT INTO work_sessions (date, position) VALUES (?1, ?2)",
            "start_time" => {
                "INSERT INTO work_sessions (date, position, start_time) VALUES (?1, ?2, ?3)"
            }
            "end_time" => {
                "INSERT INTO work_sessions (date, position, end_time) VALUES (?1, ?2, ?3)"
            }
            "lunch_break" => {
                "INSERT INTO work_sessions (date, position, lunch_break) VALUES (?1, ?2, ?3)"
            }
            _ => "INSERT INTO work_sessions (date, position) VALUES (?1, ?2)",
        };
        match field {
            "position" => {
                conn.execute(insert_sql, params![date, &value])?;
            }
            "start_time" | "end_time" | "lunch_break" => {
                conn.execute(insert_sql, params![date, default_pos, &value])?;
            }
            _ => {
                conn.execute(
                    "INSERT INTO work_sessions (date, position) VALUES (?1, ?2)",
                    params![date, default_pos],
                )?;
            }
        }
    }
    Ok(())
}

pub fn force_set_position(conn: &Connection, date: &str, pos: &str) -> Result<()> {
    force_set_field(conn, date, "position", pos, pos)
}

pub fn force_set_start(conn: &Connection, date: &str, start: &str) -> Result<()> {
    force_set_field(conn, date, "start_time", start, "O")
}

pub fn force_set_end(conn: &Connection, date: &str, end: &str) -> Result<()> {
    force_set_field(conn, date, "end_time", end, "O")
}

pub fn force_set_lunch(conn: &Connection, date: &str, lunch: i32) -> Result<()> {
    force_set_field(conn, date, "lunch_break", lunch, "O")
}

pub fn count_events_by_date(conn: &Connection, date: &str) -> Result<i64> {
    let mut stmt = conn.prepare_cached("SELECT COUNT(*) FROM events WHERE date = ?1")?;
    let n: i64 = stmt.query_row([date], |r| r.get(0))?;
    Ok(n)
}

pub fn count_sessions_by_date(conn: &Connection, date: &str) -> Result<i64> {
    let mut stmt = conn.prepare_cached("SELECT COUNT(*) FROM work_sessions WHERE date = ?1")?;
    let n: i64 = stmt.query_row([date], |r| r.get(0))?;
    Ok(n)
}

// Struct per passare argomenti alla funzione add_event
pub struct AddEventArgs<'a> {
    pub date: &'a str,
    pub time: &'a str,
    pub kind: &'a str,
    pub position: Option<&'a str>,
    pub source: &'a str,
    pub meta: Option<&'a str>,
}

/// Insert an event and run auto-lunch logic if kind == 'in'.
/// This function uses a transaction to ensure atomicity. It also performs dual-write
/// to legacy `work_sessions` via existing upsert_* helpers to keep backwards compatibility.
#[allow(clippy::too_many_arguments)]
pub fn add_event(
    conn: &mut Connection,
    args: &AddEventArgs,
    config: &crate::config::Config,
) -> Result<i64> {
    let tx = conn.transaction()?;

    // Determine position_to_use:
    // - if user provided position (Some) -> use it
    // - else if kind == 'out' -> try to inherit from last 'in' on the same date
    // - otherwise fallback to config.default_position
    let mut position_to_use = if let Some(p) = args.position {
        p.to_string()
    } else {
        config.default_position.clone()
    };
    if args.position.is_none() && args.kind == "out" {
        let mut stmt = tx.prepare_cached(
            "SELECT position FROM events WHERE date = ?1 AND kind = 'in' AND time <= ?2 ORDER BY time DESC LIMIT 1",
        )?;
        if let Some(found_pos) = stmt
            .query_row([args.date, args.time], |row| row.get::<_, String>(0))
            .optional()?
        {
            position_to_use = found_pos;
        }
    }

    tx.execute(
        "INSERT INTO events (date, time, kind, position, lunch_break, source, meta, created_at) VALUES (?1, ?2, ?3, ?4, 0, ?5, ?6, ?7)",
        params![args.date, args.time, args.kind, position_to_use, args.source, args.meta.unwrap_or(""), Utc::now().to_rfc3339()],
    )?;

    let event_id = tx.last_insert_rowid();

    // Dual-write to legacy table to ease rollout
    if args.kind == "in" {
        // store start in legacy place
        let _ = upsert_start(&tx, args.date, args.time);
    } else if args.kind == "out" {
        let _ = upsert_end(&tx, args.date, args.time);
    }

    // If this is an 'in' event, attempt to populate lunch on the previous 'out' (auto-lunch)
    if args.kind == "in"
        && let Some(prev_out) = last_out_before(&tx, args.date, args.time)?
    {
        // Exclude holiday positions
        if prev_out.position != "H" && position_to_use != "H" {
            // Ensure previous out has no lunch yet
            if prev_out.lunch_break == 0 {
                // Parse times
                if let (Ok(prev_time), Ok(new_time)) = (
                    NaiveTime::parse_from_str(&prev_out.time, "%H:%M"),
                    NaiveTime::parse_from_str(args.time, "%H:%M"),
                ) {
                    let noon = NaiveTime::from_hms_opt(12, 0, 0).unwrap();
                    let latest = NaiveTime::from_hms_opt(14, 30, 0).unwrap();
                    if prev_time >= noon && new_time <= latest && new_time > prev_time {
                        let delta = (new_time - prev_time).num_minutes() as i32;
                        let mut lunch_val = delta;
                        if lunch_val < config.min_duration_lunch_break {
                            lunch_val = config.min_duration_lunch_break;
                        }
                        if lunch_val > config.max_duration_lunch_break {
                            lunch_val = config.max_duration_lunch_break;
                        }
                        if lunch_val > 0 {
                            tx.execute(
                                "UPDATE events SET lunch_break = ?1 WHERE id = ?2",
                                params![lunch_val, prev_out.id],
                            )?;
                            // also update legacy work_sessions lunch for compatibility
                            let _ = upsert_lunch(&tx, args.date, lunch_val);
                            // write an audit log entry inside the same transaction
                            let msg = format!(
                                "auto_lunch {} min for out_event {} (date={})",
                                lunch_val, prev_out.id, args.date
                            );
                            tx.execute(
                                "INSERT INTO log (date, operation, message) VALUES (?1, ?2, ?3)",
                                params![Utc::now().to_rfc3339(), "auto_lunch", msg],
                            )?;
                        }
                    }
                }
            }
        }
    }

    tx.commit()?;
    Ok(event_id)
}

/// Reconstruct work sessions from events for a given date.
/// Produces one WorkSession per matched in/out pair (or partial if unmatched).
pub fn reconstruct_sessions_from_events(conn: &Connection, date: &str) -> Result<Vec<WorkSession>> {
    let events = list_events_by_date(conn, date)?;
    let mut sessions: Vec<WorkSession> = Vec::new();

    let mut pending_in: Option<Event> = None;

    for e in events {
        if e.kind == "in" {
            // treat latest 'in' as the pending entry
            pending_in = Some(e);
        } else if e.kind == "out" {
            if let Some(in_ev) = pending_in.take() {
                // matched pair
                let ws = WorkSession {
                    id: e.id, // use out event id as session id
                    date: date.to_string(),
                    position: in_ev.position.clone(),
                    start: in_ev.time.clone(),
                    lunch: e.lunch_break,
                    end: e.time.clone(),
                };
                sessions.push(ws);
            } else {
                // out without in -> partial session
                let ws = WorkSession {
                    id: e.id,
                    date: date.to_string(),
                    position: e.position.clone(),
                    start: "".to_string(),
                    lunch: e.lunch_break,
                    end: e.time.clone(),
                };
                sessions.push(ws);
            }
        }
    }

    // any remaining pending_in -> incomplete session
    if let Some(in_ev) = pending_in {
        let ws = WorkSession {
            id: in_ev.id,
            date: date.to_string(),
            position: in_ev.position.clone(),
            start: in_ev.time.clone(),
            lunch: 0,
            end: "".to_string(),
        };
        sessions.push(ws);
    }

    Ok(sessions)
}

/// Delete events by ids and recompute/update work_sessions for the given date atomically.
pub fn delete_events_by_ids_and_recompute_sessions(
    conn: &mut Connection,
    ids: &[i32],
    date: &str,
) -> Result<usize> {
    if ids.is_empty() {
        return Ok(0);
    }

    let tx = conn.transaction()?;

    // Execute delete inside a narrow scope so statement is dropped early
    let deleted = {
        // Build delete SQL
        let mut sql = String::from("DELETE FROM events WHERE id IN (");
        sql.push_str(&vec!["?"; ids.len()].join(","));
        sql.push(')');
        let params_vec: Vec<&dyn ToSql> = ids.iter().map(|i| i as &dyn ToSql).collect();
        let mut del_stmt = tx.prepare(&sql)?;

        del_stmt.execute(rusqlite::params_from_iter(params_vec))?
    };

    // Query remaining events for the date inside the same transaction; keep statement scoped
    let mut remaining: Vec<Event> = Vec::new();
    {
        let mut sel = tx.prepare(
            "SELECT id, date, time, kind, position, lunch_break, source, meta, created_at FROM events WHERE date = ?1 ORDER BY time ASC",
        )?;
        let remaining_rows = sel.query_map([date], row_to_event)?;
        for r in remaining_rows {
            remaining.push(r?);
        }
        // sel and remaining_rows dropped here
    }

    if remaining.is_empty() {
        // delete work_sessions row(s) for date
        tx.execute("DELETE FROM work_sessions WHERE date = ?1", params![date])?;
    } else {
        // end_time = max time among remaining events
        if let Some(max_time) = remaining.iter().map(|e| e.time.clone()).max() {
            // Update or insert end_time via existing helper using the transaction
            // We'll use direct SQL to update within the tx (force_set_end uses Connection methods)
            let changed = tx.execute(
                "UPDATE work_sessions SET end_time = ?1 WHERE date = ?2",
                params![&max_time, date],
            )?;
            if changed == 0 {
                tx.execute(
                    "INSERT INTO work_sessions (date, position, end_time) VALUES (?1, ?2, ?3)",
                    params![date, "O", &max_time],
                )?;
            }
        }

        // start_time: choose earliest 'in' if any, otherwise earliest event time
        let min_time_opt = remaining
            .iter()
            .filter(|e| e.kind == "in")
            .map(|e| e.time.clone())
            .min()
            .or_else(|| remaining.iter().map(|e| e.time.clone()).min());
        if let Some(min_time) = min_time_opt {
            let changed = tx.execute(
                "UPDATE work_sessions SET start_time = ?1 WHERE date = ?2",
                params![&min_time, date],
            )?;
            if changed == 0 {
                tx.execute(
                    "INSERT INTO work_sessions (date, position, start_time) VALUES (?1, ?2, ?3)",
                    params![date, "O", &min_time],
                )?;
            }
        }

        // lunch_break: get the latest 'out' event (max time among kind='out') and use its lunch_break
        let last_out = remaining
            .iter()
            .filter(|e| e.kind == "out")
            .max_by(|a, b| a.time.cmp(&b.time));
        if let Some(out_ev) = last_out {
            let lunch_val = out_ev.lunch_break;
            let changed = tx.execute(
                "UPDATE work_sessions SET lunch_break = ?1 WHERE date = ?2",
                params![lunch_val, date],
            )?;
            if changed == 0 {
                tx.execute(
                    "INSERT INTO work_sessions (date, position, lunch_break) VALUES (?1, ?2, ?3)",
                    params![date, "O", lunch_val],
                )?;
            }
        }

        // position: if all remaining events share same position, set it; otherwise leave as-is
        let mut positions: Vec<String> = remaining.iter().map(|e| e.position.clone()).collect();
        positions.sort();
        positions.dedup();
        if positions.len() == 1 {
            let pos = &positions[0];
            let changed = tx.execute(
                "UPDATE work_sessions SET position = ?1 WHERE date = ?2",
                params![pos, date],
            )?;
            if changed == 0 {
                tx.execute(
                    "INSERT INTO work_sessions (date, position) VALUES (?1, ?2)",
                    params![date, pos],
                )?;
            }
        }
    }

    tx.commit()?;
    Ok(deleted)
}