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/pm-session.mapper.js';
let SQLitePmSessionRepository = class SQLitePmSessionRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(session) {
        const row = toDatabase(session);
        const stmt = this.db.prepare(`
      INSERT INTO pm_sessions (
        id, user_id, token, expires_at,
        created_at, updated_at, deleted_at
      ) VALUES (
        @id, @user_id, @token, @expires_at,
        @created_at, @updated_at, @deleted_at
      )
    `);
        stmt.run(row);
    }
    async findByToken(token) {
        const stmt = this.db.prepare('SELECT * FROM pm_sessions WHERE token = ? AND deleted_at IS NULL');
        const row = stmt.get(token);
        return row ? fromDatabase(row) : null;
    }
    async findValidByToken(token) {
        const now = Date.now();
        const stmt = this.db.prepare('SELECT * FROM pm_sessions WHERE token = ? AND deleted_at IS NULL AND expires_at > ?');
        const row = stmt.get(token, now);
        return row ? fromDatabase(row) : null;
    }
    async listByUser(userId) {
        const stmt = this.db.prepare('SELECT * FROM pm_sessions WHERE user_id = ? AND deleted_at IS NULL ORDER BY created_at DESC');
        const rows = stmt.all(userId);
        return rows.map(fromDatabase);
    }
    async softDelete(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE pm_sessions SET deleted_at = ? WHERE id = ?');
        stmt.run(now, id);
    }
    async deleteExpired() {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE pm_sessions SET deleted_at = ? WHERE expires_at <= ? AND deleted_at IS NULL');
        const result = stmt.run(now, now);
        return result.changes;
    }
};
SQLitePmSessionRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLitePmSessionRepository);
export { SQLitePmSessionRepository };
