export class ErrorRepository {
    db;
    stmts;
    constructor(db) {
        this.db = db;
        this.stmts = {
            create: this.db.prepare(`
        INSERT INTO errors (project_id, terminal_id, fingerprint, type, message, raw_output, context, file_path, line_number, column_number)
        VALUES (@project_id, @terminal_id, @fingerprint, @type, @message, @raw_output, @context, @file_path, @line_number, @column_number)
      `),
            createChain: this.db.prepare(`
        INSERT OR IGNORE INTO error_chains (parent_error_id, child_error_id, relationship)
        VALUES (@parent_error_id, @child_error_id, @relationship)
      `),
            findChainChildren: this.db.prepare('SELECT e.* FROM errors e JOIN error_chains ec ON e.id = ec.child_error_id WHERE ec.parent_error_id = ?'),
            findChainParents: this.db.prepare('SELECT e.* FROM errors e JOIN error_chains ec ON e.id = ec.parent_error_id WHERE ec.child_error_id = ?'),
            findRecentByProject: this.db.prepare('SELECT * FROM errors WHERE project_id = ? AND first_seen >= ? ORDER BY first_seen DESC LIMIT ?'),
            findAllPaginated: this.db.prepare('SELECT * FROM errors ORDER BY last_seen DESC LIMIT ? OFFSET ?'),
            getById: this.db.prepare(`
        SELECT * FROM errors WHERE id = ?
      `),
            update: this.db.prepare(`
        UPDATE errors
        SET project_id = COALESCE(@project_id, project_id),
            terminal_id = COALESCE(@terminal_id, terminal_id),
            fingerprint = COALESCE(@fingerprint, fingerprint),
            type = COALESCE(@type, type),
            message = COALESCE(@message, message),
            raw_output = COALESCE(@raw_output, raw_output),
            context = COALESCE(@context, context),
            file_path = COALESCE(@file_path, file_path),
            line_number = COALESCE(@line_number, line_number),
            column_number = COALESCE(@column_number, column_number),
            occurrence_count = COALESCE(@occurrence_count, occurrence_count),
            last_seen = COALESCE(@last_seen, last_seen),
            resolved = COALESCE(@resolved, resolved),
            resolved_at = COALESCE(@resolved_at, resolved_at)
        WHERE id = @id
      `),
            delete: this.db.prepare(`
        DELETE FROM errors WHERE id = ?
      `),
            findByFingerprint: this.db.prepare(`
        SELECT * FROM errors WHERE fingerprint = ? ORDER BY last_seen DESC
      `),
            findByProject: this.db.prepare(`
        SELECT * FROM errors WHERE project_id = ? ORDER BY last_seen DESC
      `),
            findUnresolvedAll: this.db.prepare(`
        SELECT * FROM errors WHERE resolved = 0 ORDER BY last_seen DESC
      `),
            findUnresolvedByProject: this.db.prepare(`
        SELECT * FROM errors WHERE resolved = 0 AND project_id = ? ORDER BY last_seen DESC
      `),
            countSinceAll: this.db.prepare(`
        SELECT COUNT(*) as count FROM errors WHERE first_seen >= ?
      `),
            countSinceByProject: this.db.prepare(`
        SELECT COUNT(*) as count FROM errors WHERE first_seen >= ? AND project_id = ?
      `),
            search: this.db.prepare(`
        SELECT errors.* FROM errors
        JOIN errors_fts ON errors.id = errors_fts.rowid
        WHERE errors_fts MATCH ?
        ORDER BY rank
      `),
            incrementOccurrence: this.db.prepare(`
        UPDATE errors
        SET occurrence_count = occurrence_count + 1,
            last_seen = datetime('now')
        WHERE id = ?
      `),
        };
    }
    create(data) {
        const result = this.stmts.create.run({
            project_id: data.project_id,
            terminal_id: data.terminal_id ?? null,
            fingerprint: data.fingerprint,
            type: data.type,
            message: data.message,
            raw_output: data.raw_output,
            context: data.context ?? null,
            file_path: data.file_path ?? null,
            line_number: data.line_number ?? null,
            column_number: data.column_number ?? null,
        });
        return result.lastInsertRowid;
    }
    getById(id) {
        return this.stmts.getById.get(id);
    }
    update(id, data) {
        this.stmts.update.run({
            id,
            project_id: data.project_id ?? null,
            terminal_id: data.terminal_id ?? null,
            fingerprint: data.fingerprint ?? null,
            type: data.type ?? null,
            message: data.message ?? null,
            raw_output: data.raw_output ?? null,
            context: data.context ?? null,
            file_path: data.file_path ?? null,
            line_number: data.line_number ?? null,
            column_number: data.column_number ?? null,
            occurrence_count: data.occurrence_count ?? null,
            last_seen: data.last_seen ?? null,
            resolved: data.resolved ?? null,
            resolved_at: data.resolved_at ?? null,
        });
    }
    delete(id) {
        this.stmts.delete.run(id);
    }
    findByFingerprint(fingerprint) {
        return this.stmts.findByFingerprint.all(fingerprint);
    }
    findByProject(projectId) {
        return this.stmts.findByProject.all(projectId);
    }
    findUnresolved(projectId) {
        if (projectId !== undefined) {
            return this.stmts.findUnresolvedByProject.all(projectId);
        }
        return this.stmts.findUnresolvedAll.all();
    }
    countSince(since, projectId) {
        if (projectId !== undefined) {
            const row = this.stmts.countSinceByProject.get(since, projectId);
            return row.count;
        }
        const row = this.stmts.countSinceAll.get(since);
        return row.count;
    }
    search(query) {
        return this.stmts.search.all(query);
    }
    incrementOccurrence(id) {
        this.stmts.incrementOccurrence.run(id);
    }
    createChain(parentErrorId, childErrorId, relationship = 'caused_by_fix') {
        this.stmts.createChain.run({
            parent_error_id: parentErrorId,
            child_error_id: childErrorId,
            relationship,
        });
    }
    findChainChildren(errorId) {
        return this.stmts.findChainChildren.all(errorId);
    }
    findChainParents(errorId) {
        return this.stmts.findChainParents.all(errorId);
    }
    findRecentByProject(projectId, since, limit = 10) {
        return this.stmts.findRecentByProject.all(projectId, since, limit);
    }
    findAll(limit = 100, offset = 0) {
        return this.stmts.findAllPaginated.all(limit, offset);
    }
}
//# sourceMappingURL=error.repository.js.map