import type { DatabaseSync } from 'node:sqlite' import { localDateString } from '../../services/writing-date.util' export class PomodoroDailyRepository { constructor(private db: DatabaseSync) {} recordComplete(bookId: string, wordDelta: number, date = localDateString()): void { this.db .prepare( `INSERT INTO pomodoro_daily (date, book_id, completed_count, total_words) VALUES (?, ?, 1, ?) ON CONFLICT(date, book_id) DO UPDATE SET completed_count = completed_count + 1, total_words = total_words + excluded.total_words` ) .run(date, bookId, wordDelta) } getTodayCount(bookId: string, date = localDateString()): number { const row = this.db .prepare('SELECT completed_count FROM pomodoro_daily WHERE date = ? AND book_id = ?') .get(date, bookId) as { completed_count: number } | undefined return row?.completed_count ?? 0 } getTodayStats( bookId: string, date = localDateString() ): { completedCount: number; totalWords: number } { const row = this.db .prepare( 'SELECT completed_count, total_words FROM pomodoro_daily WHERE date = ? AND book_id = ?' ) .get(date, bookId) as { completed_count: number; total_words: number } | undefined return { completedCount: row?.completed_count ?? 0, totalWords: row?.total_words ?? 0 } } }