/**
 * SQLite Time Entry Repository Implementation
 *
 * Implements ITimeEntryRepository using better-sqlite3.
 * Tracks time logged against work items for project time management.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { injectable } from 'tsyringe';
import { toDatabase, fromDatabase, } from '../persistence/sqlite/mappers/time-entry.mapper.js';
let SQLiteTimeEntryRepository = class SQLiteTimeEntryRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(entry) {
        const row = toDatabase(entry);
        const stmt = this.db.prepare(`
      INSERT INTO time_entries (
        id, work_item_id, duration_minutes, note,
        logged_at, created_at, updated_at
      ) VALUES (
        @id, @work_item_id, @duration_minutes, @note,
        @logged_at, @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM time_entries WHERE id = ?');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async listByWorkItem(workItemId) {
        const stmt = this.db.prepare('SELECT * FROM time_entries WHERE work_item_id = ? ORDER BY logged_at DESC');
        const rows = stmt.all(workItemId);
        return rows.map(fromDatabase);
    }
    async listByProject(projectId, startDate, endDate) {
        const conditions = ['wi.project_id = ?'];
        const values = [projectId];
        if (startDate !== undefined) {
            conditions.push('te.logged_at >= ?');
            values.push(startDate instanceof Date ? startDate.getTime() : startDate);
        }
        if (endDate !== undefined) {
            conditions.push('te.logged_at <= ?');
            values.push(endDate instanceof Date ? endDate.getTime() : endDate);
        }
        const stmt = this.db.prepare(`
      SELECT te.* FROM time_entries te
      JOIN work_items wi ON wi.id = te.work_item_id
      WHERE ${conditions.join(' AND ')}
      ORDER BY te.logged_at DESC
    `);
        const rows = stmt.all(...values);
        return rows.map(fromDatabase);
    }
    async getTotalMinutes(workItemId) {
        const stmt = this.db.prepare('SELECT COALESCE(SUM(duration_minutes), 0) as total FROM time_entries WHERE work_item_id = ?');
        const row = stmt.get(workItemId);
        return row.total;
    }
    async delete(id) {
        const stmt = this.db.prepare('DELETE FROM time_entries WHERE id = ?');
        stmt.run(id);
    }
};
SQLiteTimeEntryRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteTimeEntryRepository);
export { SQLiteTimeEntryRepository };
