/**
 * SQLite PmNotification Repository Implementation
 *
 * Implements INotificationRepository using better-sqlite3.
 * Manages in-app notifications for PM events.
 */
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/notification.mapper.js';
let SQLiteNotificationRepository = class SQLiteNotificationRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(notification) {
        const row = toDatabase(notification);
        const stmt = this.db.prepare(`
      INSERT INTO pm_notifications (
        id, project_id, recipient_id, type, title, body,
        is_read, is_archived, reference_id, reference_type,
        created_at, updated_at, deleted_at
      ) VALUES (
        @id, @project_id, @recipient_id, @type, @title, @body,
        @is_read, @is_archived, @reference_id, @reference_type,
        @created_at, @updated_at, @deleted_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM pm_notifications WHERE id = ? AND deleted_at IS NULL');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async listByRecipient(recipientId, options) {
        const conditions = ['recipient_id = ?', 'deleted_at IS NULL', 'is_archived = 0'];
        const params = [recipientId];
        if (options?.unreadOnly) {
            conditions.push('is_read = 0');
        }
        if (options?.projectId) {
            conditions.push('project_id = ?');
            params.push(options.projectId);
        }
        const limit = options?.limit ?? 50;
        const sql = `SELECT * FROM pm_notifications WHERE ${conditions.join(' AND ')} ORDER BY created_at DESC LIMIT ?`;
        params.push(limit);
        const stmt = this.db.prepare(sql);
        const rows = stmt.all(...params);
        return rows.map(fromDatabase);
    }
    async markRead(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE pm_notifications SET is_read = 1, updated_at = ? WHERE id = ?');
        stmt.run(now, id);
    }
    async markAllRead(recipientId, projectId) {
        const now = Date.now();
        if (projectId) {
            const stmt = this.db.prepare('UPDATE pm_notifications SET is_read = 1, updated_at = ? WHERE recipient_id = ? AND project_id = ? AND is_read = 0');
            stmt.run(now, recipientId, projectId);
        }
        else {
            const stmt = this.db.prepare('UPDATE pm_notifications SET is_read = 1, updated_at = ? WHERE recipient_id = ? AND is_read = 0');
            stmt.run(now, recipientId);
        }
    }
    async archive(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE pm_notifications SET is_archived = 1, updated_at = ? WHERE id = ?');
        stmt.run(now, id);
    }
    async countUnread(recipientId, projectId) {
        if (projectId) {
            const stmt = this.db.prepare('SELECT COUNT(*) as count FROM pm_notifications WHERE recipient_id = ? AND project_id = ? AND is_read = 0 AND deleted_at IS NULL AND is_archived = 0');
            const result = stmt.get(recipientId, projectId);
            return result.count;
        }
        const stmt = this.db.prepare('SELECT COUNT(*) as count FROM pm_notifications WHERE recipient_id = ? AND is_read = 0 AND deleted_at IS NULL AND is_archived = 0');
        const result = stmt.get(recipientId);
        return result.count;
    }
    async softDelete(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE pm_notifications SET deleted_at = ?, updated_at = ? WHERE id = ?');
        stmt.run(now, now, id);
    }
};
SQLiteNotificationRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteNotificationRepository);
export { SQLiteNotificationRepository };
