export class RuleRepository {
    db;
    stmts;
    constructor(db) {
        this.db = db;
        this.stmts = {
            create: this.db.prepare(`
        INSERT INTO rules (pattern, action, description, confidence, occurrences, active, project_id)
        VALUES (@pattern, @action, @description, @confidence, @occurrences, @active, @project_id)
      `),
            getById: this.db.prepare(`
        SELECT * FROM rules WHERE id = ?
      `),
            update: this.db.prepare(`
        UPDATE rules
        SET pattern = COALESCE(@pattern, pattern),
            action = COALESCE(@action, action),
            description = COALESCE(@description, description),
            confidence = COALESCE(@confidence, confidence),
            occurrences = COALESCE(@occurrences, occurrences),
            active = COALESCE(@active, active),
            project_id = COALESCE(@project_id, project_id),
            updated_at = datetime('now')
        WHERE id = @id
      `),
            delete: this.db.prepare(`
        DELETE FROM rules WHERE id = ?
      `),
            findActiveAll: this.db.prepare(`
        SELECT * FROM rules WHERE active = 1 ORDER BY confidence DESC
      `),
            findActiveByProject: this.db.prepare(`
        SELECT * FROM rules WHERE active = 1 AND (project_id = ? OR project_id IS NULL) ORDER BY confidence DESC
      `),
            findByPattern: this.db.prepare(`
        SELECT * FROM rules WHERE pattern = ?
      `),
        };
    }
    create(data) {
        const result = this.stmts.create.run({
            pattern: data.pattern,
            action: data.action,
            description: data.description ?? null,
            confidence: data.confidence ?? 0.5,
            occurrences: data.occurrences ?? 0,
            active: data.active ?? 1,
            project_id: data.project_id ?? null,
        });
        return result.lastInsertRowid;
    }
    getById(id) {
        return this.stmts.getById.get(id);
    }
    update(id, data) {
        this.stmts.update.run({
            id,
            pattern: data.pattern ?? null,
            action: data.action ?? null,
            description: data.description ?? null,
            confidence: data.confidence ?? null,
            occurrences: data.occurrences ?? null,
            active: data.active ?? null,
            project_id: data.project_id ?? null,
            updated_at: data.updated_at ?? null,
        });
    }
    delete(id) {
        this.stmts.delete.run(id);
    }
    findActive(projectId) {
        if (projectId !== undefined) {
            return this.stmts.findActiveByProject.all(projectId);
        }
        return this.stmts.findActiveAll.all();
    }
    findByPattern(pattern) {
        return this.stmts.findByPattern.all(pattern);
    }
}
//# sourceMappingURL=rule.repository.js.map