export class AntipatternRepository {
    db;
    stmts;
    constructor(db) {
        this.db = db;
        this.stmts = {
            create: db.prepare(`
        INSERT INTO antipatterns (pattern, description, severity, suggestion, occurrences, project_id, global)
        VALUES (@pattern, @description, @severity, @suggestion, @occurrences, @project_id, @global)
      `),
            getById: db.prepare('SELECT * FROM antipatterns WHERE id = ?'),
            delete: db.prepare('DELETE FROM antipatterns WHERE id = ?'),
            findByProject: db.prepare('SELECT * FROM antipatterns WHERE project_id = ? ORDER BY occurrences DESC'),
            findGlobal: db.prepare('SELECT * FROM antipatterns WHERE global = 1 ORDER BY occurrences DESC'),
        };
    }
    create(data) {
        const result = this.stmts.create.run(data);
        return result.lastInsertRowid;
    }
    getById(id) {
        return this.stmts.getById.get(id);
    }
    update(id, data) {
        const fields = Object.keys(data).filter((key) => data[key] !== undefined);
        if (fields.length === 0)
            return false;
        const setClauses = fields.map((field) => `${field} = @${field}`).join(', ');
        const stmt = this.db.prepare(`UPDATE antipatterns SET ${setClauses} WHERE id = @id`);
        const result = stmt.run({ ...data, id });
        return result.changes > 0;
    }
    delete(id) {
        const result = this.stmts.delete.run(id);
        return result.changes > 0;
    }
    findByProject(projectId) {
        return this.stmts.findByProject.all(projectId);
    }
    findGlobal() {
        return this.stmts.findGlobal.all();
    }
}
//# sourceMappingURL=antipattern.repository.js.map